The Two Sides of a Relation: Owning vs Inverse
Fun fact for your upcoming Doctrine taco party: Every
relationship can be viewed from two different sides. Take
Starship: it has multiple parts, making it a
one-to-many relationship from the Starship perspective. But, flip the
telescope around and look from the StarshipPart end…
Many-to-Many but with Extra Data
ManyToMany relations are the one place in Doctrine where we have a
table in our database - starship_droid - but no corresponding
entity in our app.
But there's a catch: we can't add extra columns to that join table. Like,
what if we wanted…
Pagination with Pagerfanta
…to create a Pagerfanta object... you can see how in the "Rendering
Pagerfantas" section. This looks simple enough: create a new Pagerfanta and,
because we're using Doctrine, create a new QueryAdapter and pass in our
$queryBuilder.
Cool: $pagerfanta = new Pagerfanta()... and new QueryAdapter().…
ManyToMany... with Extra Fields on the Join Table?
…the join table without needing to create an
entity for it.
So now that we understand that, from the database's perspective not much is
changing, let's run the migration to make those tweaks:
symfony console doctrine:migrations:migrate
And... it fails! Rut roo…
Adding Property Types to Entities
…t explode.
Ok, so adding property types is nice: it makes our code cleaner and tighter. But,
there's another big advantage: we don't need the targetEntity anymore! Doctrine
is now able to figure that out for us. So delete this... and celebrate!
Then…
Recipe Upgrades: Part 2!
Run:
composer recipes:update
Next up is doctrine-extensions-bundle. This one... when we look... just modified
a comment! Easy!. So commit that... and then move onto debug-bundle.
I'll clear the screen and run that. This made two changes. Run:
git status
The…
Custom Paginator
When an API resource is a Doctrine entity like CheeseListing, life is good!
We get tons of things for free, like pagination, access to a bunch of pre-built
filters and one time I swear I got free pizza.
But... these filters will not work…
Twig Block Tricks
This tutorial is all about Doctrine relations... plus a few other Easter eggs
along the way. And one big topic we need to talk about is how to create queries
that join across these relationships. To do that properly, we're going to build
a…
Custom Queries
…and we'll continue writing a few more here and
there. But if you have something particularly challenging, check out our
Go Pro with Doctrine Queries tutorial. That tutorial uses
Symfony 3, but the query logic is exactly the same as in Symfony 4.
Next…
Query across a JOIN (and Love it)
What about a JOIN query with Doctrine? Well, they're really cool.
Here's our last challenge. Go to /genus. Right now, this list is ordered by the
speciesCount property. Instead, I want to order by which genus has the most recent
note - a column…
Database Config and Automatic Table Creation
…meaning all services are
configured: app/config/config.yml. Scroll down to the doctrine key:
Ah, this is what tells Doctrine all about your database connection.
But, the information is not hardcoded here - these are references to parameters
that are defined in parameters.yml:
Update…
Database Setup & Docker
…problem! Comment out this line and uncomment the
sqlite one. SQLite doesn't require a server: it's
just a file on your filesystem. Because Doctrine abstracts the database layer,
for the most part, the code we write will work with any database type. Cool…
Class Table Inheritance
…ships!
Now, let's peek into the database to see what we're dealing with. Back in the terminal,
run:
symfony console doctrine:query:sql 'select * from starship'
As you can see, this only contains the columns for the Starship entity, not
Freighter and Scout…
Forever Scroll with Turbo Frames
…team! Congrats on finishing the Doctrine course! I hope you're feeling
powerful. You should be! The only major missing part of Doctrine now is Doctrine
Relations: being able to associate one entity to another through relationships, like
many-to-one and many-to-many…
Upgrading/Migrating from StofDoctrineExtensions
…5 support. We're saved!
So let's switch to use this fork. Copy the stof package name again, and
remove it:
composer remove stof/doctrine-extensions-bundle
Composer removes it and then... explodes! That's ok: it was removed... but because
our app, needs…
Fixing N+1 With a Join?
We made a huge leap forward by telling Doctrine to make COUNT queries to
count the comments for each BigFootSighting... instead of querying for all the
comments just to count them. That's a big win.
Could we go further... and make a smarter query…
Extending our Bundle's Entity
Our bundle has an abstract Translation mapped superclass and is
registered with Doctrine. Time to create the real Translation entity
in our app.
At your terminal, run:
symfony console make:entity
For the class name, use Translation. All properties are defined in
the abstract Translation…
Foundry: Always Pass a Factory Instance to a Relation
I love Foundry. But using Foundry with Doctrine relationships is probably the
hardest part of this library. So let's push a bit further. Pretend that, in this
situation, we want to override the question value. Right now it grabs any random
Question from the…
The 4 (2?) Possible Relation Types
Officially, there are four types of relations in Doctrine: ManyToOne,
OneToMany, OneToOne and ManyToMany. But... that's kind of a lie! In reality,
there are only two types.
Let me explain. We already know that a ManyToOne relationship and a OneToMany
relationship are really just…
SELECT the SUM (or COUNT)
In every query so far, Doctrine gives us objects. That's its default mode,
but we can also easily use it to select specific fields.
On our category page, you can see how many of each fortune has been printed
over time. At the top…
x
1000+