Parameters: The Variables of Configuration
…and set that
to a value. Why is this cool? Because you can then reuse that value in any other file
by saying %locale%.
Look under the doctrine key:
Hey, a bunch more, like %database_host% and %database_port%. These are set just
like locale…
Caching in the prod Environment Only
…the dev environment only?
Yes! Copy the doctrine_cache from config.yml and paste it into config_dev.yml.
Next, change type from file_system to array:
The array type is basically a "fake" cache: it won't ever store anything.
Yea, that's it…
Config.yml: Control Center for Services
…twig and doctrine -
corresponds to a bundle that is being configured:
All of this stuff under framework is configuration for the FrameworkBundle:
Everything under twig is used to control the behavior of the services from TwigBundle:
The job of a bundle is to give us…
Dynamic Roles
…ORM\Column annotation and set its type to json_array:
This is really cool because the $roles property will hold an array of roles,
but when we save, Doctrine will automatically json_encode that array and store
it in a single field. When we query…
The Mysterious "User Provider"
…the user provider
is responsible for loading the User from the session and making sure that it's
up to date. In Doctrine, we'll want our's to re-query for a fresh User object to
make sure all the data is still up…
Filters
…put anything in DiscontinuedFilter yet, but most of the work
is done. That ClassMetadata argument is your best friend: this is the Doctrine
object that knows everything about the entity we're querying for. You can
read your annotation mapping config, get details on associations…
Symfony Overlord: The Service Container
…With such a tiny list, it’s easy
to spot the entity manager service: doctrine.orm.entity_manager. This
is the “name” of the service and we use it to get this object out of the
service container.
We’ve been getting the entity manager…
More with ManyToMany: Avoiding Duplicates
…an error!
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
‘4-4’ for key ‘PRIMARY’
Our User is once again added as an attendee to the Event. And when Doctrine saves,
it tries to add a second row to the join table. Not cool!
Adding…
ManyToMany Relationship
…
Like with a ManyToOne, we just need an annotation that tells
Doctrine what type of association this is and what entity it relates to:
// src/Yoda/EventBundle/Entity/Event.php
// ...
/**
@ORM\ManyToMany(targetEntity="Yoda\UserBundle\Entity\User")
/
protected $attendees;
Whenever you have a relationship that…
Adding createdAt and updatedAt Timestampable Fields
…library does this for us. It’s called timestampable,
enable it in config.yml:
# app/config/config.yml
# ...
stof_doctrine_extensions:
Head to the timestampable section of the documentation to see how this works.
We already have the Gedmo annotation, so just copy in the…
After-dinner Mint
…Let’s pretend
that the email field isn’t requied. Remove the NotBlank constraint
from it and set a nullable=true option in the Doctrine metadata. Don’t
worry about updating your schema - this change is just temporary:
// src/Yoda/UserBundle/Entity/User.php
// ...
/**
@ORM\Column…
Controlling Data / Fixtures in a Test
…is stored statically on the parent
test class:
// src/Yoda/UserBundle/Tests/Controller/RegisterControllerTest.php
// ...
public function testRegister()
{
}
Now, grab the Doctrine entity manager by getting the doctrine service
and calling getManager. If you’re not comfortable with what I just did,
don’t worry…
User Serialization
…object hidden in our entity causes serialization to fail.
The entity manager contains a database connection and other information that
just can’t be serialized.
Using the Serializable Interface¶
We need to help Doctrine out. Start by adding the Serializable
interface to the User class…
The UserProvider: Custom Logic to Load Security Users
…logging in now! Ah, a great error:
The Doctrine repository “Yoda\UserBundle\Entity\UserRepository” must implement UserProviderInterface.
The UserProviderInterface¶
Without the property, Doctrine has no idea how to look up the User. Instead
it tries to call a method on our UserRepository. But for that…
Repository Security
…Serializable
Note
Actually, if you don’t set the repositoryClass option, Doctrine
just gives you a base repository class for that entity.
Repositories are where query logic should live. We could create methods like
findActiveUsers, which would query the database for users that have a…
Adding Dynamic Roles to each User
…database, these are stored as a JSON string. Doctrine takes care of converting
back and forth between the array and JSON.
Now, just update the getRoles() method to use this property and add a
setRoles method:
public function getRoles()
{
}
public function setRoles(array $roles)
{
}
Cool…
Saving Users
…class (LoadEvents.php) into the UserBundle, rename it to LoadUsers,
and update the namespaces:
// src/Yoda/UserBundle/DataFixtures/ORM/LoadUsers.php
namespace Yoda\UserBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Yoda\UserBundle\Entity\User;
class LoadUsers implements FixtureInterface
…
Entity Security
…one:
# app/config/security.yml
security:
I’m just inventing the our_database_users part, that can be anything.
But the entity key is a special built-in provider that knows how to load
users via a Doctrine entity.
Yea, and that’s really it…
Contentful: Loading Data from an External CMS
…what we're doing, it's a pretty quick process and would give us a
lot of power on our site.
But one of the beautiful things about Layouts is that our value types can come from
anywhere: a Doctrine Entity, data on an external…
Final Upgrades & Cleanups
…s possible that some recipes
have new versions we can update to. Run:
composer recipes:update
Oh, whoops! I need to commit my changes:
git commit -m 'upgrading doctrine/dbal from 2 to 3'
Perfect! Now run
composer recipes:update
and... cool! There are two…
x
1000+