我在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”:“检测到循环引用(配置限制: 1) “” 类 “:” 的Symfony \元器件\串行\异常\ CircularReferenceException“
当使用JMSSerializer时,当我转到控制器的相应页面时,页面永远不会完成加载。同时在dev.log文件中,每秒都会添加新的Doctrine.debug条目以及对我的数据库的请求。
答案 0 :(得分:2)
$normalizers->setCircularReferenceHandler(function ($object) {
return $object->getId();
});
如果你的objectNormalizer()在你创建实例后添加它 它对我来说很完美
答案 1 :(得分:0)
如果使用FosRestBundle,则可以将GROUPS用于序列化程序。 FosRestBundle给出了一个注释:@FOS \ RestBundle \ Controller \ Annotations \ View(serializerGroups = {“user”})
您的论坛可以排除圆形属性。
你可以做到这一点的另一个想法。在你的app / config / services.yml
中circular_reference_handler:
public: false
class: callback
factory: [AppBundle\Serializer\CircularHandlerFactory, getId]
serializer.normalizer.object:
class: Symfony\Component\Serializer\Normalizer\ObjectNormalizer
arguments: ["@serializer.mapping.class_metadata_factory", null, "@serializer.property_accessor"]
public: false
tags: [serializer.normalizer]
calls:
- method: setCircularReferenceLimit
arguments: [1]
- method: setCircularReferenceHandler
arguments: ["@circular_reference_handler"]
工厂可以是这样的:
namespace AppBundle\Serializer;
class CircularHandlerFactory
{
/**
* @return \Closure
*/
public static function getId()
{
return function ($object) {
return $object->getId();
};
}
}