Custom Filter for Custom Resources
…had before:
The only difference is that the apply() method has different arguments
than filterProperty()... which makes sense because that method was all
about querying via Doctrine.
Before we start filling this in, go to src/Entity/DailyStats.php so we can use
the filter…
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…
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…
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…
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…
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…
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…
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…
Setup: For Dependent Select Fields
…location will be
optional. Now run:
php bin/console make:migration
and open the Migrations/ directory to check out that new file.
No surprises here, so let's go back and run it:
php bin/console doctrine:migrations:migrate
Perfect!
Next, open the ArticleFormType so…
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…
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…
All about the User class
…And actually,
it's not that important. I'll tell you what it does later.
But before we get there, forget about security and remember that our User class
is a Doctrine entity. Let's add another field to it, generate a migration & add
some…
Customizing the User Entity
…But! A word of warning. Check out
the roles field on top:
It's an array and its Doctrine type is json. This is really cool. Newer
databases - like PostgreSQL and MySQL 5.7 - have a native "JSON" column type
that allows you to store…
Dynamic Roles
…also need to add ROLE_USER: the getRoles() method will
make sure that's returned even if it's not stored in the database:
Let's reload those fixtures!
php bin/console doctrine:fixtures:load
When that finishes, move over and go back to /login…
ApiToken Entity
…at.
And, it creates the foreign key.
That looks perfect. Move back and run it!
php bin/console doctrine:migrations:migrate
So, the question of how these ApiTokens will be created is not something we're
going to answer. As we talked about, it's…
API Token Authenticator
…in a
minute.
But first, move over to your terminal: we need to find a valid API key! Run:
php bin/console doctrine:query:sql 'SELECT * FROM api_token'
Copy one of these long strings, move back to Postman and paste! To see what this…
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…
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…
Inserting new Objects
…on a slow query.
I still can't believe it's working - things never work on the first try! To
triple-check it, head to the terminal. To run a raw SQL query, use:
There they are. So inserting objects with Doctrine... pretty darn easy.
Process that Form!
…to use that associative array to create a new Genus
object, populate it with the data, and save it via Doctrine.
But, it would be awesomesauce if the form framework could do our job for us. I mean,
use the data to automatically create the…
x
1000+