Starting in Symfony2: Course 1 (2.4+)
…from
the ground-up, touching on and discussing the most fundamental parts
of the application. Specifically, we'll cover:
Installation, Git and Setup
Composer
Routing
Controllers
Introduction to the service container
Twig
Doctrine
Server setup
Code generation
Fixtures & external libraries
And other tips and tricks
Starting in Symfony2: Course 3 (2.4+)
…Plus a ton more!
Highlights:
Doctrine relations, metadata, and the JoinColumn
ManyToMany relationship that's bidirectional, owning versus inverse side and why that matters
Loading fixtures with shared data
Creating shortcuts with your very own base controller!
Doctrine extensions: timestampable, sluggable
Ajax: returning Json from…
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…
Logging in with a Username or Email
…we need to make a little adjustment to our
user repository.
In src/Repository/UserRepository.php, implement another interface:
UserLoaderInterface. This one comes from the Symfony Doctrine bridge, and it has a
single method, so implement it down here: loadUserByIdentifier().
It takes a string $identifier …
Creating a Registration Form
…If one does, we get this message: "There is
already an account with this email." Pretty useful.
We already have this as a Doctrine constraint, but this is a validation constraint.
The difference is the Doctrine constraint is enforced at the database level, so a…
Enabling "Remember Me" Feature
…to log the user in. Here, you can customize this
behavior with your own service, or use the built-in doctrine provider, which stores the token in a
database table. I think the default is good enough for most cases, but it's nice to…
Security Events: Tracking the Last Login
…error-prone: if you modify a mutable date in
place, Doctrine won't see it as a change and your update will never be saved.
Now generate the migration:
symfony console make:migration
Jump back to your editor and find that migration. Here it is…
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…
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…
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..…
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…
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…
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…
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.
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 (…
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…
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…
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…
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…
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…
x
1000+