1000 search results

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
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
When a Migration Falls Apart

…ve fixed the migration, how can we run it again? Let's try the obvious: symfony console doctrine:migrations:migrate It fails again! But for a different reason: dropping the foreign key failed because that's already gone. Here's the problem. When we first…

6:23
Handling ManyToMany in Foundry

…maybe you see it... but let's try it anyways. Spin over and reload the fixtures: symfony console doctrine:fixtures:load Awesome. And now check the database. I'll first say: symfony console doctrine:query:sql 'SELECT * FROM tag' Yep! We do have 100 tags…

4:01
Filtering to Return only Approved Answers

…doesn't contain any surprises It looks good: ALTER TABLE answer ADD status. Close that, spin back to your terminal and execute it: symfony console doctrine:migrations:migrate Because we have exactly three possible statuses, I'm going to add a constant for each one…

9:40
Relation OrderBy & fetch=EXTRA_LAZY

…six answers, eight answers. But check out the web debug toolbar. Woh! We suddenly have a lot of queries. Click to open Doctrine's profiler. The first query is still for all of the question objects. But then, one-by-one it selects FROM answer…

7:22
Fetching Relations

…inside the Question entity, the $answers property will not be a true array of Answer objects. Nope, it will be some sort of Doctrine collection object. It may be an ArrayCollection object or this PersistentCollection object... just depending on the situation. It doesn't really…

6:58
Relations in Foundry

…Notice that, in getDefaults(), we are not setting the question property. And so, if you spin over to your terminal and run: symfony console doctrine:fixtures:load ... we get our favorite error: question_id column cannot be null. To fix this, in AppFixtures, pass a…

4:41
Saving Relations

…passing the ID to the question property, we're setting the entire Question object onto the property. Doctrine will be smart enough to save these in the correct order: it'll save the question first, grab its new id, and use that to save the…

3:58
UUID as a API Identifier

…talked about it before. Go to the top of User. Every API Resource needs an identifier. And when you use Doctrine, API Platform assumes that you want the database id as the identifier. To tell it to use something different, add @ApiProperty() with identifier=false: That…

5:16
UUID's

…how do we generate these UUID strings? Symfony 5.2 will come with a new UUID component, which should allow us to easily generate UUID's and store them in Doctrine. But since that hasn't been released yet, we can use ramsey/uuid, which…

7:29
Type Validation

…its work, it first tries to figure out what type the field should be, which we know it does in a number of different ways, like reading Doctrine metadata, setter argument type-hints and even PHPDoc. Then, for scalar types like the int price, if…

7:29
Input Data Transformer

…object. Second, we transform that CheeseListingInput into a CheeseListing in the data transformer. And third, the normal Doctrine data persister saves things. That's a really clean process! Go back to the docs and look at the put operation that updates cheeses. Will this work…

4:24
Custom Filter Logic for Entities

… Our filter is working! So this is what it looks like to make a custom filter when your resource is a Doctrine entity. But the process is different if you need to make a custom filter for an API resource that is not an entity…

8:25
Output Properties & Metadata

…that's no surprise! When we serialize a CheeseListing entity, API Platform can use the Doctrine metadata above the title property to figure out that it's a string: But in this case, when it looks at title, it doesn't get any info about…

7:14
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
Custom Filter, getDescription() & properties

…implement an interface or extend some base class. In this case, we need to extend AbstractFilter. Make sure you get the one from Doctrine ORM... it's actually impossible to see which one we have here... so I'll randomly guess: Hey! I got it…

7:09
Collection "Types" and readableLink

…lot of metadata about each property, like its type and whether it's required. And it gets that from many different sources like Doctrine metadata and our own PHPDoc. And because collecting all of this can take time, it caches it. Now, API Platform is…

7:12
Property Metadata

… Integers? Aliens? API Platform gets metadata about each property from many different places, like by reading Doctrine metadata, PHPDoc, looking at the return types of getter methods, looking at the argument type-hint on setters, PHP 7.4 property types and more. What's really…

6:20
Custom Resource Data Provider

…as the identifier? In Doctrine, it happens automatically thanks to the @ORM\Id annotation: When you're not in an entity - or if you want to use a field other than the database id in an entity - you can add @ApiProperty() with identifier=true: Cool! Let…

6:40