I have an Asset object – Which as a creator object as a property – and the Creator object has a ‘featured_asset’ as a property. But this is causing a circular reference error in Symfony Serializer. I have tried setting #[MaxDepth(1)] on the featured asset but it doesnt make a difference, so I want to implement a custom CircularReferenceHandler.
I have these lines in config/packages/framework.yaml:
serializer:
circular_reference_handler: AppSerializerCircularReferenceHandler
enabled: true
And in AppSerializerCircularReferenceHandler.php:
<?php
namespace AppSerializer;
class CircularReferenceHandler
{
public function __invoke($object)
{
return $object->getId();
}
}
The way I am serializing the object is with $this->json in SearchController:
return $this->json($this->buildResponse($results), 200, [], [‘groups’ => $this->scope, ‘max_depth’ => 1]);
But it seems to have an issue with me adding ‘serializer’ in framework.yaml, becauese I get this error:
There is no extension able to load the configuration for “serializer” (in “/var/www/html/config/packages/framework.yaml”). Looked for namespace “serializer”, found “”framework”, “nelmio_cors”, “fos_elastica”, “doctrine”, “doctrine_migrations”, “fos_rest”, “security”” in /var/www/html/config/packages/framework.yaml (which is being imported from “/var/www/html/src/Kernel.php”).
I think I must be missing something. I ran composer install symfony/serializer. This is my composer.json:
{
"type": "project",
"license": "proprietary",
"minimum-stability": "stable",
"prefer-stable": true,
"require": {
"php": "8.3",
"ext-ctype": "*",
"ext-curl": "*",
"ext-dom": "*",
"ext-iconv": "*",
"ext-memcached": "*",
"ext-simplexml": "*",
"ext-zip": "*",
"doctrine/annotations": "^2.0",
"doctrine/dbal": "^3",
"doctrine/doctrine-bundle": "^2.11",
"doctrine/doctrine-migrations-bundle": "^3.3",
"doctrine/orm": "*",
"friendsofsymfony/elastica-bundle": "*",
"friendsofsymfony/rest-bundle": "^3.6",
"nelmio/cors-bundle": "^2.4",
"symfony/console": "6.4.*",
"symfony/dotenv": "6.4.*",
"symfony/expression-language": "6.4.*",
"symfony/flex": "^2",
"symfony/form": "6.4.*",
"symfony/framework-bundle": "6.4.*",
"symfony/http-foundation": "6.4.*",
"symfony/process": "6.4.*",
"symfony/runtime": "6.4.*",
"symfony/security-bundle": "6.4.*",
"symfony/serializer": "6.4.*",
"symfony/validator": "6.4.*",
"symfony/yaml": "6.4.*"
},
"config": {
"allow-plugins": {
"php-http/discovery": true,
"symfony/flex": true,
"symfony/runtime": true
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"App\": "src"
}
},
"autoload-dev": {
"psr-4": {
"App\Tests\": "tests/"
}
},
"replace": {
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php72": "*",
"symfony/polyfill-php73": "*",
"symfony/polyfill-php74": "*",
"symfony/polyfill-php80": "*",
"symfony/polyfill-php81": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "6.4.*"
}
},
"require-dev": {
"rector/rector": "^1.0"
}
}
What can I do to fix this? Really I would love not to do this at all and just be able to set the MaxDepth and have the featured_asset only display top level properties.
Please help.