升级后与Doctrine的关联错误

时间:2019-04-24 22:47:39

标签: doctrine-orm doctrine

我正在努力将产品从Symfony 2.7升级到4.2(当前为3.4),并陷入一些现有的关联中。

  • 字段AppBundle \ Entity \ User#currentBillingAgreement在双向关系的拥有方,但是目标实体AppBundle \ Entity \ BillingAgreement#上指定的mappingBy关联不包含必需的'inversedBy'属性。
  • 如果关联AppBundle \ Entity \ User#currentBillingAgreement是一对一的,则相反侧的AppBundle \ Entity \ BillingAgreement#user也必须是一对一的。

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映射时,出现新错误:

  • 字段AppBundle \ Entity \ BillingAgreement#user在双向关系的拥有方,但是在目标实体AppBundle \ Entity \ User#上指定的mappingBy关联不包含必需的'inversedBy'属性。

,原始的2个错误仍然保留。

1 个答案:

答案 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;

参考