Мое название, вероятно, не самое лучшее; Мне трудно найти краткое описание проблемы.
У меня есть 2 объекта: объект и элемент (с ассоциацией ManyToMany)
Объект:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity(repositoryClass="AppBundle\Repository\ObjectRepository")
* @ORM\Table(name="object")
*/
class Object {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
*
* @ORM\Column(type="string", nullable=true)
*/
protected $name;
/**
* @ORM\ManyToMany(targetEntity="Element", inversedBy="objects", cascade={"persist"})
* @ORM\JoinTable(name ="objects_elements")
*/
protected $elements;
function __toString() {
return $this->getName();
}
getters and setters....
И элемент:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity(repositoryClass="AppBundle\Repository\ElementRepository")
* @ORM\Table(name="element")
*/
class Element {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
*
* @ORM\Column(type="string", nullable=true)
*/
protected $name;
/**
* @ORM\ManyToMany(targetEntity="Object", mappedBy="elements", cascade={"persist"})
*/
private $objects;
getters and setters....
Контроллер расширяет FOSRestController:
<?php
namespace AppBundle\Controller\Api;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use AppBundle\Entity\Object;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations\View;
/**
* Object controller.
*
* @Route("/object")
*/
class ObjectController extends FOSRestController {
/**
* Show Object entitie.
*
* @Route("/{id}/edit/api", options={"expose"=true}, name="object_show_api")
* @Method("GET")
*
*/
public function jsonShowAction($id) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('AppBundle:Object')->findOneById($id);
$view = $this->view($entity
, 200)
->setTemplate("object/json.html.twig");
return $this->handleView($view);
}
Вывод контроллера следующий json:
{
"object": {
"id": 10,
"name": "Object 3",
"elements": [{
"id": 1,
"name": "Element 1",
"objects": [{
"id": 1,
"name": "Object 2a",
"elements": [],
}]
}]
}
}
Я хочу, чтобы в «элементах» отображался массив «объекты», потому что там снова будут отображаться «элементы» и так далее...
Каков правильный способ добиться этого? Я предпочитаю, чтобы вывод был примерно таким:
{
"object": {
"id": 10,
"name": "Object 3",
"elements": [{
"id": 1,
"name": "Element 1",
}]
}
}
Большое спасибо!