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…
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…
Adding & Checking the User's Password
…yes!
It looks perfect: ALTER TABLE user ADD password:
Close that, go back to your terminal, and migrate:
php bin/console doctrine:migrations:migrate
Awesome!
Go open the User class. Yep - we now have a password field:
And all the way at the bottom, a…
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…
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…
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…
Fixtures: Seeding Dummy Data!
…one article... but it should
work! Try it: find your terminal and run a new console command:
php bin/console doctrine:fixtures:load
This will ask if you want to continue because - important note! - the command will
empty the database first, and then load fresh…
Sluggable & other Wonderful Behaviors
…Now the recipe installs.
Thanks to this, we now have a shiny new config/packages/stof_doctrine_extensions.yaml
file:
This is where we need to enable the extensions we want. We want sluggable.
We can use the example in the docs as a guide…
x
1000+