Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The association X refers to the inverse side field Y which is not defined as association

I have two entities:

Team:

class Teams
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer", name="id")
     * @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="team")
     *
     */
    private $id;

User:

class User implements UserInterface, \Serializable
{
    /**
     * @var int
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Teams", inversedBy="id")
     * @ORM\JoinColumn(name="team_id", referencedColumnName="id")
     */
    private $team;

One team have many users

And I've got these errors:

The association App\Entity\User#team refers to the inverse side field App\Entity\Teams#id which is not defined as association.

The association App\Entity\User#team refers to the inverse side field App\Entity\Teams#id which does not exist.

I don't know where I'm wrong... Anyone know ?

Regards

like image 747
FindL Avatar asked Nov 05 '25 05:11

FindL


1 Answers

I'm bit confused of what you want to do.

If you want unidirectional relation from user side then remove

 * @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="team")

this code.

And user should have only

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Teams")
 */
private $teams;

On the other hand if you want bidirectional relation then add property user on teams entity.

It doesn't work for you because the mapping you defined is on $id and it should be on the property

Bidirectional way:

class Teams
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer", name="id")
     *
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="teams")
     * @ORM\JoinColumn(name="team_id", referencedColumnName="id")
     */
    private $user;

class User implements UserInterface, \Serializable
{
    /**
     * @var int
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Teams", mappedBy="user")
     */
    private $teams;

It's good practice to name entities with singular name so perhaps you'd like to change Teams to Team entity.

Check also http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-one-unidirectional

like image 88
Robert Avatar answered Nov 06 '25 21:11

Robert