Creating your First ApiResource
…file... just to make
sure it doesn't contain any weird surprises.
Looks good! So spin back over and run this with:
symfony console doctrine:migrations:migrate
Done!
We now have an entity and a database table. But if you go and refresh the
documentation..…
Processing the Submitted Form
…that no ID is set because Doctrine hasn't saved it in the database yet.
I'll quickly add a PHPDoc above the variable to make PhpStorm's autocomplete
happier, and delete the dd() statement:
To save the new part, we need Doctrine's EntityManager…
Built-in Symfony Form Themes
…fancy and use an Order enum
for the second argument. So instead of ASC, write Order, importing the
enum from Doctrine\Common\Collections, then ::Ascending->value:
Super nerdy... But at least we know we didn't make a typo in those, ummm...
3 letters...
Since…
Alien Tech for Fixtures: Foundry & Faker
…other two... and remove the old code:
Bonus! Remove the persist() and flush() calls: Foundry handles that for us!
Let's see what this does! Reload the fixtures:
symfony console doctrine:fixtures:load
Choose yes and... success! Back over, refresh and... it looks the same…
Black Hole: Deleting Entities
…search on and the value is the value to search
for: ['slug' => $slug]:
When using these out-of-the-box find methods, Doctrine automatically
escapes the values, so you don't need to worry about SQL injection attacks.
Adjust this if statement to if (…
Starship Upgrade: Adding Slug and Timestamp Fields
…Open up the new migration file. I love this.
Remember that migrations are generated by comparing the database
to the entity class. Doctrine sees the new fields in the class, does not see the
corresponding columns in the table, generates the SQL to fix that…
Bonus: Custom DQL Function
…on.
It's subtle, but look at the WHERE clause: s0_.id = ?. This s0_ is an
internal SQL table alias Doctrine uses - it's our starship table. I expected
this to use our discriminator column, but it's using the id. I guess when…
High-Tech Controllers: Auto-inject Entities
…SEO-friendly!
Flying through space is dangerous business. Sometimes starships experience
"rapid unscheduled disassemblies"... or in other words, they explode. We need
a way to remove ships from our database that no longer... err... exist. Next, we'll
see how to delete entities with Doctrine.
Quantum Refactor: Rich Entities
…maintainability.
Ok crew, that's it for this Doctrine Fundamentals course! If you're looking to upgrade your
Doctrine skills, search for "Doctrine" on SymfonyCasts
to find more advanced courses. The Doctrine documentation
is a great resource too.
And as always, if you have any…
Swagger UI: Interactive Docs
…of all of the DragonTreasures we
currently have, which is just this one.
So in just a few minutes of work, we have a fully featured API for our Doctrine
entity. That is cool.
Copy the URL to the API endpoint, open a new tab…
Part Entity
…cleaned up the old migrations from the last course.
So this one is all about StarshipPart. Run it with:
symfony console doctrine:migrations:migrate
The table is in the database! But there are two fields that I
like to add to all my entities: createdAt…
Setting the Relation
…not related yet.
Pff, try to load the fixtures anyway. Head over to your terminal and run:
symfony console doctrine:fixtures:load
Rutro:
starship_id cannot be null on the starship_part table.
Why is that column required? In StarshipPart, the starship property has a…
Fetching a Relation's Data
… Instead, pass the Starship object itself. You could pass the id if
you're feeling lazy, but in the spirit of Doctrine, relationships, and
thinking about objects, passing the entire Starship object is the way to go.
Let's debug and see what we've…
The Clever Criteria System
…10 of them
cost more than 50,000. What a waste! Could we ask Doctrine to change the query
so it only grabs the parts related to the starship where the price is greater
than 50,000?
Enter the Criteria object. This thing is mighty…
Initialize the Bundle
…bundles already exist out there that are capable of this, but I wanted to approach
it fresh, with a slightly different perspective.
So that's what our bundle will do: translate Doctrine entities!
Let's get started!
Jump over to the code for this project…
Joining to Avoid the N+1 Trap
…in the template looping over
the parts, when we reference part.starship, Doctrine has a light bulb moment.
It realizes it has the part data, but not the Starship data for this part.
So it queries for it. We end up with one query for…
Ordering a Relation and "fetch" type
…what's happening: we grab the starships, then
as soon as we count ship.parts, Doctrine realizes it doesn't have
that data yet. So it fetches all the parts for each ship one by one and
counts them. This is a common situation: we…
Cascade Persist
…and... we can now see when each droid was assigned.
That's it, friends! We've explored the deepest corners of Doctrine
relationships, even the elusive many-to-many with extra fields. As always,
if you have questions, drop them in the comments below. We…
Many-To-Many Relationship
…database: with a join table.
The real magic of Doctrine is that we only need to think about objects. A Starship
object has many Droid objects, and a Droid object has many Starship objects.
Doctrine handles the tedious details of saving that relationship to the…
Re-adding addDroid(): Hide that Join Entity!
…This one is pretty common in Doctrine,
though not always easy to understand:
A new entity was found through the relationship Starship#starshipDroids
that was not configured to cascade persist for the entity StarshipDroid.
This is a very fancy way of saying we've created…
x
1000+