Symfony 3.0.4在使用FOSRestBundle进行序列化时检测到圆形参考 [英] Symfony 3.0.4 Circular reference detected during serialization with FOSRestBundle
问题描述
这是呈现响应的方法:
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);
//一切都ok到这一点
return $ this-> handleview($ view);
这些是我的实体:
Holding.php
class Holding
{
。
/ **
* @ ORM\OneToMany(targetEntity =Subsidiary,mappedBy =holding)
* /
private $ subsidiary;
}
Subsidiary.php
class Subsidiary
{
...
/ **
* @ ORM\ManyToOne(targetEntity =Holding,inversedBy =subsidiary)
* @ ORM\JoinColumn(name =id_holding,referencedColumnName =id_holding)
* /
private $ holding;
/ **
* @ ORM\OneToMany(targetEntity =品牌,mappedBy =子公司)
* /
private $品牌;
}
Brand.php
class品牌
{
...
/ **
* @ ORM\ManyToOne(targetEntity =Subsidiary,inversedBy =brands)
* @ ORM\JoinColumn(name =id_subsidiary,referencedColumnName =id_subsidiary)
* /
private $子公司;
/ **
* @ 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)
* /
私人品牌;
/ **
* @ 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)
* /
私人$产品;
/ **
* @ 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 $商业;
}
Sector.php
class Sector
{
...
/ **
* @ ORM\OneToMany(targetEntity =Product,mappedBy =sector)
* /
private $ products;
}
使用默认的symfony序列化程序时,会收到以下错误:
message:已检测到循环引用(配置限制:
1),class:Symfony \\Component\Serializer\Exception\CircularReferenceException
而当使用JMSSerializer时,当我转到相应的页面控制器,页面从不完成加载。同时在dev.log文件中,每秒都会添加新的Doctrine.debug条目,其中包含对我的数据库的请求。
$ normalizers-> setCircularReferenceHandler(function($ object){
return $ object-> getId();
});
如果您的objectNormalizer()
它完全适用于我
I'm using FOSRestBundle in a Symfony project. When it I try to handle a view, it fails during the serialization of my data with the Symfony serializer as well as with the JMSSerializer.
This is the method rendering the response:
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);
And these are my entities:
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;
}
When using the default symfony serializer, I get the following error:
"message":"A circular reference has been detected (configured limit: 1).","class":"Symfony\Component\Serializer\Exception\CircularReferenceException"
And when using the JMSSerializer, when I go to the corresponding page of the controller, the page just never finishes loading. At the same time in the dev.log file new Doctrine.debug entries with requests to my DB are added every second.
$normalizers->setCircularReferenceHandler(function ($object) {
return $object->getId();
});
Just add it after you make the instance if your objectNormalizer() it worl perfectly for me
这篇关于Symfony 3.0.4在使用FOSRestBundle进行序列化时检测到圆形参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!