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…
Sharing Data between Fixture Classes
…ORM/LoadUsers.php
// ...
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
class LoadUsers implements FixtureInterface, ContainerAwareInterface, OrderedFixtureInterface
{
}
Head over to LoadEvents and make the same change, except returning 20
so that the class is run second:
// src/Yoda/EventBundle/DataFixtures/ORM/LoadEvents.php
// ...
use Doctrine\Common\DataFixtures\OrderedFixtureInterface…
Introduction
…the hood. We’ll learn what a service is, find out more about the core Symfony
services and create a few of our own. In Doctrine, we’ll create some ManyToOne
and ManyToMany relationships. We’ll also talk about lifecycle callbacks and
event listeners. And…
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…
Introduction
…some of the most difficult areas of Symfony, like security, forms, and
some serious Doctrine topics. Some of this stuff will look pretty tough at
first, but that’s just because we’re taking your Symfony foo up to the next
level. When you finish…
Adding Outside Bundles with Composer
…are dummy data we put into the database.
When we started the project, we downloaded the Symfony Standard edition:
our pre-started project that came with Symfony and other tools like Doctrine.
Unfortunately, it didn't come with any tools for handling fixtures.
But we…
Code Generation FTW!
…we need a CRUD for the Event entity.
Want to use Doctrine to generate a CRUD? Yea, there's a console command for
that doctrine:generate:crud:
php app/console doctrine:generate:crud
This inquisitive command first wants to know which entity we need a…
Inserting and Querying Data
…the error straight
from MySQL saying that the details column can't be null.
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'details' cannot be null
So Doctrine assumes by default that all of your columns should be set to NOT NULL
when creating the table…
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")
…
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…
Foundry: Fixture Model Factories
…we deserve better! Let's use a super fun new library instead. Google for
"Zenstruck Foundry" and find its GitHub Page.
Foundry is all about creating Doctrine entity objects in an easy, repeatable
way. It's perfect for fixtures as well as for functional tests…
Data Fixtures
…random controller like QuestionController, we can install a bundle to
do it properly. Find your terminal and run:
composer require orm-fixtures --dev
This is another flex alias: orm-fixtures installs doctrine/doctrine-fixtures-bundle
When this finishes... it installed a recipe! I committed all…
x
1000+