Updating an Entity
…not needed for updates! When you query Doctrine for an object, it already
knows that you want that object to be saved to the database when you call flush().
Doctrine is also smart enough to know that it should update the object, instead
of inserting…
Using Faker for Seeding Data
…Faker function:
$this->faker->numberBetween(5, 100):
After these few improvements, let's make sure the system is actually as easy
as pie. Find your terminal and run:
php bin/console doctrine:fixtures:load
No errors and... back on the browser, it works! Of course…
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.
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…
Fixture References & Relating Objects
…for each loop. For the second argument, it passes
the object itself.
This reference system is a little "extra" built into Doctrine's fixtures library.
When you add a "reference" from one fixture class, you can fetch it out in another
class. It's super…
Partial Mocking
…going on? This is subtle: PhpUnit is smart enough to take this one dinosaur object
and return it each time growFromSpecification() is called. But to Doctrine, it
looks like we're asking it to save the same one Dinosaur object: not three separate
dinosaurs. The…
Test Fixtures & Fast Databases!
…step
will have already been done for you automatically.
We haven't hooked the fixtures into our tests yet, but we can at least try them!
Run:
php bin/console doctrine:fixtures:load
Go check out the browser! Yes! The fixtures gave us 3 enclosures…
Safe Migrations
…back a deploy before. But, let's update it to be safe: SET image = poster, and
then ALTER TABLE to drop poster:
This is a safe migration. First, try it locally:
./bin/console doctrine:migrations:migrate
Perfect! And now... deploy! Right? No! Stop that…
Priming cache.app
…production. Add framework, cache and app set to cache.adapter.redis:
cache.adapter.redis is the id of a service that Symfony automatically makes
available. You can also use cache.adapter.filesystem - which is the default -
doctrine, apcu, memcached or create your own service. If…
Tests, Assertions & Coding
…tutorial/ directory with a Dinosaur class inside. Copy that and paste it
into the real Entity directory.
This is just a simple class with a length property. It does have Doctrine annotations,
but that's not important! Sure, we will eventually be able to save…
Customizing the Forms
…production anyways, but, you've been warned!
Move over to your terminal to generate the migration:
php bin/console doctrine:migrations:diff
That looks right! Run it:
php bin/console doctrine:migrations:migrate
New field added! Now, how can we add it to the form?…
My Users don't have a Username!
… No more username! Register as aquanaut4@gmail.com
and submit. Check it out in the database:
php bin/console doctrine:query:sql 'SELECT * FROM user'
There it is! The username is now equal to the email.
Oh, and if you're using the profile edit…
_defaults, autowire & autoconfigure
…it for you.
This works for many - but not all tags. It does not work for doctrine.event_subscriber
or form.type_extension:
Because it has an extended_type tag option... which the system can't guess for you.
When you're developing a feature…
Autowiring Controller Arguments
…container directly! We know the fix: type-hint a new argument with
LoginFormAuthenticator $authenticator and use that below:
Almost done! The app.doctrine.hash_password_listener service isn't being used anywhere,
and neither is app.form.help_form_extension.
And... that's it! At…
Autowiring Deprecations
…we can create an alias.
Copy the Doctrine\ORM\EntityManager class name. Then, find your editor and open
up services.yml. Add the alias: Doctrine\ORM\EntityManager aliased to @,
and then copy the target service id: @doctrine.orm.default_entity_manager:
We have full control…
DQL Filtering & Sorting
…a lot of species. Start by adding a list key and a new,
awe-inspiring option: dql_filter. For the value, pretend that you're building a
query in Doctrine. So, entity.speciedCount >= 50000:
The alias will always be entity.
Try it! Ten down…
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…
Collection Delete & allow_delete
…submit, the final array will have
three GenusScientist objects in it, instead of four.
Ready? Submit!
Hmm, no error... but it still doesn't work. Why not? Hint: we already know the
answer... and it relates to Doctrine's inverse relationships. Let's fix it.
Adding to a Collection: Cascade Persist
…operations for GenusScientist.
Umm, what? Here's what's going on: when we persist the Genus, Doctrine sees the
new GenusScientist on the genusScientists array... and notices that we have
not called persist on it. This error basically says:
Yo! You told me that you…
Webhooks: Preventing Replay Attacks
…the AppBundle/Entity directory, create a new PHP Class called StripeEventLog:
Give it a few properties: $id, $stripeEventId and a $handledAt date field:
Since this project uses Doctrine, I'll add a special use statement on top and
then add some annotations, so that this…
x
1000+