Property Metadata
… Integers?
Aliens?
API Platform gets metadata about each property from many different places, like
by reading Doctrine metadata, PHPDoc, looking at the return types of getter methods,
looking at the argument type-hint on setters, PHP 7.4 property types and more.
What's really…
Custom Resource Data Provider
…as the identifier? In Doctrine,
it happens automatically thanks to the @ORM\Id annotation:
When you're not in an entity - or if you want to use a field other than
the database id in an entity - you can add @ApiProperty() with identifier=true:
Cool! Let…
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…
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…
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…
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…
x
1000+