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…
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…
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…
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…
A bit of Security Cleanup
…with our User
class so we get auto-completion.
Back down in our method, we can say
$this->getUser()->getArticles()->isEmpty(), which is a method on Doctrine's
Collection object. So, if we don't have ROLE_ADMIN_ARTICLE and we are not the
author…
Clear that Location Name Data
…location is star, specificLocationName
is Rigel and id is 28. Let's go verify this in the database: find your terminal
and run:
php bin/console doctrine:query:sql 'SELECT * FROM article WHERE id = 28'
Yep! All the data looks like we expected! But…
Agree to Terms Checkbox Field
…bit of glue code in your controller that does
whatever you need.
Later, we'll discuss a third option: creating a custom model class for your
form.
Before we move on, try to reload the fixtures:
php bin/console doctrine:fixtures:load
It... explodes! Duh…
Tweak your Form based on the Underlying Data
…with some default data. The form system would update that Article
object, but Doctrine would still be smart enough to insert a new row when we save.
Anyways, that's why I'm checking not only that the Article is an object, but
that it…
Autocomplete Endpoint & Serialization Group
…that URL, open a new tab paste and.... boo! A circular reference has
been detected. This is a common problem with the serializer and Doctrine objects.
Check it out: open the User class. By default, the serializer will serialize
every property... or more accurately, every…
Agree to Terms Database Field
…directory, open that file and... yep! It adds
the one field. Run it with:
php bin/console doctrine:migrations:migrate
Oh no! Things are not happy. We have existing users in the database!
When we suddenly create a new field that is NOT NULL, MySQL…
The Edit Form
…Go to /article/1/edit. Dang - I don't have an article with id
Let's go find a real id. In your terminal, run:
php bin/console doctrine:query:sql 'SELECT * FROM article'
Perfect! Let's us id 26. Hello, completely pre-filled form…
HTML5 & "Sanity" Validation
…can be kind of annoying & surprising. However, when you bind your
form to an entity class, the form field guessing system uses the nullable Doctrine
option to choose the correct required option value for you. In fact, if we look
at the textarea field... yep…
EntityType: Drop-downs from the Database
…Anyways,
we'll see in a minute how we can control what's displayed here.
So... great first step! It looks like the form guessing system correctly sees the
Doctrine relation to the User entity and configured the EntityType for us. Go
team!
But now…
Bind Your Form to a Class
…bind your form to a class, a
special system - called the "form type guessing" system - tries to guess the proper
"type" for each field. It notices that the $content property on Article is a longer
text Doctrine type. And so, it basically says:
Hey peeps…
Impersonation (switch_user)
…is also used by a few other features to load the user,
like remember_me and switch_user. If you're using the Doctrine user provider
like we are, then this property key determines which field will be used for all
of this:
If you…
Author ManyToOne Relation to User
…use
$this->getRandomReference('main_users') to get a random User object from
that group:
At the top of the class, I can remove this old static property.
Try it! Move over and run:
php bin/console doctrine:fixtures:load
It works! But... only by chance…
x
1000+