我正在努力将产品从Symfony 2.7升级到4.2(当前为3.4),并陷入一些现有的关联中。
User
实体具有以下关联:
/**
* @var BillingAgreement
* @ORM\OneToOne(
* targetEntity="AppBundle\Entity\BillingAgreement",
* inversedBy="user",
* cascade={"persist"}
* )
* @ORM\JoinColumn(
* name="currentBillingAgreementID",
* referencedColumnName="billingAgreementID"
* )
*/
protected $currentBillingAgreement;
/**
* @var ArrayCollection
* @ORM\OneToMany(
* targetEntity="AppBundle\Entity\BillingAgreement",
* mappedBy="user",
* cascade={"persist"}
* )
* @Serializer\Exclude()
*/
protected $billingAgreements;
和BillingAgreement具有以下内容:
/**
* @var User
* @ORM\ManyToOne(
* targetEntity="AppBundle\Entity\User",
* inversedBy="billingAgreements"
* )
* @ORM\JoinColumn(
* name="userID",
* referencedColumnName="userID",
* nullable=false
* )
*/
protected $user;
当我向BillingAgreement::$user
(@ORM\OneToOne(targetEntity="AppBundle\Entity\User", inversedBy="currentBillingAgreement")
)添加OneToOne映射时,出现新错误:
,原始的2个错误仍然保留。
答案 0 :(得分:0)
您可以通过从注释中删除inversedBy="user",
来建立OneToOne关联unidirectional。
或
为BillingAgreement实体上的每个关联使用不同字段:
/**
* @var User
* @ORM\ManyToOne(
* targetEntity="AppBundle\Entity\User",
* inversedBy="billingAgreements"
* )
* @ORM\JoinColumn(
* name="userID",
* referencedColumnName="userID",
* nullable=false
* )
*/
protected $user;
/**
* @var User
* @ORM\OneToOne(targetEntity="AppBundle\Entity\User", inversedBy="currentBillingAgreement")
*/
protected $singleUser;
并在用户实体中:
/**
* @var BillingAgreement
* @ORM\OneToOne(
* targetEntity="AppBundle\Entity\BillingAgreement",
* inversedBy="singleUser",
* cascade={"persist"}
* )
* @ORM\JoinColumn(
* name="currentBillingAgreementID",
* referencedColumnName="billingAgreementID"
* )
*/
protected $currentBillingAgreement;
/**
* @var ArrayCollection
* @ORM\OneToMany(
* targetEntity="AppBundle\Entity\BillingAgreement",
* mappedBy="user",
* cascade={"persist"}
* )
* @Serializer\Exclude()
*/
protected $billingAgreements;
参考