Inserting Data via Fixtures
…paste some code from the tutorial/ directory.
We now have three ship objects, but nothing has been saved - or persisted to the
database yet. But interesting, Doctrine passes us an ObjectManager.
This is the heart of Doctrine. We'll use it to save, fetch, update…
Symfony Forms: The Basics
…this course, you’ll learn how to:
Install and configure the Symfony Form component
Build form types with Maker and map them to Doctrine entities
Understand the difference between Form and FormView
Render forms and fields using Twig’s helper functions
Use form_start(), form…
Pagination
…s a generic pagination library
but has great Doctrine integration! Add the two required packages:
composer require babdev/pagerfanta-bundle pagerfanta/doctrine-orm-adapter
Scroll up to see what this installed. pagerfanta/doctrine-orm-adapter is the glue between
Pagerfanta and Doctrine.
On our homepage…
Upgrading to Symfony 8
…edges that pop up along the way. And along the journey, we’ll tackle one of the biggest ecosystem shifts: upgrading Doctrine to take advantage of PHP 8.4’s native lazy proxies - goodbye generated proxy classes, hello simpler, faster magic.
Whether you’re maintaining…
Tracking & Fixing Deprecations
…s coming from Doctrine, something about raw field value
access... But look at this! A link to a pull request right in the message! Very
helpful, I love it! Copy the link and paste it in your browser...
This is the Doctrine PR that modified…
Many To One: The King of Relationships
…null.
But that's not allowed, thanks to the nullable: false. Clear those 50 rows with:
symfony console doctrine:query:sql "DELETE FROM starship_part"
And run the migration again:
So, how do we go about linking a StarshipPart object with its Starship?
Buckle up…
Ship Upgrades: Updating an Entity
…on our homepage only lists incomplete starships, so we need to find a
completed one. In your terminal, run:
symfony console doctrine:query:sql 'SELECT slug, status FROM starship'
lunar-marauder-1 is a completed ship. Copy the slug, and back in the app,
visit…
Setting Relations in Foundry
…our good
friend: Foundry. Remove the manual code, then anywhere, say:
StarshipPartFactory::createMany(100):
And try the fixtures:
symfony console doctrine:fixtures:load
Uh-oh!
starship_id cannot be null in starship_part.
This traces all the way back to StarshipPartFactory, down in
the defaults…
Many To Many with Foundry
…not
technically required - good luck cleaning the bathroom without them! -
I like to keep them out of defaults() and set them where we're using
StarshipFactory.
Next, we'll learn how to JOIN across ManyToMany relationships. Once again,
Doctrine handles the heavy lifting for us.
JOINs
…a database perspective, in order to update our
WHERE clause to include WHERE fortune_cookie.fortune = :searchTerm, we first
need to JOIN to the fortune_cookie table.
And that is what we're going to do in Doctrine... except with a twist. Instead
of…
EXTRA_LAZY Relationships
…to count them... isn't the greatest thing for efficiency.
If you find yourself in this situation, you can tell Doctrine to be clever with
how it loads the relation. Go into the Category entity and find the OneToMany
relationship for $fortuneCookies. At the end…
JOINs and addSelect Reduce Queries
…fortuneCookies|length.
In PHP land, we're calling the getFortuneCookies() method on Category. But until
now, Doctrine has not yet queried for the FortuneCookie data for this Category.
However, as soon as we access the $this->fortuneCookies property, it magically
makes that query, basically saying:
Give…
Reusing Queries in the Query Builder
…
Anywhere inside here, add a new private function called
addFortuneCookieJoinAndSelect(). This will accept a QueryBuilder object (make
sure you get the one from Doctrine\ORM - the "Object Relational Mapper"), and let's
call it $qb. This will also return a QueryBuilder.
The next step is…
Raw SQL Queries
…s as boring as SQL queries can
get. I used fortune_cookie for the table name because I know that, by default,
Doctrine underscores my entities to make table names.
Now that we have the query string, we need to create a Statement with
$stmt…
SELECTing into a New DTO Object
… I like to work with
objects whenever possible. Fortunately, Doctrine gives us a simple way to improve
this situation: we query for the data we want... but tell it to give us an object.
First, we need to create a new class that will hold…
Translated Object Wrapper
…ObjectTranslator, we have
this todo waiting for us.
There's a couple ways we can approach this. One way is to directly modify
the properties on the object. But since this is a Doctrine entity, it'll
be marked as modified. If you unintentionally flush…
Decorating the CollectionProvider
…the failed response. Hmm:
More than one result was found for query, although one row or none was expected.
If you view the page source, this is coming from Doctrine... and eventually the
core ItemProvider that we're calling. Back on the docs, the GetCollection…
State Processors: Hashing the User Password
…platform: we need some way to run
code after our data is deserialized onto the User object, but before it's saved.
In our tutorial about API platform 2, we used a Doctrine listener for this,
which would still work. Though, it does some negatives…
Factory Data Seeding
…and look at src/Factory/LockDownFactory.php. I'm not going to talk
too much about these factory classes: we already cover them in our Doctrine tutorial.
But this class will make it easy to create LockDown objects, even setting
createdAt to a random DateTime…
Extending with Events
…it says "Updated By", "Null". Our goal is to set that field automatically
when a question is updated.
A great solution for this would be to use the doctrine extensions library and
its "blameable" feature. Then, no matter where this entity is updated - inside
the…
x
1000+