Search code examples
symfonyone-to-manymany-to-one

Doctrine. One to many. How to write annotations?


I have a OneToMany relation between two tables 'user' and 'profil' (a user has one only profile, and a profile can be asseigned to many users) I'm getting this error whenever I try to update the schema in doctrine console.

here is my two entities :

    <?php

namespace CNAM\CMSBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping\ManyToOne;

/**
 * user
 *
 * @ORM\Table(name="user")
 * @ORM\Entity
 */
class user
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="password", type="string", length=40)
     */
    private $password;


    public function __construct()
    {
    }

    /**
     * @var boolean
     *
     * @ORM\Column(name="etat", type="boolean")
     */
    private $etat;

    /**
     * @var profil $profil
     *
     * @ORMManyToOne(targetEntity="profil", inversedBy="users", cascade={"persist", "merge"})
     * @ORMJoinColumns({
     *  @ORMJoinColumn(name="profil", referencedColumnName="id")
     * })
     */
    private $profil;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Set id
     *
     * @param integer $id
     * @return user
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Set password
     *
     * @param string $password
     * @return user
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

    /**
     * Get password
     *
     * @return string 
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Set etat
     *
     * @param boolean $etat
     * @return user
     */
    public function setEtat($etat)
    {
        $this->etat = $etat;

        return $this;
    }

    /**
     * Get etat
     *
     * @return boolean 
     */
    public function getEtat()
    {
        return $this->etat;
    }
    /**
     * Get profil
     *
     * @return integer
     */
    public function getProfil()
    {
        return $this->profil;
    }
    /**
     * Set profil
     *
     * @param integer $profil
     * @return user
     */
    public function setProfil($profil)
    {
        $this->profil = $profil;

        return $this;
    }

    public function getUsername()
    {
        return $this->id;
    }
    public function getRoles()
    {
        return array('ROLE_USER');
    }

    public function getSalt()
    {
        return null;
    }

    public function eraseCredentials()
    {

    }

    public function equals(UserInterface $user)
    {
        return $user->getId() == $this->getId();
    }
}

    <?php

namespace CNAM\CMSBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping\OneToMany;

/**
 * profil
 *
 * @ORM\Table(name="profil")
 * @ORM\Entity
 */
class profil
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="libelle", type="string", length=20)
     */
    private $libelle;

    /**
     * @var ArrayCollection $users
     *
     * @ORMOneToMany(targetEntity="user", mappedBy="profil", cascade={"persist", "remove", "merge"})
     */
    private $users;

    public function __construct()
    {

    }


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set libelle
     *
     * @param string $libelle
     * @return profil
     */
    public function setLibelle($libelle)
    {
        $this->libelle = $libelle;

        return $this;
    }

    /**
     * Get libelle
     *
     * @return string 
     */
    public function getLibelle()
    {
        return $this->libelle;
    }

    /**
     * Set id
     *
     * @param integer $id
     * @return profil
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    public function __toString() {
        return $this->libelle;
    }
}

Solution

    1. You have syntax mistake. @ORMManyToOne - this is mistake.

      @ORM\ManyToOne(targetEntity="Profil", inversedBy="users") 
      

      Above will be correct. And everywhere you use ORM You need to write ORM\something. ORM - is just a namespace, or simply path to the ORM classes in doctrine vendor directory!

    2. Every class name should start with capital letter. It is a wright way! So you should write class User, but not class user!

    3. You didn't show in Profil class, that you have a lot of users to each profil. the users field will be of array collection type (simple array but with useful methods.) So in Profil class in __construct() method:

      public function __construct()
      {
          $this->users = new ArrayCollection();
      }
      
    4. You have One-To-Many, Bidirectional relation. You can say :

      • Every (ONE) profile can have a lot of (MANY) users, which use it (One-To-Many)
      • Every user can use only one exact profile

    So try this

    // Class Profil
        /**
         * Bidirectional - One-To-Many (INVERSED SIDE)
         * One Profile can have many users, which use it
         *
         * @var ArrayCollection $users
         *
         * @ORM\OneToMany(targetEntity="Path-to-your-user-class\Users", mappedBy="profil", cascade={"persist", "remove"}, orphanRemoval=true)
         */
        private $users;
    
        public function __construct()
        {
            $this->users = new ArrayCollection();
        }
    
    
    // Class user
        /**
         * Bidirectional - One-To-Many (OWNER SIDE)
         * One user have only one profile (or profil)
         *
         * @var Profil $profil
         *
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Profil", inversedBy="users")
         * @ORM\JoinColumn(name="profil_id", referencedColumnName="id")
         */
        private $profil;