Chapters
27 Chapters
|
2:54:11
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3
Subscribe to download the code!Compatible PHP versions: ^7.1.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Este tutorial funciona muy bien para Symfony 5 y la Plataforma API 2.5/2.6.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"api-platform/core": "^2.1", // v2.4.3
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.10.2
"doctrine/doctrine-bundle": "^1.6", // 1.11.2
"doctrine/doctrine-migrations-bundle": "^2.0", // v2.0.0
"doctrine/orm": "^2.4.5", // v2.7.2
"nelmio/cors-bundle": "^1.5", // 1.5.5
"nesbot/carbon": "^2.17", // 2.19.2
"phpdocumentor/reflection-docblock": "^3.0 || ^4.0", // 4.3.1
"symfony/asset": "4.2.*|4.3.*|4.4.*", // v4.3.11
"symfony/console": "4.2.*", // v4.2.12
"symfony/dotenv": "4.2.*", // v4.2.12
"symfony/expression-language": "4.2.*|4.3.*|4.4.*", // v4.3.11
"symfony/flex": "^1.1", // v1.21.6
"symfony/framework-bundle": "4.2.*", // v4.2.12
"symfony/security-bundle": "4.2.*|4.3.*", // v4.3.3
"symfony/twig-bundle": "4.2.*|4.3.*", // v4.2.12
"symfony/validator": "4.2.*|4.3.*", // v4.3.11
"symfony/yaml": "4.2.*" // v4.2.12
},
"require-dev": {
"symfony/maker-bundle": "^1.11", // v1.11.6
"symfony/stopwatch": "4.2.*|4.3.*", // v4.2.9
"symfony/web-profiler-bundle": "4.2.*|4.3.*" // v4.2.9
}
}
13 Comments
Hey! Going out on a limb here.
In the sorely missed polymorphism I've previously used in laravel projects, I've created a unidirectional relationship with a "price" entity that many products have a manyToOne relationship to (@ORM\ManyToOne(targetEntity=Price::class)).
This works for all intents and purposes, but the embed does not seem to work, with the right groups in the right places, the relation still returns an IRI.
Since I'm not using traditional relationships where everything is bidirectional (although doctrine seems to think uni is fine), any chance someone knows/can confirm, that unidirectional relationships do not possess this functionality?
So it does work, I was just too tunnel vision'ed to see why, I had two @ApiResource annotations.
Remember to check your annotations people, IDE won't help you here hahaha.
Hey Musa
Yeah, that's the problem with annotations, they're hard to validate. If you're on PHP8 you may want to consider migrating to PHP attributes
Cheers!
I have an interesting scenario:
I would like to return some object of class A, but include only IRI of B.
The way I see it would have to mark property in HasTimestamp with
@GroupBut either way, I get:
`
{
}
`
Is there any way to tell the Serializer not to go down the object B, for this property, despite the @Groups?
Thanks! :)
I think I've got the it, but I would really appreciate any thoughts on this. I wouldn't want to go down any hackish solution :)
Just added `@ApiProperty(readableLink=false)` to property `b` of class A. Does that make sense?
But this comes at a cost: What if I wanted to get only B's title (and not just IRI)? This brings me back to square one :(
Hey Jovan P.!
Hmm, interesting problem. You're correct that this should be handled via @Group... but I guess the tricky part is that the properties are shared in the trait, and so you can't give the HasTimestamp properties different groups in each entity. If you were using Yaml or XML config for API Platform this wouldn't be a problem, but I don't think that you can mix them (have most config in annotations, and just one property in Yaml/XML).
About
readableLink=false, from my understanding of that option - https://github.com/api-platform/core/issues/479 - I think your solution is correct/valid. But I see your point that this completely turns things into an IRI... but if you wanted to include an embedded object but avoid the "mtime" field, then that's still not possible. You effectively want to be able to change the "group" of a property in an object at runtime, and I'm not sure that's possible (well, probably possible, but maybe not easy/nice).The only solution I can think of is to:
A) put no groups into HasTimestamp so that they are not serialized
B) Inside A, add getModifiedTime() that returns mtime and put the A:read group above this. Repeat the same for B and put B:read above that.
The downside is more work :). The upside is that you are explicitly controlling what fields you want serialized.
Let me know what you think!
Cheers!
I actually very much like the idea of having explicit control with methods. Nice! :) The whole time I was chasing the solution by annotating the properties, but that sounds much more elegant even if that means just a bit more of code.
Thanks Ryan! :)
How to increase embedded form depth? because i think default is 2
i added "userQueue:next" in @Groups in all related entities but only first relation is embedded document, rest are IRI
Hey Daniel K.
You can define the depth by using the
MaxDepthannotation. You can check it on the docs here (almost at the bottom of the topic): https://api-platform.com/docs/core/serialization/#using-serialization-groupsCheers!
Hi, it's possible to use MaxDepth with sorting value ?
Example.. i have user and want also embedded data as last 5 transactions or transaction for 30 days.
Is this possible by annotation or i need to serape api requests to solve this scenario?
Hey @A. Mikolaj!
Hmm. I’m not sure about MaxDepth and how that fits into things. But here is how I’d solve this.
I would create, for example, a getRecentTransactions() method. Inside, filter the collection how you want (last 5 or last 30 days). You can use Doctrine’s criteria system to do this efficiently (without querying for ALL the transactions... only to limit them later). Details at https://symfonycasts.com/sc...
Once you’ve done this, you can expose this as a field via @Groups().
Does this help? I feel like I might be missing a detail - because I can’t think of how MaxDepth fits in here. So let me know :).
Cheers!
Hi all,
I have a many to many relation to itself (via a join table).
And i think this is the challenge.
Relation is working just fine as long as i only show the links to the relation.
As soon as i want to show the related data, i get this error.
<blockquote> "hydra:title": "An error occurred",
"hydra:description": "The total number of joined relations has exceeded the specified maximum. Raise the limit if necessary, or use the \"max_depth\" option of the Symfony serializer.",</blockquote>
Do i need to use 'max depth' and if so where do i put it, in the 'parent' or the 'child'? I've tried, but i keep getting the message.
Thanks in advance.
In the mean time I have made an entity for this join table, because i need some extra fields in this join table.
So now i have a one-to-many and a many-to-one relation to this join table.
This doesn't solve the problem ofcourse.
I think i solved it<spoiler>`
/**
`</spoiler>
Hey truuslee
That's a tricky one, you made me dig but didn't find anything out of the box. In this thread they recommend to create a custom normalizer for such cases. https://github.com/api-plat...
I hope it helps. Cheers!
"Houston: no signs of life"
Start the conversation!