【发布时间】:2017-02-26 15:35:54
【问题描述】:
我正在尝试使用 Doctrine 2 在 Symfony 3 中使用 4 个实体,但是当我想序列化 Account 实体时遇到循环引用异常:
检测到循环引用(配置限制:1)。
我在实体中选择了双向关系,架构是这样的:
- Account [1] ---- [0..*] AccountSheet
- AccountSheet [1] ---- [0..*] Operation
- Operation [0..*] ---- [1] Category
这里是实体(为了清楚起见,做了一些清洁):
src\AppBundle\Entity\Account.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use AppBundle\Entity\AbstractGenericEntity;
/**
* @ORM\Entity()
* @ORM\Table(name="accounts",
* uniqueConstraints={@ORM\UniqueConstraint(name="accounts_name_unique",columns={"name"})})
*/
class Account extends AbstractGenericEntity{
/**
* @ORM\OneToMany(targetEntity="AccountSheet", mappedBy="account")
* @var AccountSheet[]
*/
protected $accountSheets;
public function __construct($name = null, $description = null){
$this->accountSheets = new ArrayCollection();
$this->name = $name;
$this->description = $description;
}
}
src\AppBundle\Entity\AccountSheet.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use AppBundle\Entity\AbstractGenericEntity;
/**
* @ORM\Entity()
* @ORM\Table(name="accounts_sheets",
* uniqueConstraints={@ORM\UniqueConstraint(name="accountsheet_account_unique", columns={"name", "account_id"})})
* @ORM\HasLifecycleCallbacks
*/
class AccountSheet extends AbstractGenericEntity{
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Account", inversedBy="accountSheets")
* @var Account
*/
protected $account;
/**
* @ORM\OneToMany(targetEntity="Operation", mappedBy="accountSheet")
* @var Operation[]
*/
protected $operations;
public function __construct($name = null){
$this->operations = new ArrayCollection();
$this->name = $name;
}
}
src\AppBundle\Entity\Operation.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\AbstractGenericEntity;
/**
* @ORM\Entity()
* @ORM\Table(name="operations")
*/
class Operation extends AbstractGenericEntity{
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\AccountSheet", inversedBy="operations")
* @ORM\JoinColumn(nullable=false)
* @var AccountSheet
*/
protected $accountSheet;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Category", inversedBy="operations")
* @var Category
*/
protected $category;
public function __construct($type = null, $label = null, $montant = null, $comment = null){
$this->label = $label;
$this->type = $type;
$this->comment = $comment;
$this->montant = $montant;
}
}
src\AppBundle\Entity\Category.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use AppBundle\Entity\AbstractGenericEntity;
/**
* @ORM\Entity()
* @ORM\Table(name="categories")
*/
class Category extends AbstractGenericEntity{
/**
* @ORM\Column(type="string")
*/
protected $label;
/**
* @ORM\Column(type="string")
*/
protected $description;
/**
* @ORM\OneToMany(targetEntity="Operation", mappedBy="category")
* @var Operation[]
*/
protected $operations;
public function __construct($name = null){
$this->operations = new ArrayCollection();
$this->name = $name;
}
}
我猜它在 Operation 实体上,其中 AccountSheet 再次被引用。双向 on 操作并不是真正需要的。
我该如何重新安排呢?
谢谢!
【问题讨论】:
-
The bi-directional on operation is not really needed.所以不要使用它:) 你可以使用单向 -
@AnomalySmith 你的构造函数是什么样的?
-
@MaxLipsky 由于文档指定关系一对多必须是双向的,我想我别无选择? docs.doctrine-project.org/projects/doctrine-orm/en/latest/….
-
@AlphonseD。我已更新以添加实体构造函数(继承的抽象在其构造函数中没有任何代码逻辑)
-
@AnomalySmith 我只是想确定一下 :) 如果你想保持这种双向关系,我想你可以使用 attribute groups 或 ignoring attributes
标签: php doctrine symfony circular-reference