Server-Side Validation
…It takes two options: the field
that should be unique followed by a message. Add a constraint for both the
username and the email:
// src/Yoda/UserBundle/Entity/User.php
// ...
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
@ORM\Table(name="yoda_user")
@ORM\Entity(repositoryClass=…
Upgrading to 2.2
…replace the core Symfony libraries, and not any
custom lines you may have added. In this case, the doctrine-fixtures-bundle
is custom, so I’ll leave it alone:
"require": {
},
Also, be sure you have the minimum-stability set to alpha. As Stof
points out…
FOSUserBundle
…stored in the database. Let’s create
a new bundle called UserBundle to house the new entity. Instead of using
the doctrine:generate:bundle task, I’ll just create a UserBundle
and make sure there’s a properly-named bundle class inside. Don’t forget…
Content Browser
…when we try the page, we're greeted with an error!
Netgen Content Browser backend for doctrine_recipe value type does not exist.
Yep! We need to implement a class that helps Layouts browse our recipes. That's
called a "content browser".
Adding a content…
Item View Template
…match one piece of config, like
this template, with some other piece of config, like the doctrine_recipe value
type. There are a finite number of these matchers, and we're going to see the most
important ones along the way. So don't worry…
Value Converter
…s an instanceof Recipe:
Second, for getValueType(), return the internal key of our value type:
doctrine_recipe:
Next is getId()... and we're literally going to return our ID with
$object->getId():
We don't have autocomplete on this, but we know this object will…
Adding Lists: Value Type
…The same is true if you're using Ibexa CMS.
So here's our next big goal: to add our Recipe Doctrine entity as a "value
type" in layouts so that we can create lists and grids containing recipes.
Step one to adding a value…
Foundry Tricks
…Faker again! self::faker()->numberBetween(1, 4) and then true
to return this as a string.
Let's take this for a test drive! Find your terminal and reload the fixtures with:
symfony console doctrine:fixtures:load
Go check the homepage and... yea!
Oh, but…
Update Query & Rich vs Anemic Models
…whenever we need to save something in Doctrine, we need the
entity manager.
Add another argument: EntityManagerInterface $entityManager.
Then, below, replace the dd($question) with $entityManager->flush().
Done! Seriously! Doctrine is smart enough to realize that the Question object
already exists in the database and…
Smarter Entity Methods
…Symfony binary can inject the environment variables. When this finishes,
I like to double check the migration to make sure it doesn't contain any surprises.
This looks perfect. Execute it with:
symfony console doctrine:migrations:migrate
Beautiful!
But... this did break one little thing…
Custom Repository Class
…here.
We'll see this idea of custom entity methods later.
At the bottom, there's one more link: question.slug.
Done! Doctrine makes it easy to query for data and Twig makes it easy to render.
Go team! At the browser, refresh and... cool…
docker-compose Env Vars & Symfony
…For example, because this service is called database - we technically could have
called it anything - the Symfony binary is already exposing an environment variable
called DATABASE_URL: the exact environment variable that Doctrine is looking
for.
I'll show you exactly what I mean. First…
Migrate Password Hashing
…Got it!
It's also kinda fun to see how this looks in the database. Find your terminal
and run:
php bin/console doctrine:query:sql 'SELECT email, password FROM user'
Interesting: every hashed password starts with the same $2y thing. That's no
accident…
Validation Auto-Mapping
…How the
heck did that work? The system - validation auto-mapping - automatically adds
sensible validation constraints based off of your Doctrine metadata. The
Doctrine Column annotation has a nullable option and its default value is
nullable=false:
In other words, the title column is required in…
Using the Filesystem
…that: article_image/ and then $newFilename.
I think we're ready! Let's clear out the uploads/ directory again. And then
try our fixtures:
php bin/console doctrine:fixtures:load
Oh! It does not work!
Unused binding $uploadsPath in service UniqueUserValidator.
This is a bad…
Dummies
…object - an object that does work, but doesn't hold much data, like
DinosaurFactory, Doctrine's EntityManager or a class that sends emails, mock
it. That's because these are usually difficult to instantiate and often have side
effects, like talking to the database or…
Let: The Setup Function
…If you're a Doctrine user, this will look familiar. But, no, I'm not recommending
that you actually create or move the EntityManagerInterface into your app like
this. We're adding this interface as a convenient way to "pretend" like Doctrine
exists in our…
Buzzwords! Specification & Examples
…bin/phpspec describe App/Entity/Dinosaur
I could have chosen any namespace starting with App - that's up to how you want
to organize your code. But, if you're used to Doctrine in Symfony, this will feel
familiar.
Ok! One new file: DinosaurSpec.php…
Triaging a Bug Issue
…the user says that he got an error when trying to serialize a Doctrine
QueryBuilder with the web profiler: something about not being able to serialize
a PDO instance.
The web profiler works by collecting a bunch of information about the request
and serializing it…
Linking Symfony deps to your Local Copy
…to use annotations with the
validator, you need to install the annotations library. Stop the web server and
run:
composer require annotations
This installs sensio/framework-extra-bundle... we only technically need
to install doctrine/annotations. But, that's ok. Restart the built-in web…
x
1000+