Fosrestbundle: When using contexts relations are not being filled

Created on 5 Feb 2017  路  1Comment  路  Source: FriendsOfSymfony/FOSRestBundle

I'm quite new in FOSRestBundle, so sorry.

I have two entities Event and Ticket. Event has a relation *@ORM\OneToMany(targetEntity="Ticket", mappedBy="event") with Ticket.

I'm also using JSMSerializerBundle and I would want to restrict showing the Tickets of an Event only to the @Group 'detail' like so: * @JMS\Groups({"detail"})

After that, in the EventController I set the context:

 /**
 * @Rest\Get("/events/{id}")
 * ...
 */
public function findById($id)
{
    $context = new Context();
    $context->addGroups(['default', 'detail']);

    //...entity manager stuff and so

    $view = $this->view($event, Response::HTTP_OK);
    $view->setContext($context);

    return $this->handleView($view);
}

What I get since I set the $context is (Tickets are not being filled):

{
  "id": 1,
  "name": "Venue1",
  "slug": "venue1",
  "date_tms": "1485733573",
  "active": true,
  "tickets": [
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    {}
  ]
}

If I don't use $context the related Tickets are being included in the json response, but obviously, groups are not working):

{
  "id": 1,
  "name": "Venue1",
  "slug": "venue1",
  "date": "2017-01-31T22:00:00+0100",
  "date_tms": "1485733573",
  "active": true,
  "created_at": "2017-01-29T17:33:00+0100",
  "tickets": [
    {
      "id": 1,
      "locator": "locator1",
      "active": true
    },
    {
      "id": 2,
      "locator": "locator2",
      "active": true
    },

I have tried to force the fetch mode to EAGER with no luck, and I'm really lost with this.

Using:

"symfony/symfony": "3.2.*",
"doctrine/orm": "^2.5",
...        
"friendsofsymfony/rest-bundle": "^2.1",
"jms/serializer-bundle": "^1.1",

Thanks for any help

Most helpful comment

Finally I solved it. For anyone with the same doubt the related entities must have defined groups also.

I have defined some groups for the action findById, in this case using serializerGroups in the @View annotation:

/**
 * @Rest\Get("/events/{id}")
 * @Rest\View(serializerGroups={"default", "detail"})
 * ...
 * @return View
 */
public function findById($id)
{
   ...
}

So, the Ticket entity must define any property under any of the default or detail groups, if not the related Tickets will be empty.

It is not an issue so it can be closed. Sorry.

>All comments

Finally I solved it. For anyone with the same doubt the related entities must have defined groups also.

I have defined some groups for the action findById, in this case using serializerGroups in the @View annotation:

/**
 * @Rest\Get("/events/{id}")
 * @Rest\View(serializerGroups={"default", "detail"})
 * ...
 * @return View
 */
public function findById($id)
{
   ...
}

So, the Ticket entity must define any property under any of the default or detail groups, if not the related Tickets will be empty.

It is not an issue so it can be closed. Sorry.

Was this page helpful?
0 / 5 - 0 ratings