Symfony 3.0.4在使用FOSRestBundle进行序列化期间检测到循环引用

我在Symfony项目中使用FOSRestBundle.当我尝试处理视图时,在使用Symfony序列化程序以及JMSSerializer对数据进行序列化时失败.

这是呈现响应的方法:

DefaultController.php

$em = $this->getDoctrine()->getManager('magellan');
$qb = $em->createQueryBuilder();

$query = $qb->select('h')
        ->from('DataBundle:Holding', 'h')
        ->where($qb->expr()->eq('h.id', ':holding_id'))
        ->setParameter('holding_id', $holding_id)
        ->getQuery();

$results = $query->getResult();

$view = $this->view($results, 200);

// Everything's ok up to this point

return $this->handleview($view);

这些是我的实体:

Holding.php

class Holding
{

    ...

    /**
     * @ORM\OneToMany(targetEntity="Subsidiary", mappedBy="holding")
     */
    private $subsidiaries;
}

Subsidiary.php

class Subsidiary
{

    ...

    /**
     * @ORM\ManyToOne(targetEntity="Holding", inversedBy="subsidiaries")
     * @ORM\JoinColumn(name="id_holding", referencedColumnName="id_holding")
     */
    private $holding;

    /**
     * @ORM\OneToMany(targetEntity="Brand", mappedBy="subsidiary")
     */
    private $brands;
}

Brand.php

class Brand
{

    ...

    /**
     * @ORM\ManyToOne(targetEntity="Subsidiary", inversedBy="brands")
     * @ORM\JoinColumn(name="id_subsidiary", referencedColumnName="id_subsidiary")
     */
    private $subsidiary;

    /**
     * @ORM\OneToMany(targetEntity="Product", mappedBy="brand")
     */
    private $products;
}

Product.php

class Product
{

    ...

    /**
     * @ORM\ManyToOne(targetEntity="Brand", inversedBy="products")
     * @ORM\JoinColumn(name="id_brand", referencedColumnName="id_brand")
     */
    private $brand;

    /**
     * @ORM\ManyToOne(targetEntity="Sector", inversedBy="products")
     * @ORM\JoinColumn(name="id_sector", referencedColumnName="id_sector")
     */
    private $sector;

    /**
     * @ORM\OneToMany(targetEntity="Commercial", mappedBy="product")
     */
    private $commercials;
}

Commercial.php

class Commercial
{

    ...

    /**
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="commercials")
     * @ORM\JoinColumn(name="id_product", referencedColumnName="id_product")
     */
    private $product;

    /**
     * @ORM\OneToMany(targetEntity="CommercialReport", mappedBy="commercial")
     */
    private $reports;

CommercialReport.php

class CommercialReport
{

    ...

    /**
     * @ORM\ManyToOne(targetEntity="Commercial", inversedBy="reports")
     * @ORM\JoinColumn(name="id_commercial", referencedColumnName="id_commercial")
     */
    private $commercial;
}

Sector.php

class Sector
{

    ...

    /**
     * @ORM\OneToMany(targetEntity="Product", mappedBy="sector")
     */
    private $products;
}

使用默认的symfony序列化程序时,我收到以下错误:

“message”:”A circular reference has been detected (configured limit:
1).”,”class”:”Symfony\Component\Serializer\Exception\CircularReferenceException”

当使用JMSSerializer时,当我转到控制器的相应页面时,页面永远不会完成加载.同时在dev.log文件中,每秒都会添加新的Doctrine.debug条目以及对我的DB的请求.

最佳答案

    $normalizers->setCircularReferenceHandler(function ($object) {
        return $object->getId();
    });

如果你的objectNormalizer(),你只需在创建实例后添加它
它对我来说很完美

点赞