Lets Generate a PDF!
…or 0 users. If it doesn't send any emails, try reloading your fixtures
by running:
php bin/console doctrine:fixtures:load
If you are so lucky that it's sending more than 2 emails, you'll get an error
from Mailtrap, because it limits…
Unit Testing our Emails
…a general rule, I like to mock
services but manually instantiate simple "data" objects, like Doctrine entities.
The reason is that these classes don't have dependencies and it's usually
dead-simple to put whatever data you need on them. Basically, it's easier…
Async Emails with Messenger
…email - you need to configure a "queueing"
system where details about that work - called "messages" - will be sent. Messenger
calls these transports. Because we're already using Doctrine, the easiest "queueing"
system is a database table. Uncomment that MESSENGER_TRANSPORT_DSN to use it.
Next…
Processing Encore Files through inline_css()
…EntrypointLookupInterface - we're using a "service subscriber". You can learn
about this in, oddly-enough, our
tutorial about Symfony & Doctrine.
To fetch the service, go down to getSubscribedServices() and add
EntrypointLookupInterface::class.
Back up in getEncoreEntryCssSource(), we can say
$files = $this->container->get(EntrypointLookupInterface…
Auto-set the Owner: Entity Listener
…Don't worry, I'll tell you which one I would use and why.
Our options include an API Platform event listener - a topic we haven't talked
about yet - an API Platform data persister or a Doctrine event listener. The first
two - an API…
Query Extension: Auto-Filter a Collection
…soon as we create a class that implements this interface, every
single time that API Platform makes a Doctrine query for a collection of results,
like a collection of users or a collection of cheese listings, it will call this
method and pass us a…
Login with json_login
…section. This was added in the
last tutorial for us by the make:user command. It tells the security system
that our User lives in Doctrine and it should query for the user via the email
property. If you have a more complex query... or…
Bootstrapping a Test Suite
…use a different database, let's
create that database! Run bin/console doctrine:database:create. But since we
want this command to execute in the test environment, also add --env=test:
Repeat that same thing with the doctrine:schema:create command:
Perfect! Next, Api Platform has…
ACL: Only Owners can PUT a CheeseListing
…manager. Needing the entity manager
is so common, let's create another shortcut for it:
protected function getEntityManager() that will return EntityManagerInterface.
Inside, return self::$container->get('doctrine')->getManager().
Let's use that: $em = $this->getEntityManager(), $em->persist($cheeseListing)
and $em->batman(). Kidding. But…
Conditional Field Setup
…php bin/console doctrine:migrations:migrate
That updates our normal database. But because our test environment uses a
different database, we also need to update that too. Instead of worrying about
migrations on the test database, update it with:
php bin/console doctrine:schema:update…
Resetting the Database Between Tests
…using random data and linking objects
to each other. It was the inspiration behind a fixture class that we created and
used in our Symfony Doctrine
tutorial. The recipe creates a fixtures/ directory for the YAML files and a new
command for loading that data…
Adding the plainPassword Field
…after the JSON is deserialized into a User object, but before it's saved
to the database. One way to do this is via a Doctrine event listener or
entity listener, which are more or less the same thing. That's a fine option...
though…
Bulletproof MongoDB
…through what the PHP driver does when you use it. So we
start with a connection string, which is similar to if use Doctrine with a
relational database there's a DSN. This is very similar. We have specifications
that tell us how to parse…
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…
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…
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…
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…
Querying for Data!
…hold on, that's important! When you query for something, Doctrine
returns objects, not just an associative arrays with data. That's really the whole
point of Doctrine! You need to stop thinking about inserting and selecting rows in
a database. Instead, think about saving…
All about Entity Repositories
…at the top of your Article
class:
This says: when we ask for the Article class's repository, Doctrine should
give us an instance of this ArticleRepository class:
Oh, and the built-in find*() methods actually come from one of the parent classes
of ArticleRepository.…
Updating an Entity with New Fields
…filename:
I love it!
Close that, run back to your terminal, and migrate!
php bin/console doctrine:migrations:migrate
Next, we need to make sure these new fields are populated on our dummy articles.
Open ArticleAdminController.
Oh, but first, remember that, in the Article entity…
x
1000+