Core Listeners & Accessing the "Resource" Objects
…fine... because nobody is the
currently-authenticated user in a console command anyways. But if you did
want a custom field to be populated everywhere - even in a console command - we
have one more solution: a Doctrine postLoad listener. Let's check that out next…
Adding & Populating the Custom Field
…find the original tab and refresh. Oh! It's not an array
of users! It's a Paginator object with a Doctrine Paginator inside! So... this obviously
isn't an array, but the Paginator object is iterable: we can loop over it
like an array…
Setting a Custom Field Via a Listener
…We just set the $isMe field on the authenticated User object. One cool
thing about Doctrine is that if API platform later queries for that same user,
Doctrine will return this exact object in memory, which means that the
$isMe field will be set to…
Custom Item Data Provider
…it extends a less strict
ItemDataProviderInterface. I'm implementing that other crazy interface because
that's what the core Doctrine item data provider uses... and I want to be able
to pass it the same arguments.
Ok: go to the "Code"->"Generate" menu - or…
Completely Custom Field via a Data Provider
…providers that
load one object for the item operations.
Normally, we don't need to think about this system because API Platform has a
built-in Doctrine data provider that handles all of it for us. But if we want to
load some extra data…
Detecting the "Published" State Change
…Methods"
and add the three method that we need:
Before we fill in the code, let's immediately inject the doctrine data
persister so that we can use it to do the actual saving. Add
public function __construct() with DataPersisterInterface $decoratedDataPersister:
I'll hit Alt…
App & Test Setup
…who can publish an item under different conditions.
Then we'll do everything custom: add completely custom fields, completely custom API
resources that aren't backed by Doctrine, custom filters and we'll even dive deep
into API Platform's input and output DTO system…
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…
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…
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…
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…
Using a Base Email Template
…data is fresh... with recent article created dates. Run:
php bin/console doctrine:fixtures:load
This should add enough users and articles that about 1-2 authors will be subscribed
to the newsletter and have recent articles. Try that command:
Ha! It didn't explode…
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…
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…
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…
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…
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…
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…
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…
Creating Embedded Objects
…>persist() on that new CheeseListing, which
is why Doctrine isn't sure what to do when trying to save the User.
If this were a traditional Symfony app where I'm personally writing the code to
create and save these objects, I'd probably just…
x
1000+