具有ManyToMany关联的FOSRest视图实体阻止深层嵌套的json数组

时间:2016-08-28 17:43:41

标签: arrays json symfony fosrestbundle

我的头衔可能不是最好的;我发现很难找到问题的简短描述。

我有两个对象: 对象和元素(具有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": [],
            }]
        }]
    }
}

我想在&#34;元素&#34;数组&#34;对象&#34;显示,因为有&#34;元素&#34;将再次出现,等等......

实现这一目标的正确方法是什么? 我更喜欢输出是这样的:

{
    "object": {
        "id": 10,
        "name": "Object 3",
        "elements": [{
            "id": 1,
            "name": "Element 1",
        }]
    }
}

非常感谢!

1 个答案:

答案 0 :(得分:1)

您必须将对象和元素之间的关系更改为单向: 对象方:

/**
 * @ORM\ManyToMany(targetEntity="Element", cascade={"persist"})
 * @ORM\JoinTable(name ="objects_elements")
 */
 protected $elements;

元素方面:

null

检查并纠正最终错误(可选):

bin/console doctrine:schema:valid

或对于Symfony&lt; 3

app/console doctrine:schema:validate

最后删除Element类中的旧setter / getter和objects属性并执行:

bin/console doctrine:generate:entities YourBundle:Element

如果您想获取Element的所有对象链接,请记住您必须从ElementRepository中获取类似getObjects()的客户存储库方法。我希望它有所帮助。