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…
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…
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…
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…
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…
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…
Fixture References & Relating Objects
…for each loop. For the second argument, it passes
the object itself.
This reference system is a little "extra" built into Doctrine's fixtures library.
When you add a "reference" from one fixture class, you can fetch it out in another
class. It's super…
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…
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…
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…
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…
OrderBy & fetch EXTRA_LAZY
… Move over and, refresh! Brilliant! The newest comments are on top.
This actually changed how Doctrine queries for the related comments.
Oh, and I want to mention two quick things about the syntax for annotations.
First... well... the syntax can sometimes be confusing - where to…
Awesome Random Fixtures
…fixed, let's rename the class back from this ridiculous name to
CommentFixture:
To celebrate, move over, refresh and... awesome! 8, random comments. We rock!
Next, let's learn about some tricks to control how Doctrine fetches the
comments for an article, like, their order.
Fetching Relations
…s why we use 'article' => $article. Of course, behind the
scenes, Doctrine will make a query where article_id = the id from this Article.
But, in PHP, we think all about objects.
As nice as this was... there is a much simpler way…
Updating an Entity
…not needed for updates! When you query Doctrine for an object, it already
knows that you want that object to be saved to the database when you call flush().
Doctrine is also smart enough to know that it should update the object, instead
of inserting…
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…
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…
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.…
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…
Test Fixtures & Fast Databases!
…step
will have already been done for you automatically.
We haven't hooked the fixtures into our tests yet, but we can at least try them!
Run:
php bin/console doctrine:fixtures:load
Go check out the browser! Yes! The fixtures gave us 3 enclosures…
x
1000+