Using a Test Database
…as the default.
Once we do that, we can setup the test environment to use a different database
name. Open config.yml and copy the doctrine configuration. Paste it
into config_test.yml to override the original. All we really want to
change is dbname…
Symfony: Keep it Simple with @Route and Templates
…
So let’s finish this page. It should be fairly straightforward: we’re going
to use Doctrine to query for all the posts and then pass them into a template:
/**
@Route("/posts")
/
public function indexAction()
{
}
Now, notice that my template name does not have any…
Making Fixtures Awesome with Alice
…files, and the same DoctrineFixturesBundle
that we were talking about before. This is a really nice combination because
it's going to mean that we can still run our normal php app/console doctrine:fixtures:load.
But after that, instead of writing raw PHP code…
Interrupt Symfony with an Event Subscriber
…a lot more interesting.
You can already see where our controller is called, and under the controller
you can see the Twig template and even some Doctrine calls being made.
Before and after that, there are a lot of event listeners - you notice a
lot…
Making an Argument Available to All Controllers
…which works via a listener to kernel.controller,
grabs the id off of the request attributes, queries for a Post object
via Doctrine with that id, and then adds a new request attribute called post
that’s set to that object:
// Summarized version of ParamConverterListener…
Dependency Inject All the Things
…the router
service. So how can we get the router service inside EventReportManager?
You know the secret: dependency injection.
Add a second constructor argument and a second class property:
// src/Yoda/EventBundle/Reporting/EventReportManager.php
// ...
use Doctrine\ORM\EntityManager;
use Symfony\Component\Routing\Router;
class…
Using PHPDoc for Auto-Completion
…better than nothing.
But first, update your test database for our latest schema changes:
php app/console doctrine:schema:update --force --env=test
We need this because we configured our project in episode 2 to use an entirely
different database for testing.
./bin/phpunit -c app/
Using the ManyToMany so Users can Attend an Event
…that we show 1 attending means that the database relationship was
stored correctly. We can prove it by querying for the join table:
php app/console doctrine:query:sql "SELECT * FROM event_user"
Yep, we see one row that links our user to this event.…
Your Very First Service
…we don’t extend anything and we
don’t magically have access to this Doctrine object.
The code inside EventReportManager is dependent on this “doctrine”
object. Well, more specifically, it’s dependent on Doctrine’s entity manager.
The fix for our puzzle is to “inject…
Configuration Loading and Type-Hinting
…mind trick ... I mean
auto-complete the use statement for me. It’s lazy, but it almost always works:
// src/Yoda/EventBundle/Reporting/EventReportManager.php
// ...
use Doctrine\ORM\EntityManager;
class EventReportManager
{
}
If you’re not too comfortable with this, don’t worry. This is optional…
Welcome to Symfony!
…requests, responses and
templating.
Get comfortable with a few extra tools, like Doctrine for dealing with
the database and code generation tools.
And the real reason you are here, to learn enough best practices and tips
to impress your friends at a party.
Ready? Let…
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…
How to handle dynamic Subdomains in Symfony
…differences here are a bit subtle. For example, the baseHost is now
stored in a property and we can get Doctrine’s repository through the $em
property. We’ve also replaced the createNotFoundException call by instantiating
a new NotFoundHttpException instance. The createNotFoundException
method lives in…
Querying Classes
…setParameter(), for the second argument, use
$this->getEntityManager()->getClassMetadata(Scout::class).
This returns a special
metadata object for the Scout class and is what Doctrine needs to properly handle the INSTANCE OF
operator with parameters.
Refresh the browser... and nice, just the scouts again!
Try…
Installing API Platform
…s empty because we don't, ya know, actually have an API
just yet, but this is going to come to life very soon.
Next: Let's create our first Doctrine entity and "expose" that as an API Resource.
It's time for some magic.
Multiple Submit Buttons
…it also guesses
field type options. For example, if a Doctrine entity property is nullable: true like
for this notes field:
Then Symfony will automatically make it optional. And the reverse for
the other fields, the required HTML attribute will be added if the field…
Autoconfiguration
…configuration, and environments. We are powerful! No, unstoppable.
In the next tutorial, we'll introduce Doctrine - the industry standard way to
work with databases in PHP. Until then practice. Go build something -
anything - and tell us about it. And if you have any questions, thoughts…
The Serializer
…methods.
Cool! This will also cause the column in my database to change... so spin
over to your console and run:
symfony console make:migration
I'll live dangerously and run that immediately:
symfony console doctrine:migrations:migrate
Done! Thanks to that rename... over in…
x
1000+