Problems with Entities in Messages
…it's not already in the list,
it's added. Then, when we call flush(), Doctrine loops over all of these
objects, looks for any that changed, and creates the appropriate UPDATE or INSERT
queries. It knows whether or not an object should be inserted…
Warmup Command Configuration
…the parent class if we detect that it's a proxy.
After creating the ReflectionClass, write if ($class->implementsInterface(Proxy::class)).
Import the class from Doctrine\Persistence:
All generated proxies have this interface.
Inside, write $class = $class->getParentClass() to get the true entity class:
…
Translation Logic
…and run:
symfony console debug:autowiring ManagerRegistry
Here we go: alias:doctrine - doctrine is the service ID - that's a short one!
Copy that and back in services.php, define it as the fourth argument:
service(), paste:
Perfect!
Doctrine's now available in our ObjectTranslator…
Resetting the Database
…method and run code here that does that. Fortunately,
we don't need to because there are multiple libraries that already solve this
problem. My favorite is Foundry.
Run:
If you watched our Doctrine tutorial, you'll remember Foundry! But you may not
know about…
Upgrading to Symfony 7
…Doctrine uses behind the scenes to load lazy relationships. Recently,
Symfony added its own version of proxies called "ghost objects". They're spooky
cool. Anyway, this proxy package isn't needed anymore. It was originally added
to our app way back when we installed Doctrine…
Builder in Symfony & with a Factory
…character builder needed a service like the EntityManager?
With our new setup, we can make that happen. I don't actually have Doctrine installed
in this project, so instead of the EntityManager, let's require
LoggerInterface $logger... and I'll again add private in front…
make:user
…like
HumanoidEntity. If the "thing" that logs into your site would be better called
a Company or University or Machine, use that name here.
Do you want to store user data in the database via Doctrine?
For us: that's a definite yes... but it…
Bonus: Messenger Monitor Bundle
…but I
don't have migrations configured for this project. So in your terminal, run:
symfony console doctrine:schema:update --force
Before we check out the UI, the bundle has two optional dependencies that
I want to install. First:
composer require knplabs/knp-time-bundle…
Foundry: Fixtures You'll Love
…But first... let's run blindly forward and use this class! In AppFixtures,
delete everything and replace it with VinylMixFactory::createOne().
That's it! Spin over and reload the fixtures with:
symfony console doctrine:fixtures:load
And... it fails! Boo
Expected argument type "DateTime", "null"…
High Priority Transports
…which in our
case is using doctrine. Below, add options, then queue_name set to high.
The name high isn't important - we could use anything. The queue_name option
is specific to the Doctrine transport and ultimately controls the value of a column
in…
Goodbye SensioFrameworkExtraBundle
…the route does have a {slug}
wildcard... but then a $mix argument, which is a Doctrine entity.
Behind the scenes, the param converter would automatically query for a
VinylMix where slug equals the {slug} in the URL. No annotation needed:
it just worked.
The good…
Testing with Multiple Symfony Versions
…In our composer.json file, for the Symfony
packages, this means 6.4.0. For the doctrine-bundle, it would be 2.18.0.
To get these lowest versions installed, over in the terminal, run:
symfony composer update --prefer-lowest
We can see Composer switched…
Customizing the User Class
…roles and first_name columns.
Close this... and run it:
symfony console doctrine:migrations:migrate
Success!
And because the User entity is... just a normal Doctrine entity, we can also
add dummy users to our database using the fixtures system.
Open up src/DataFixtures/AppFixtures…
Verify Email after Registration
…migration:
symfony console make:migration
Go check that out and... awesome. It looks exactly like we expect:
Run it!
symfony console doctrine:migrations:migrate
Beautiful! Let's do one more thing related to the database. Inside of
src/Factory/UserFactory.php, to make life simpler…
Giving Users Passwords
…sure everything looks good:
And... it does! Close it... and run it:
symfony console doctrine:migrations:migrate
Perfect! Now that our users have a new password column in the database, let's
populate that in our fixtures. Open up src/Factory/UserFactory.php and
find…
Hashing Plain Passwords & PasswordCredentials
…try this. Find your terminal and run:
symfony console doctrine:fixtures:load
This will take a bit longer than before because hashing passwords is actually CPU
intensive. But... it works! Check the user table:
symfony console doctrine:query:sql 'SELECT * FROM user'
And... got it…
Query Extension: Auto-Filter a Collection
…we did with the PersistProcessor.
Except... that won't quite work. Why? The CollectionProvider from Doctrine executes
the query and returns the results. So all we would be able to do is take those
results... then hide the ones we don't want. That's..…
Filtering Relation Collection
…internally, Doctrine will first query for all
100... even though we'll only return 10. If you have large collections,
this can be a performance problem. In our Doctrine tutorial, we talk about fixing
this with something called the Criteria system.
But with both approaches…
Updating an Entity
…When you call flush(), Doctrine loops over all of the entity
objects that it "knows about" and "saves" them. And that "save" is smart. If Doctrine
determines that an entity has not been saved yet, it will execute an INSERT query.
But if it's…
Adding new Properties
…anything in my
database? The answer is no. I can prove it by running a helpful command:
symfony console doctrine:schema:update --dump-sql
This is very similar to the make:migration command... but instead of generating
a file with the SQL, it just prints…
x
1000+