Smarter Entity Methods
…Symfony binary can inject the environment variables. When this finishes,
I like to double check the migration to make sure it doesn't contain any surprises.
This looks perfect. Execute it with:
symfony console doctrine:migrations:migrate
Beautiful!
But... this did break one little thing…
"5 Minutes Ago" Strings
…That's not going to work!".
And... you're right:
Object of class DateTime could not be converted to string
We know that when we have a datetime type in Doctrine, it's stored in PHP
as a DateTime object. That's nice because DateTime…
How does the Controller Access the Container?
…And
AbstractController implements a special interface called
ServiceSubscriberInterface. This is actually something we talk about in
one of our
Doctrine tutorials.
When you implement ServiceSubscriberInterface, it forces you to have a method
called getSubscribedServices() where you return an array that says which
services you need…
Events, Events & Events!
…see the Twig templates being executed below it, and even little Doctrine
queries happening along the way.
The biggest thing I want you to notice is that most of the other lines - both
before and after the controller - contain the word Listener, or sometimes
Subscriber…
Migrate Password Hashing
…Got it!
It's also kinda fun to see how this looks in the database. Find your terminal
and run:
php bin/console doctrine:query:sql 'SELECT email, password FROM user'
Interesting: every hashed password starts with the same $2y thing. That's no
accident…
Wall Time, Exclusive Time & Other Wonders
…whatever that is. If you use Doctrine,
you might know what this is - but let's pretend we have no idea.
Before we dive further into the root cause behind this slow function, the
other way to order the calls is by the number of…
Sending Handlers to Different Transports: from_transport
…for the options you can pass here... though you can always
check MessageSubscriberInterface: it talks about what's available.
Next, let's up our queueing game by changing from the Doctrine transport to
RabbitMQ - also commonly referred to as AMQP. It's buckets of fun!
Passing Entity Ids inside of Messages
…Enter -> Initialize Fields
to create that property and set it.
Back in the method, we can say
$imagePost = $this->imagePostRepository->find($imagePostId).
That's it! And this fixes our Doctrine problem! Now that we're querying for the
entity, when we call flush()…
Reordering the Files
…the usual:
php bin/console make:migration
and go to the src/Migrations directory so we can make sure it doesn't contain
any surprises. Looks perfect! Close that and run:
php bin/console doctrine:migrations:migrate
Ok, the database is ready! For the frontend…
Deleting Files
…problem:
the file might get deleted, but then the row stays because of a temporary connection
error to the database.
If you're worried about this, use a Doctrine transaction to wrap all of this
logic. If the file was successfully deleted, commit the transaction…
Setup for Uploading Private Article References
…yep!
CREATE TABLE article_reference
... with a foreign key back to article. Run that with:
php bin/console doctrine:migrations:migrate
Before we get back to work, open the Article entity. The command did create
the $articleReferences property that allows us to say
$article->getArticleReferences()…
Dummies
…object - an object that does work, but doesn't hold much data, like
DinosaurFactory, Doctrine's EntityManager or a class that sends emails, mock
it. That's because these are usually difficult to instantiate and often have side
effects, like talking to the database or…
Buzzwords! Specification & Examples
…bin/phpspec describe App/Entity/Dinosaur
I could have chosen any namespace starting with App - that's up to how you want
to organize your code. But, if you're used to Doctrine in Symfony, this will feel
familiar.
Ok! One new file: DinosaurSpec.php…
ManyToMany Relationship
…thing is that
it will hold the array of User objects that are linked to this Genus:
Above, add the annotation: @ORM\ManyToMany with targetEntity="User".
Finally, whenever you have a Doctrine relationship where your property is an array
of items, so, ManyToMany and OneToMany…
Tagging Scenarios in order to Load Fixtures
…with no var_dump(). Perfect!
One way to execute the fixture is by running the doctrine:fixtures:load command.
I use a different method that gives me more control. Add $loader = new ContainerAwareLoader()
and pass it the container:
Now, point to the exact fixtures…
When *I* do Something: Handling the Current User
…object which will be the author for those products:
Now, add an if statement that says, if $author is passed then, $product->setAuthor():
I already have that relationship setup with in Doctrine. Great!
In thereAreProducts(), change the body of this function to $this->createProducts($count)…
The SymfonyExtension & Clearing Data Between Scenarios
…use hooks.
Create a new public function clearData():
Clearing data now is pretty easy, since we have access to the entity manager via
self::container->get('doctrine')->getManager();:
Now we can issue DELETE queries on the two entities that we care about so far:
product…
Controlling the Database
…so we can say $user = new User().
Then set the username and the "plainPassword":
I have a Doctrine listener already setup that will encode the password automatically.
Which is good: it's well-known that raptors can smell un-encoded passwords...
In this app…
QuestionTag Fixtures & DateTimeImmutable with Faker
…We can see this. Open up the fixtures class and say
QuestionTagFactory::createMany(10). I'm going to put a return statement here
because some of the code below is currently broken.
Let's try this:
symfony console doctrine:fixtures:load
And... it fails! But.…
Saving Items in a ManyToMany Relation
… Let's try this thing! Reload the fixtures:
symfony console doctrine:fixtures:load
And... no errors! Check the database:
symfony console doctrine:query:sql 'SELECT * FROM tag'
No surprise: we have two tags in this table. Now
SELECT * FROM question_tag - the join table.
And…
x
1000+