Lucky you! You found an early release chapter - it will be fully polished and published shortly!
Rest assured, the gnomes are hard at work
completing this video!
Coming soon...
Let's get our write endpoints working for our new Dragon Treasure. So let's see here. There is a test called test post to create treasure. That sounds like a good one. Symfony PHP bin slash PHP init dash dash filter equals test post to create treasure. And that explodes actually ran a couple of tests. We can look at any of them. So I'm going to say the same thing. No mapper found for Dragon Treasure API to Dragon Treasure. So following the logic here, we know what happens internally. When we try to create something, it deserializes the JSON into a Dragon Treasure API object, and then calls our processor. Our processor takes that API object and tries to use the micro mapper to map it over to our entity. So we're missing is that Dragon Treasure API to Dragon Treasure mapper. There's no problem. Let's create that. In source mapper, I'll create a new Dragon Treasure API to entity mapper. Y'all know the drill at this point, it's going to implement mapper interface. Use the as mapper to say that this is going to come from a Dragon Treasure API to a Dragon Treasure. Then we'll implement the methods. This is going to be very similar to our user API to entity method. So one of the things we need to do in load is if if there is an ID, we're actually going to query for that object. So I'm going to go ahead and add a constructor right now. The private Dragon Treasure repository repository. Down here, I'll use my normal DTO equals from an assert that DTO is an instance of Dragon Treasure API, just to keep my editor and I both sane. If the entity is going to be actually I'm going to steal some code from our other mapper, it's gonna be so similar. So I'm gonna copy the code there, paste it here, it cancels, I don't actually want that you statement. So let's rename this to just entity. So if the detail has an ID, it means we're editing it and we want to find it. Else, we're going to create a new Dragon Treasure. And it shouldn't happen, we'll have the exception in case that's not found. Now, one interesting thing about our Dragon Treasure entity is that it has a constructor argument, which is the name. And there actually is no set name method on it. So the only way to set the name is through the constructor. So it's actually a case where I'm going to transfer the name from the DTO onto the entity right there when I instantiate it if it's a new one. Alright, then down in populate, I'll start with the same code that I have on load. Also add entity equals two. And one more assert for entity being an instance of Dragon Treasure. And then I'll put to do for the other fields down there, let's just at least get this thing mapping. Alright, so when we ran our test earlier, it actually ran three tests that match that name, let me make the test name a little more unique. So this is test post create treasure using the normal login mechanism. So we're going to say with login. And let's rerun that treasure, rerun just that test. And okay, current status code
"Houston: no signs of life"
Start the conversation!
// composer.json
{
"require": {
"php": ">=8.1",
"ext-ctype": "*",
"ext-iconv": "*",
"api-platform/core": "3.1.x-dev", // 3.1.x-dev
"doctrine/annotations": "^2.0", // 2.0.1
"doctrine/doctrine-bundle": "^2.8", // 2.10.2
"doctrine/doctrine-migrations-bundle": "^3.2", // 3.2.4
"doctrine/orm": "^2.14", // 2.16.1
"nelmio/cors-bundle": "^2.2", // 2.3.1
"nesbot/carbon": "^2.64", // 2.69.0
"phpdocumentor/reflection-docblock": "^5.3", // 5.3.0
"phpstan/phpdoc-parser": "^1.15", // 1.23.1
"symfony/asset": "6.3.*", // v6.3.0
"symfony/console": "6.3.*", // v6.3.2
"symfony/dotenv": "6.3.*", // v6.3.0
"symfony/expression-language": "6.3.*", // v6.3.0
"symfony/flex": "^2", // v2.3.3
"symfony/framework-bundle": "6.3.*", // v6.3.2
"symfony/property-access": "6.3.*", // v6.3.2
"symfony/property-info": "6.3.*", // v6.3.0
"symfony/runtime": "6.3.*", // v6.3.2
"symfony/security-bundle": "6.3.*", // v6.3.3
"symfony/serializer": "6.3.*", // v6.3.3
"symfony/stimulus-bundle": "^2.9", // v2.10.0
"symfony/string": "6.3.*", // v6.3.2
"symfony/twig-bundle": "6.3.*", // v6.3.0
"symfony/ux-react": "^2.6", // v2.10.0
"symfony/ux-vue": "^2.7", // v2.10.0
"symfony/validator": "6.3.*", // v6.3.2
"symfony/webpack-encore-bundle": "^2.0", // v2.0.1
"symfony/yaml": "6.3.*", // v6.3.3
"symfonycasts/micro-mapper": "^0.1.0" // v0.1.1
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.4
"mtdowling/jmespath.php": "^2.6", // 2.6.1
"phpunit/phpunit": "^9.5", // 9.6.11
"symfony/browser-kit": "6.3.*", // v6.3.2
"symfony/css-selector": "6.3.*", // v6.3.2
"symfony/debug-bundle": "6.3.*", // v6.3.2
"symfony/maker-bundle": "^1.48", // v1.50.0
"symfony/monolog-bundle": "^3.0", // v3.8.0
"symfony/phpunit-bridge": "^6.2", // v6.3.2
"symfony/stopwatch": "6.3.*", // v6.3.0
"symfony/web-profiler-bundle": "6.3.*", // v6.3.2
"zenstruck/browser": "^1.2", // v1.4.0
"zenstruck/foundry": "^1.26" // v1.35.0
}
}