We <3 Creating Stripe Customers
…our user record, say $user->setStripeCustomerId($customer->id):
Then, I'll use Doctrine to run the UPDATE query to the database:
If you're not using Doctrine, just make sure to update the user record in the database
however you want.
Now, add the else…
Adding a Cache Service
…it entirely on our own.
First, we need to configure the bundle. To see what keys it has, find the terminal
and run:
The list has a new entry: doctrine_cache. Re-run the command with this:
Nice! There's our huge configuration example! Ok…
Weird Endpoint: The tagline as a Resource?
…the route should be /api/programmers/{nickname}/tagline.
To be super hip, add an @Method annotation: we know this should only match PUT requests:
Like before, type-hint the Programmer argument so that Doctrine will query for
it for us, using the nickname value. And…
Tests with the Container
…means
we have an easy way to clear data. Create a new private function called purgeDatabase().
Because we have the Doctrine DataFixtures
library installed, we can use a great class called ORMPurger. Pass it the
EntityManager - so $this->getService('doctrine')->getManager(). To clear
things out…
Test Fixtures and the PropertyAccess Component
…what a wonderfully
trained function:
Let's save this! Since we'll need the EntityManager a lot in this class,
let's add a protected function getEntityManager(). Use getService() with
doctrine.orm.entity_manager. And since I love autocomplete, give this
PHPDoc:
Now $this->getEntityManager()…
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…
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…
HATEOAS Loves Routers
…and
it doesn't! I messed up some syntax. Anytime you see the Doctrine\Common\Annotations
T_CLOSE_PARENTHESIS type of thing, this is a syntax error in your annotation.
I'm missing a comma between my arguments. Let's try that one more time…
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…
Deployment
…Update your database schema. The easy, but maybe dangerous way is with
the schema update console command:
php app/console doctrine:schema:update --force
Why dangerous? Let’s say you rename a property from name to firstName.
Instead of renaming the column, this task may…
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…
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…
PHP 8.4 & Recipe Updates
…we have a bunch to update. We'll go through them one-by-one. Hitting enter will
use the first one in the list, doctrine/deprecations.
"No file changed...". Hmm, run git status so see what's up. Just the symfony.lock file was modified…
Upgrading to Symfony 7.4
…Pop over to our app and refresh the homepage to make
sure everything's still running smoothly. Nice, 7.4 upgrade was a success!
Next, we're going to update Doctrine and explore a cool improvement in the
latest version of the ORM. Stay tuned!
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.
x
1000+