Embedded Write
… But the error is fascinating!
A new entity was found through the relationship CheeseListing.owner that was
not configured to cascade persist operations for entity User.
If you've been around Doctrine for awhile, you might recognize this strange error.
Ignoring API Platform for a…
Relating Resources
…that up... just to make sure it doesn't contain anything extra.
Looks good - altering the table and setting up the foreign key. Execute that:
php bin/console doctrine:migrations:migrate
Oh no! It exploded!
Cannot add or update a child row, a foreign key…
PropertyFilter: Sparse Fieldsets
…From a high-level, a filter is basically a way for you to modify the
Doctrine query that's made when fetching a collection.
There's one more filter I want to talk about... and it's a bit special: instead
of returning less results…
User API Resource
…auto-increment id, one option is to use a UUID.
We're not going to use them in this tutorial, but using a UUID as your identifier
is something that's supported by Doctrine and API Platform. UUIDs work with
any database, but they are…
Validation
…earlier, if you're using
Symfony 4.3, you might already see a validation error instead of a database error
because of a new feature where validation rules are automatically added by reading
the Doctrine database rules.
But, whether you're seeing a 500 error…
Filtering & Searching
…at the top of your class, add use BooleanFilter. But... careful...
most filters support Doctrine ORM and Doctrine with MongoDB. Make sure to choose
the class for the ORM.
Ok, now move over and refresh again.
We're back to life! Click "Try it out…
API Debugging with the Profiler
…the
goodies that you expect - cache, performance, security, Doctrine, etc.
In addition to the little web debug toolbar AJAX tracker we just saw, there are
a few other ways to find the profiler for a specific API request. First,
every response has an x-debug…
Our First ApiResource
…database exists:
php bin/console doctrine:database:create
And now run make:migration:
Let's go check that out to make sure there aren't any surprises:
CREATE TABLE cheese_listing...
Yea! Looks good! Close that and run:
php bin/console doctrine:migrations:migrate
Brilliant…
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…
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+