ManyToMany Joins & When to Avoid ManyToMany
…want to
store, then you should not use a ManyToMany relationship. In fact, you can't use
Doctrine at all, and you need to buy a new computer.
I'm kidding. If you need to store extra data on the article_tag table, then,
instead…
Saving a ManyToMany Relation + Joins
…know, that looks weird: we're calling a method but not using it! But, calling
this method is enough to make Doctrine query for the tag's real data. Below,
dump($tag) and die after the loop.
Load the fixtures again:
Boom! We have data…
The 4 (2?) Possible Relation Types
…have a user_id foreign key to
the user table. The only difference is that doctrine would make that column
unique to prevent you from accidentally linking multiple profiles to the same
user.
The point is, OneToOne relationships are kind of ManyToOne relationships in disguise…
Giving the Comments an isDeleted Flag
…the date, add an if statement: if
comment.isDeleted, then, add a close, "X", icon and say "deleted":
Find your terminal and freshen up your fixtures:
php bin/console doctrine:fixtures:load
When that finishes, move back, refresh... then scroll down. Let's see... yea…
OrderBy & fetch EXTRA_LAZY
… Move over and, refresh! Brilliant! The newest comments are on top.
This actually changed how Doctrine queries for the related comments.
Oh, and I want to mention two quick things about the syntax for annotations.
First... well... the syntax can sometimes be confusing - where to…
Awesome Random Fixtures
…fixed, let's rename the class back from this ridiculous name to
CommentFixture:
To celebrate, move over, refresh and... awesome! 8, random comments. We rock!
Next, let's learn about some tricks to control how Doctrine fetches the
comments for an article, like, their order.
Fetching Relations
…s why we use 'article' => $article. Of course, behind the
scenes, Doctrine will make a query where article_id = the id from this Article.
But, in PHP, we think all about objects.
As nice as this was... there is a much simpler way…
CollectionType Field
…about the owning and the
inverse sides of the relationship, and things called orphanRemoval and cascading.
There is some significant Doctrine magic going on behind the scenes to get it working.
So in a few minutes, we're going to look at a more custom…
Fixtures: Dummy Data Rocks
…not available
in the prod environment. That's fine for us - this is a development tool - and it keeps
the prod environment a little smaller.
Anyways, this bundle gives us a new console command - doctrine:fixtures:load. When
we run that, it'll look for …
Inserting new Objects
…on a slow query.
I still can't believe it's working - things never work on the first try! To
triple-check it, head to the terminal. To run a raw SQL query, use:
There they are. So inserting objects with Doctrine... pretty darn easy.
Test Fixtures and the PropertyAccess Component
…what a wonderfully
trained function:
Let's save this! Since we'll need the EntityManager a lot in this class,
let's add a protected function getEntityManager(). Use getService() with
doctrine.orm.entity_manager. And since I love autocomplete, give this
PHPDoc:
Now $this->getEntityManager()…
Tests with the Container
…means
we have an easy way to clear data. Create a new private function called purgeDatabase().
Because we have the Doctrine DataFixtures
library installed, we can use a great class called ORMPurger. Pass it the
EntityManager - so $this->getService('doctrine')->getManager(). To clear
things out…
Making an Argument Available to All Controllers
…which works via a listener to kernel.controller,
grabs the id off of the request attributes, queries for a Post object
via Doctrine with that id, and then adds a new request attribute called post
that’s set to that object:
// Summarized version of ParamConverterListener…
Symfony: Keep it Simple with @Route and Templates
…
So let’s finish this page. It should be fairly straightforward: we’re going
to use Doctrine to query for all the posts and then pass them into a template:
/**
@Route("/posts")
/
public function indexAction()
{
}
Now, notice that my template name does not have any…
Welcome to Symfony!
…requests, responses and
templating.
Get comfortable with a few extra tools, like Doctrine for dealing with
the database and code generation tools.
And the real reason you are here, to learn enough best practices and tips
to impress your friends at a party.
Ready? Let…
Upgrading to 2.2
…replace the core Symfony libraries, and not any
custom lines you may have added. In this case, the doctrine-fixtures-bundle
is custom, so I’ll leave it alone:
"require": {
},
Also, be sure you have the minimum-stability set to alpha. As Stof
points out…
Improving Javascript Event Security
…user.
Head over to your terminal and run:
bin/console doctrine:query:sql "SELECT * FROM user"
We have the lsCustomerId set, so let's reset it to NULL with:
bin/console doctrine:query:sql "UPDATE user SET lsCustomerId=NULL WHERE id=1"
Let's test it out…
Writing an Integrational Test for Webhooks
…an error, but that's expected.
No such table in the test environment.
Our test is failing because we need to set up a test database. We
could do this manually using Doctrine console commands, but let's take
advantage of Zenstruck Foundry, which we…
Implementing the Webhook Consumer
…method, fetch the user with
$user = $this->entityManager->getRepository(User::class)->find($userId).
Next, if $user doesn't exist, we'll throw new EntityNotFoundException()
(choose the one from Doctrine\ORM). For the message write
sprintf('User "%s" not found for LemonSqueezy webhook "%s"!', $userId…
Adding Lists: Value Type
…The same is true if you're using Ibexa CMS.
So here's our next big goal: to add our Recipe Doctrine entity as a "value
type" in layouts so that we can create lists and grids containing recipes.
Step one to adding a value…
x
1000+