Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Lucky you! You found an early release chapter - it will be fully polished and published shortly!

Collections Write

This Chapter isn't
quite ready...

Rest assured, the gnomes are hard at work
completing this video!

Browse Tutorials

Coming soon...

Let's fetch a single user in our api. I know I have one I with ID three and cool. So as we learned earlier, exposing a collection relation properties, just like any other field, just make sure that it's in the correct serialization group. And then you can go further with serialization groups to change it to be from an array of i I strings or an array of embedded objects like we have now. But my question now is, could we also modify the dragon treasures from the user endpoints? The answer is of course, yes. And we're going to do this in increasingly crazy ways. So if we look at the post endpoint, you don't see dragon treasures in there right now because the field simply isn't writeable, it's not in the right group. So to make it writeable, we know what to do. We'll just add user on. Right? And now back over on our documentation. No surprise. There we go. Dragon treasures. And what's in expecting is an array of strings, an array of I R I strings. So let's try this. Let's create a new user, fill in the email and fill in the username. And then I'm gonna assign this two existing treasures that are in our system. So I'm actually gonna cheat up here real quick and use the get collection endpoint for treasures and Perfect. So I have ID two and ID three.

Cool. So let's take down here. So slash api slash treasures.

Oops

Slash two and slash api slash treasures slash three. Ah heck, let's get greedy API slash treasure slash four. So just like when we read it, it can be I an I I, there's no problems at all with us setting it so when you execute, that worked perfectly. So we kind of stole those treasures from someone else. But wait a second, how did that work? We know that when we send fields like email, password, and username, because those are private properties that you use as the setter. So when we pass username, it calls set username. But when we pass, so who pass dragon treasures, it must call it set dragon treasures.

But guess what? We don't have a set dragon treasures, but we do have an ad dragon treasure method and a removed dragon treasure method. So the serializer is really smart. It sees that our user object has no dragon treasures. And so it recognizes that each of these three objects are new dragon treasures. And so it actually called add dragon treasure one time for each of those dragon treasures. And really importantly, the way that make SD generates these methods is it makes sure that it takes that dragon treasure and sets the owner to be this object. The reason that's important has to do with doctrine, relationships and the owning versus the inverse side. But the takeaway is that thanks to add dragon treasure being called, and the way this method is written, it gets our data, our data set up exactly like it needs to. And then when everything saves, it saves correctly. So sweet. Next, let's get more complex. By allowing treasures to be created when we're creating a new user, we're also going to remove treasures from a user. Like in the event that the dwarves take back the mountain as if.

Leave a comment!

0
Login or Register to join the conversation
Cat in space

"Houston: no signs of life"
Start the conversation!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=8.1",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "api-platform/core": "^3.0", // v3.0.8
        "doctrine/annotations": "^1.0", // 1.14.2
        "doctrine/doctrine-bundle": "^2.8", // 2.8.0
        "doctrine/doctrine-migrations-bundle": "^3.2", // 3.2.2
        "doctrine/orm": "^2.14", // 2.14.0
        "nelmio/cors-bundle": "^2.2", // 2.2.0
        "nesbot/carbon": "^2.64", // 2.64.1
        "phpdocumentor/reflection-docblock": "^5.3", // 5.3.0
        "phpstan/phpdoc-parser": "^1.15", // 1.15.3
        "symfony/asset": "6.2.*", // v6.2.0
        "symfony/console": "6.2.*", // v6.2.3
        "symfony/dotenv": "6.2.*", // v6.2.0
        "symfony/expression-language": "6.2.*", // v6.2.2
        "symfony/flex": "^2", // v2.2.4
        "symfony/framework-bundle": "6.2.*", // v6.2.3
        "symfony/property-access": "6.2.*", // v6.2.3
        "symfony/property-info": "6.2.*", // v6.2.3
        "symfony/runtime": "6.2.*", // v6.2.0
        "symfony/security-bundle": "6.2.*", // v6.2.3
        "symfony/serializer": "6.2.*", // v6.2.3
        "symfony/twig-bundle": "6.2.*", // v6.2.3
        "symfony/ux-react": "^2.6", // v2.6.1
        "symfony/validator": "6.2.*", // v6.2.3
        "symfony/webpack-encore-bundle": "^1.16", // v1.16.0
        "symfony/yaml": "6.2.*" // v6.2.2
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.2
        "symfony/debug-bundle": "6.2.*", // v6.2.1
        "symfony/maker-bundle": "^1.48", // v1.48.0
        "symfony/monolog-bundle": "^3.0", // v3.8.0
        "symfony/stopwatch": "6.2.*", // v6.2.0
        "symfony/web-profiler-bundle": "6.2.*", // v6.2.4
        "zenstruck/foundry": "^1.26" // v1.26.0
    }
}