Collection Criteria for Custom Relation Queries
…
Fortunately, no! These Doctrine Collection objects have a few tricks up their sleeves,
including a special "criteria" system for just this situation. It allows us to
describe how we want to filter the answers and then it uses that when it queries!
Remove the filter…
"5 Minutes Ago" Strings
…That's not going to work!".
And... you're right:
Object of class DateTime could not be converted to string
We know that when we have a datetime type in Doctrine, it's stored in PHP
as a DateTime object. That's nice because DateTime…
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…
Controlling the Database
…so we can say $user = new User().
Then set the username and the "plainPassword":
I have a Doctrine listener already setup that will encode the password automatically.
Which is good: it's well-known that raptors can smell un-encoded passwords...
In this app…
When *I* do Something: Handling the Current User
…object which will be the author for those products:
Now, add an if statement that says, if $author is passed then, $product->setAuthor():
I already have that relationship setup with in Doctrine. Great!
In thereAreProducts(), change the body of this function to $this->createProducts($count)…
Tagging Scenarios in order to Load Fixtures
…with no var_dump(). Perfect!
One way to execute the fixture is by running the doctrine:fixtures:load command.
I use a different method that gives me more control. Add $loader = new ContainerAwareLoader()
and pass it the container:
Now, point to the exact fixtures…
Writable Collection via the PropertyAccessor
…into why we can't have a setter, but it relates to the
fact that we need to set the owning side of the Doctrine relationship. We talk
about this in our Doctrine relations tutorial.
The point is, if we were able to just call…
Leveraging the Core Processor
…on? We map the UserApi to a new User object and save the new
User... which causes Doctrine to assign the new id to that entity object. But
we never take that new id and put it back onto our UserApi.
To fix this, after…
Entity -> DTO Item State Provider
…the querying work
manually. Instead, we'll... "delegate" it the core Doctrine item provider. Add
a second argument... we can just copy the first... type-hinted with ItemProvider
(the one from Doctrine ORM), and called $itemProvider.
I like it! Back below, let it do the…
Creating Embedded Objects
…>persist() on that new CheeseListing, which
is why Doctrine isn't sure what to do when trying to save the User.
If this were a traditional Symfony app where I'm personally writing the code to
create and save these objects, I'd probably just…
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…
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…
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…
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…
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…
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…
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…
Improving Javascript Event Security
…user.
Head over to your terminal and run:
bin/console doctrine:query:sql "SELECT * FROM user"
We have the lsCustomerId set, so let's reset it to NULL with:
bin/console doctrine:query:sql "UPDATE user SET lsCustomerId=NULL WHERE id=1"
Let's test it out…
Handling ManyToMany in Foundry
…maybe you see it... but let's try it anyways.
Spin over and reload the fixtures:
symfony console doctrine:fixtures:load
Awesome. And now check the database. I'll first say:
symfony console doctrine:query:sql 'SELECT * FROM tag'
Yep! We do have 100 tags…
When a Migration Falls Apart
…ve fixed the migration, how can we run it again? Let's
try the obvious:
symfony console doctrine:migrations:migrate
It fails again! But for a different reason: dropping the foreign key failed
because that's already gone.
Here's the problem. When we first…
x
1000+