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…
Custom Validation, Callback and Constraints
…Event entity
that looks like this (with some extras, like getter and setter methods):
// src/KnpU/QADayBundle/Entity/Event.php
namespace KnpU\QADayBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
@ORM\Entity(repositoryClass="KnpU\QADayBundle\Entity\EventRepository")
/
class Event
{
@ORM\Column(name="id", type="integer")
…
Bundle Configuration
…reference
With no arguments. This lists all the loaded bundle's, and their configuration
key (or extension alias). DoctrineBundle, alias doctrine, DoctrineMigrationsBundle,
alias doctrine_migrations. Here's our bundle: ObjectTranslationBundle, alias
object_translation.
Hmm, I'd like to prefix it with symfonycasts, like the Tailwind…
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…
Pagination
…re only using this method right here at the moment, so
rename it to createOrderedByVotesQueryBuilder()... and this will now
return a QueryBuilder - the one from Doctrine ORM. I'll remove the PHP
documentation on top... and the only thing we need to do down here…
Clean URLs with Sluggable
…it does!
It adds slug including a UNIQUE INDEX for slug. And when we run it with
symfony console doctrine:migrations:migrate
it explodes... for the same reason as last time: Not null violation. We're
adding a new slug column to our table that…
Persisting to the Database
…question!
We're only going to focus on creating objects and saving them. Doctrine will
handle the insert queries for us.
To help do this in the simplest way possible, let's make a fake "new Vinyl Mix"
page.
In the src/Controller/ directory, create…
Activating 2FA
…good. No surprises, it adds one column to our table. Run that:
symfony console doctrine:migrations:migrate
Here's the plan. A user will not have two-factor authentication enabled by default.
Instead, they'll activate it by clicking a link. When they do that…
Bundle Dependencies
…because we need the services the translation component
provides, like the LocaleAwareInterface. Following that, run:
symfony composer require doctrine/doctrine-bundle doctrine/orm
Cool, these are the bare minimum dependencies our bundle requires to function.
Now, it's time to add some development dependencies. Run:
…
Performance Optimization 2: Caching
…ObjectTranslator service. Down here in the
translationsFor() method, this findBy() is what's making the queries.
This is what we want to cache!
Doctrine does offer some result caching, but it can get complex isn't
as flexible as the Symfony Cache component. So, let…
Pagination & Column Sorting
…go fix it!
To use Pagerfanta, we'll install three libraries:
composer require babdev/pagerfanta-bundle pagerfanta/doctrine-orm-adapter pagerfanta/twig
Cool beans! Let's get the PHP side working first. Open src/Controller/MainController.php.
The current page will be stored on the…
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…
Simpler Validator for Checking State Change
…last tutorial, we put this above that same $dragonTreasures property,
but inside the User entity. The validator would loop over each DragonTreasure,
use Doctrine's UnitOfWork to get the $originalOwnerId, and then check to see
if the $newOwnerId is different from the original. If it…
The Failure Transport
…an expanded
format where you put the dsn on its own line and then add an options key with
whatever you need below that.
What options can you put here? Each transport type - like doctrine or amqp -
has its own set of options. Right now…
Lean and Mean Dev with PhpStorm (for Symfony)
…lot of the grunt work out of your coding and help
you get things done faster:
Get the life-changing Symfony plugin
Auto-complete namespaces (please stop typing them)!
Tricks for annotations, Doctrine, forms, Twig and more
Refactoring
Live Templates
Fast navigation
Symfony service integration
...…
Collection Magic with Criteria
…
But... there's a problem. If we did this, Doctrine would query for all of the
comments, even though we don't need all of the comments. If your collection is
pretty small, no big deal: querying for a few extra comments is probably fine…
Injecting the Cache Service
…Perfect!
We know this won't work: there is no get() function in this class. And more importantly,
we don't have access to the doctrine_cache.provider.my_markdown_cache service.
How can we get access? Dependency injection.
This time, add a second argument…
Custom Queries
…and-seek down
in the web debug toolbar. Ah, it turns out this is an EntityRepository object -
something from the core of Doctrine. And this class has the helpful methods on
it - like findAll() and findOneBy().
Ok, wouldn't it be sweet if we could…
Query for a List of Genuses
…create a new page that will show off all the genuses. Create
public function listAction() and give it a route path of /genus:
Remember, everything in Doctrine starts with the all-powerful entity manager. Just
like before, get it with $em = $this->getDoctrine()->getManager…
Database Migrations
…But this
bundle primarily gives us something different: a new set of console commands. Run
bin/console with no arguments:
Hiding in the middle is a whole group starting with doctrine:migrations. These
are our new best friend.
Our goal is to find a way…
x
1000+