Doing Crazy things with Foundry & Fixtures
…database. Instead, the new QuestionTag object will be related to whatever Question
is currently being created. Foundry does this by reading the Doctrine relationship
and smartly overriding the question attribute on QuestionTagFactory.
But... I did say that there was a problem with this. And... we…
JOINing Across Multiple Relationships
…question_tag data that's between
Question and Tag. Doing that is legal in SQL... but it messes up how
Doctrine creates the objects, so it doesn't allow it.
The solution is easy enough: select both. You can actually pass an array to
addSelect…
Saving Items in a ManyToMany Relation
… Let's try this thing! Reload the fixtures:
symfony console doctrine:fixtures:load
And... no errors! Check the database:
symfony console doctrine:query:sql 'SELECT * FROM tag'
No surprise: we have two tags in this table. Now
SELECT * FROM question_tag - the join table.
And…
QuestionTag Fixtures & DateTimeImmutable with Faker
…We can see this. Open up the fixtures class and say
QuestionTagFactory::createMany(10). I'm going to put a return statement here
because some of the code below is currently broken.
Let's try this:
symfony console doctrine:fixtures:load
And... it fails! But.…
Lets Generate a PDF!
…or 0 users. If it doesn't send any emails, try reloading your fixtures
by running:
php bin/console doctrine:fixtures:load
If you are so lucky that it's sending more than 2 emails, you'll get an error
from Mailtrap, because it limits…
Async Emails with Messenger
…email - you need to configure a "queueing"
system where details about that work - called "messages" - will be sent. Messenger
calls these transports. Because we're already using Doctrine, the easiest "queueing"
system is a database table. Uncomment that MESSENGER_TRANSPORT_DSN to use it.
Next…
Processing Encore Files through inline_css()
…EntrypointLookupInterface - we're using a "service subscriber". You can learn
about this in, oddly-enough, our
tutorial about Symfony & Doctrine.
To fetch the service, go down to getSubscribedServices() and add
EntrypointLookupInterface::class.
Back up in getEncoreEntryCssSource(), we can say
$files = $this->container->get(EntrypointLookupInterface…
Resetting the Database Between Tests
…using random data and linking objects
to each other. It was the inspiration behind a fixture class that we created and
used in our Symfony Doctrine
tutorial. The recipe creates a fixtures/ directory for the YAML files and a new
command for loading that data…
Adding the plainPassword Field
…after the JSON is deserialized into a User object, but before it's saved
to the database. One way to do this is via a Doctrine event listener or
entity listener, which are more or less the same thing. That's a fine option...
though…
Deleting Files
…problem:
the file might get deleted, but then the row stays because of a temporary connection
error to the database.
If you're worried about this, use a Doctrine transaction to wrap all of this
logic. If the file was successfully deleted, commit the transaction…
Reordering the Files
…the usual:
php bin/console make:migration
and go to the src/Migrations directory so we can make sure it doesn't contain
any surprises. Looks perfect! Close that and run:
php bin/console doctrine:migrations:migrate
Ok, the database is ready! For the frontend…
File Upload Field in a Form
…will not work. Pretty commonly,
you'll see people create this property on their entity, just to make the form
work. They don't persist this property to the database with Doctrine... so the idea
works, but I don't love it.
Instead, we'll…
Setup for Uploading Private Article References
…yep!
CREATE TABLE article_reference
... with a foreign key back to article. Run that with:
php bin/console doctrine:migrations:migrate
Before we get back to work, open the Article entity. The command did create
the $articleReferences property that allows us to say
$article->getArticleReferences()…
Recipe Upgrades with recipes:update
…the recipe relate to the config files, and I bet you can see
what's happening. It deleted two environment-specific config files and updated
the main one. Hmm.
Open config/packages/doctrine.yaml. Sure enough, at the bottom, we see when@test
and when…
Detecting the "Published" State Change
…Methods"
and add the three method that we need:
Before we fill in the code, let's immediately inject the doctrine data
persister so that we can use it to do the actual saving. Add
public function __construct() with DataPersisterInterface $decoratedDataPersister:
I'll hit Alt…
Custom Filter for Custom Resources
…had before:
The only difference is that the apply() method has different arguments
than filterProperty()... which makes sense because that method was all
about querying via Doctrine.
Before we start filling this in, go to src/Entity/DailyStats.php so we can use
the filter…
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…
Bootstrapping a Test Suite
…use a different database, let's
create that database! Run bin/console doctrine:database:create. But since we
want this command to execute in the test environment, also add --env=test:
Repeat that same thing with the doctrine:schema:create command:
Perfect! Next, Api Platform has…
ACL: Only Owners can PUT a CheeseListing
…manager. Needing the entity manager
is so common, let's create another shortcut for it:
protected function getEntityManager() that will return EntityManagerInterface.
Inside, return self::$container->get('doctrine')->getManager().
Let's use that: $em = $this->getEntityManager(), $em->persist($cheeseListing)
and $em->batman(). Kidding. But…
Conditional Field Setup
…php bin/console doctrine:migrations:migrate
That updates our normal database. But because our test environment uses a
different database, we also need to update that too. Instead of worrying about
migrations on the test database, update it with:
php bin/console doctrine:schema:update…
x
1000+