Chapters
41 Chapters
|
4:45:40
|
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!
28.
Autocomplete Endpoint & Serialization Group
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.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.10.2
"doctrine/doctrine-bundle": "^1.6.10", // 1.10.2
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
"doctrine/orm": "^2.5.11", // v2.7.2
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.0
"knplabs/knp-paginator-bundle": "^2.7", // v2.8.0
"knplabs/knp-time-bundle": "^1.8", // 1.8.0
"nexylan/slack-bundle": "^2.0,<2.2.0", // v2.0.0
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"phpdocumentor/reflection-docblock": "^3.0|^4.0", // 4.3.0
"sensio/framework-extra-bundle": "^5.1", // v5.2.1
"stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
"symfony/asset": "^4.0", // v4.1.6
"symfony/cache": "^3.3|^4.0", // v4.1.6
"symfony/console": "^4.0", // v4.1.6
"symfony/flex": "^1.0", // v1.21.6
"symfony/form": "^4.0", // v4.1.6
"symfony/framework-bundle": "^4.0", // v4.1.6
"symfony/property-access": "^3.3|^4.0", // v4.1.6
"symfony/property-info": "^3.3|^4.0", // v4.1.6
"symfony/security-bundle": "^4.0", // v4.1.6
"symfony/serializer": "^3.3|^4.0", // v4.1.6
"symfony/twig-bundle": "^4.0", // v4.1.6
"symfony/validator": "^4.0", // v4.1.6
"symfony/web-server-bundle": "^4.0", // v4.1.6
"symfony/yaml": "^4.0", // v4.1.6
"twig/extensions": "^1.5" // v1.5.2
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.0.2
"easycorp/easy-log-handler": "^1.0.2", // v1.0.7
"fzaninotto/faker": "^1.7", // v1.8.0
"symfony/debug-bundle": "^3.3|^4.0", // v4.1.6
"symfony/dotenv": "^4.0", // v4.1.6
"symfony/maker-bundle": "^1.0", // v1.8.0
"symfony/monolog-bundle": "^3.0", // v3.3.0
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.1.6
"symfony/stopwatch": "^3.3|^4.0", // v4.1.6
"symfony/var-dumper": "^3.3|^4.0", // v4.1.6
"symfony/web-profiler-bundle": "^3.3|^4.0" // v4.1.6
}
}
14 Comments
[Semantical Error] The annotation "@Groups" in property App\Entity\User::$email was never imported. Did you maybe forget to add a "use" statement for this annotation?
use Symfony\Component\Serializer\Annotation\Groups;
But it says Undefined namespace Annotation in PHPStorm. Is this use url outdated?
Hey Farry,
Check if you have installed this library in your
composer.jsonfilesymfony/serializer-packWhere did you create AccountController? Which course / episode?
Hey Farry,
The AccountController comes with the starting directory of this tutorial. You only have to download the course code and copy that file from the
startdirectoryCheers!
Hi,
Is it possible to serialize without using groups? Just to take several fields.
Hey Mārtiņš C.
Yes you can, you should just pass array of needed values and that's all. But you will need manually iterate your entities and create this array.
Cheers!
Hi! Can I have the link where this "groups" think was first made? I came here just to the forms tutorial and it seems is part of a tutorial that I haven't done yet.
Hey Uriel G.
Yeah, that part was made in another tutorial (but part of this track). Here you have the link to it: https://symfonycasts.com/sc...
Cheers!
I have a textHighlight() function in a controller, to highlight the typed letters inside of the suggested username(s).
But I have a problem:
I can't simply pass the $highlightedUsername variable/property to algolia.
If I simply "extend" every User Object inside a foreach() via $users[$key]->autocompleteDisplayName, $this->json(... Groups=>main) filters the newly created autocompleteDisplayName property out.
Thats why I created a new "general placeholder" property inside the User Entity with Groups("main"):
Hey Adsasd!
I don't know if I totally understand the setup, but I two suggestion, hopefully one will work.
1) Instead of a "placeholder" property, it's legal to have a
getPlaceholder()method, which the serializer WILL use, as long as you put it in the right group:But this only works if you can calculate the "placeholder" value based on the other properties in your class. I have a feeling that you can't do that... because you probably need to know some outside information (like what's been typed in so far).
2) So... if the above doesn't work, try this, which I actually think is cleaner. I would stop thinking about this API endpoint as returning "Users" and instead imagine that it returns an array of "UserSearchResult"s. Specifically, I would:
A) Create a new model class called UserSearchResult - maybe in src/Model/UserSearchResult.php:
$users = // some query for the User objects;
$userSearchResults = [];
foreach ($users as $user) {
}
return $this->json($userSearchResults, ['groups' => ['main']])
" A circular reference has been detected. This is a common problem with the serializer and Doctrine objects."
Just when i thought that you are gonna show us a fix ... well ... i lost my hope.
do you think you can provide a fix in the future ? because it's a common problem :)
Hey Remus M.!
Haha, sorry! Mostly the fix is to think about what is being serialized, and limit it to only the things you want via groups. Basically, find out *why* there is a circular reference, then avoid serializing the property that causes it.
There is another solution - where you can "handle" the circular reference and take some action - let me know if you need info about that.
Cheers!
if possible, maybe you can provide a solution, when i really need to handle a circular reference, who knows what one might want to serialize with json
Hey Ad F.!
I absolutely can. This is actually a missing feature in Symfony's serializer (easily handling circular reference stuff) - hopefully we'll add it sometime soon. It's a 2 step process. If something doesn't work - let me know - I'm writing this directly into the comment - so it's not tested yet. Also, the way this is done changed in 4.2 slightly. Basically, suppose you're serializing in a controller:
This is a bit naive, but basically, when you set this option, instead of an error, this callback is executed. Then, you just return whatever you want to display on that property (instead of it trying to serialize the object again, and getting into a loop). You can't (in Symfony 4.2) configure this option globally, but you can effectively do it by creating some new service that you call, and where that service executes the serializer for your and always passes this option. In ApiPlatform, they implement this automatically and they return the API URL to the given resource - e.g. /api/products/10.
Let me know if it works - or doesn't! If you're on 4.1 or earlier, the solution is slightly different.
Cheers!
"Houston: no signs of life"
Start the conversation!