Provider: Transforming Entities to DTOs
Let's keep track of the goal. When we first used stateOptions, it triggered the
core Doctrine collection provider to be used. That's great... except that it
returns User entities, meaning that those became the central objects
for the UserApi endpoints. That causes a…
Entities, DTO's & The "Central" Object
…too good to be true. It gives us all the
flexibility, in theory, of a custom class, while reusing all the core Doctrine
provider and processor logic. But hold your horses because there are two, albeit
fixable, snags.
Most importantly, we're not allowed to…
Refactoring `ObjectTranslator`
…a constructor for this class... and open it up to give us some space.
Now copy the Doctrine-related properties from ObjectTranslator's
constructor ($translationClass and $doctrine) and paste them into our
new constructor:
This paves the way for our next method, public function idFor()…
Filters: Automatically Modify Queries
…But what if we want to apply some criteria like this globally to
every query to a table? Like, telling Doctrine that whenever we query for fortune
cookies, we want to add a WHERE discontinued = false to that query.
That sounds crazy. And yet…
Persisting the More Complex Many-to-Many Relationship
We refactored our many-to-many relationship to include a join entity
called StarshipDroid, instead of relying on Doctrine to create
the join table for us. Reload our fixtures, but hold on to your
hats:
Error!
Undefined property: App\Entity\Starship::$droids
This error is…
Reusable Entity->Dto Provider & Processor
Our UserAPI is now a fully functional API resource class! We've got our
EntityToDtoStateProvider, which calls the core state provider from Doctrine, and
that gives us all the good stuff, like querying, filtering, and pagination. Then,
down here, we leverage the MicroMapper system to…
File Uploads & Data Fixtures
Open up src/DataFixtures/ArticleFixtures.php. Here's how this works: this function
creates 10 articles whenever we run bin/console doctrine:fixtures:load. It's a cool
helper we created in our Symfony series. But, the setImageFilename() stuff
is now a problem. We know…
Making Questions owned by Users
Our site has users and these questions are created by those users. So in the
database, each Question needs to be related to the User that created it
via a Doctrine relationship. Right now, if you open src/Entity/Question.php,
that is not the…
Querying the Database
…text file.
We're going to stop using this MixRepository and instead load these $mixes
from the database.
Ok: to save objects, we leveraged the EntityManagerInterface service, which is
the most important service by far in Doctrine. Whelp, this service can also
query for objects…
Migrations
…run:
symfony console doctrine:migrations:migrate
Say y to confirm and... beautiful! It tells us that it's Migrating up to
that specific version. It seems... like that worked! To make sure, you can
try another bin/console command: symfony console doctrine:query:sql
with…
Timestampable & Failed Migrations
…Yup! It looks
good: it adds the two columns.
Back at the terminal, run it with:
symfony console doctrine:migrations:migrate
And... yikes!
Invalid datetime format 0000 for column created_at at row one.
The problem is that our database already has rows in the…
DQL & The Query Builder
…back is a custom class. Well, technically you don't have to have a custom
repository class - and if you don't, Doctrine will just give you an instance of
EntityRepository. But in practice, I always have custom repository classes.
Anyways, when we ask for…
Persisting to the Database
…How can I insert a new row in the question table?
We need to think:
Let's create a Question object, populate it with data and then ask Doctrine
to save it.
To play with all of this, let's add a new, sort of…
Migrations
…the database. But... that table does not exist yet.
How can we create it?
Well, because Doctrine has all of this configuration about the entity, like
the fields and field types, it should - in theory - be able to create the table
for us. And... it…
Fixing our Deprecations: Form, Controller & Mailer
…the current list for the homepage.
There are technically 12 deprecations. But remember, we can ignore all the
ones from doctrine/persistence because they're not related to Symfony.
With that in mind... if you look closely, there are really only two real
deprecations left..…
Upgrading to Symfony 5.0
We've done it! We fixed all the deprecations in our app... except for the
doctrine/persistence stuff, which we don't need to worry about because we're
not upgrading that library. That means... we are ready for Symfony5!
How... do we actually upgrade…
JOINs!
…do! I mean, a query isn't truly interesting unless you're
joining across tables to do some query Kung fu. Doctrine makes JOINs really
easy - it's one of my favorite features! Heck, they're so easy that I think
it confuses people. Let…
Value Loader + Preview Template
…it chose to do an odd thing: explode! The Ajax call that failed says:
Value loader for doctrine_recipe value type does not exist.
To review: we have a custom value type called doctrine_recipe, which we created
so that we could add grids and…
The Server & New IsGranted
… First, in .env, change the database name to symfony3_tutorial,
or whatever the database name was called when you first setup the project. Now
when we run doctrine:migrations:status... yes! We have a full database!
Let's start the built-in web server:
./bin…
Migrating Services & Security
…goal: to move our code - which mostly lives in config/ - into
the new directory structure.
The next section is doctrine... and there's nothing special here: this is the
default config from Symfony 3. Compare this with config/packages/doctrine.yaml.
If you look closely…
x
1000+