Skip to content

Why would the find methods of the repository not return anything? #54911

Closed Answered by nbro
nbro asked this question in Q&A
Discussion options

You must be logged in to vote

We can't simply return the entity objects returned by the find methods as JSON content. We need to use a serializer or create an array from the object properties.

In my case, I did

composer require symfony/serializer
composer require symfony/property-access

Then in the controller

...
$encoders = [new JsonEncoder()];
$normalizers = [new ObjectNormalizer()];
$serializer = new Serializer($normalizers, $encoders);
$products = $repo->findAll();
return $this->json($serializer->serialize($products, 'json')); 

Alternatively, instead of just installing the packages symfony/serializer and symfony/property-access, we can install the symfony/serializer-pack pack with composer install symfony/serial…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by nbro
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant