1000 search results

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…

8:59
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…

3:52
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…

6:36
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.…

4:07
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…

4:56
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…

6:11
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…

7:27
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…

4:51
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…

7:31
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…

8:58
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…

5:10
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…

9:09
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()…

5:45
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…

6:13
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…

9:08
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…

7:49
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…

8:49
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…

4:29
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…

9:23
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…

6:42