0

I'm trying to encapsulate data from my symfony api.

There is my serialization.yml:

AppBundle\Entity\Parcs:
attributes:
    id:
        groups: ['parcs']
    latitude:
        groups: ['parcs']
    longitude:
        groups: ['parcs']
    sac:
        groups: ['parcs']
    name:
        groups: ['parcs'] 

And there is my ParcsController:

 /**
 * @Rest\View(serializerGroups={"parcs"})
 * @Rest\Get("/parcs")
 */
public function getParcsAction(Request $request)
{
    $parcs = $this->get('doctrine.orm.entity_manager')
            ->getRepository('AppBundle:Parcs')
            ->findAll();
    /* @var $parcs Parc[] */

    return $parcs;
}

With those data i want to get [id, sac, name, Coordinate[latitude, longitude]].

PS : I'm still learning symfony if you have a good tutorial I'll be happy too. Thx a lot!

1 Answer 1

1

You've only defined groups there, but you haven't actually 'used' them with the serializer. You need to install and configure Serializer Component https://symfony.com/doc/current/components/serializer.html

Next, you'd want to extend AbstractController which has a neat json() method that will automatically figure out that the serializer service is loaded and will automatically serialize your data.

 // extends AbstractController

 /**
* @Rest\View(serializerGroups={"parcs"})
* @Rest\Get("/parcs")
*/
public function getParcsAction(Request $request)
{
   ... everything you said

return $this->json($parcs);
}  

EDIT: Also, I see you're expecting latitude and longitude to be a part of different object (from your response example). If that's the case, you need to define those two properties as part of that new object (serialization.yml). They can't be part of Parcs entity, you'd need to create a new one.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.