I’m building an API with the MongoDBbundle. I have a document “Ingredient” that looks like that :
#[MongoDBId]
#[Groups(["getIngredients"])]
private string $id;
#[MongoDBField(type: 'string')]
#[Groups(["getUnits","getUserSources","getIngredients"])]
private string $name;
#[MongoDBField(type: 'string')]
private string $slug;
#[MongoDBReferenceOne(targetDocument: IngredientCategorie::class)]
#[MaxDepth(1)]
private IngredientCategorie $categorie;
#[MongoDBReferenceOne(targetDocument: User::class)]
#[MaxDepth(1)]
private User $owner;
#[MongoDBField(type: 'string')]
private string $privacy;
#[MongoDBField(type: 'collection')]
private $instructions;
I’m trying to build a route to get the list of every ingredients so I did this :
#[Route('/ingredients', name:'getAllIngredients', methods:['GET'])]
public function getAllIngredients(DocumentManager $dm, SerializerInterface $serializer, Request $request, TagAwareCacheInterface $cachePool): JsonResponse
{
$idCache = "getAllIngredients";
$jsonList = $cachePool->get($idCache,function(ItemInterface $item) use ($page,$limit,$serializer,$dm){
$item->tag("ingredientsCache");
$ingredients = $dm->getRepository(Ingredient::class)->findAll();
return $serializer->serialize($ingredients, 'json',["group"=>"getIngredients"]);
});
return new JsonResponse($jsonList, Response::HTTP_OK,[],true);
}
I’m already using groups annotations to avoir circular reference, here I’m only suppose to serialize $id and $name from the “Ingredient.php” class. But I still get a circular reference :
“A circular reference has been detected when serializing the object of class “MongoDBODMProxiesPMAppDocumentUserGenerateda24e9e269e70a012aa0eb80f98d12805″ (configured limit: 1).”
My “User.php” class as two fields with EmbedMany relation, $sources and $actives. Both Sources class and Actives class have one field like this :
#[MongoDBReferenceOne(targetDocument: Ingredient::class)]
private Ingredient $ingredient;
But I’m not serializing it, how to get rid of that circular reference ?
I tried to use #[MaxDepth(1)] annotation but without success. Already restarted the server and cleared the cache.