Maker Bundle: Let's Generate Some Code!
…same thing. Thanks
to the new bundle, we have a ton of commands that start with make! Commands for
generating a security system, making a controller, generating doctrine entities to
talk to the database, forms, listeners, a registration form.... lots and lots of
stuff!
Let…
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…
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…
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..…
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…
Inheritance with Twig
…if statement... like if ship.type == 'freighter', but that would get
messy really fast. Instead, let's use Twig template inheritance. Twig Template Inheritance
with Doctrine Inheritance, I love it!
First, in the templates/starship directory, create a new teaser
directory. This will be…
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…
Filters: Searching Results
…since we're using the Doctrine ORM -
because we want to allow the user to filter on a boolean field.
The second thing you need top pass is properties set to an array of which fields
or properties you want to use this filter on…
Pagination & Foundry Fixtures
…method. All we need
is: DragonTreasureFactory::createMany(40) to create a healthy trove of 40
treasures:
Let's try this thing! Back at your terminal, run:
symfony console doctrine:fixtures:load
Say "yes" and... it looks like it worked! Back on our API docs, refresh…
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…
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.
Installing Messenger
…validates it, uses another service
to store that file - that's the uploadImage() method, creates a new ImagePost
entity, saves it to the database with Doctrine and then, down here, we have
some code to add Ponka to our photo. That ponkafy() method does the…
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…
Relating Resources
…ll do our standard double-check to make sure the migration isn't trying to
mine bitcoin. Yep, all boring SQL queries here.
Run it with:
symfony console doctrine:migrations:migrate
And it explodes in our face. Rude! But... it shouldn't be too surprising…
Creating a User Entity
…run:
symfony console make:migration
Then... spin over and open that new migration file. No surprises: it creates the
user table:
Close that up and run it with:
symfony console doctrine:migrations:migrate
Sweet! Though, I think our new entity deserves some juicy data fixtures…
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…
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…
Embedded Write
…our data, created a new User object and
then set the username onto it. Doctrine failed because we never told it to persist
the new User object.
Though... that's not the point: the point is that we don't want a new User!
We…
x
1000+