3
votes

I'm working on upgrading a product from Symfony 2.7 to 4.2 (currently at 3.4) and am getting stuck with some existing associations.

  • The field AppBundle\Entity\User#currentBillingAgreement is on the owning side of a bi-directional relationship, but the specified mappedBy association on the target-entity AppBundle\Entity\BillingAgreement# does not contain the required 'inversedBy' attribute.
  • If association AppBundle\Entity\User#currentBillingAgreement is one-to-one, then the inversed side AppBundle\Entity\BillingAgreement#user has to be one-to-one as well.

The User entity has these associations:

    /**
     * @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;

and BillingAgreement has this:

    /**
     * @var User
     * @ORM\ManyToOne(
     *     targetEntity="AppBundle\Entity\User",
     *     inversedBy="billingAgreements"
     * )
     * @ORM\JoinColumn(
     *     name="userID",
     *     referencedColumnName="userID",
     *     nullable=false
     * )
     */
    protected $user;

When I add a OneToOne mapping to BillingAgreement::$user (@ORM\OneToOne(targetEntity="AppBundle\Entity\User", inversedBy="currentBillingAgreement")), I get a new error:

  • The field AppBundle\Entity\BillingAgreement#user is on the owning side of a bi-directional relationship, but the specified mappedBy association on the target-entity AppBundle\Entity\User# does not contain the required 'inversedBy' attribute.

and the original 2 errors remain.

1

1 Answers

0
votes

You can make the OneToOne association unidirectional by removing inversedBy="user", from the annotation.

or

Use a different field for each association on BillingAgreement entity:

/**
 * @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;

and in User entity:

/**
 * @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;

References