Symfony2 - Doctrine and FOSUserBundle - wrong annotations

13,675

Solution 1

You forgot to add the @ORM\ prefix in your annotations:

/**
 * @ManyToMany(targetEntity="User", mappedBy="hasAccessToMe")
 **/

should be

/**
 * @ORM\ManyToMany(targetEntity="User", mappedBy="hasAccessToMe")
 **/

Solution 2

You could also import each annotation individually — the way I prefer:

use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\ManyToMany;
// ...

/**
 * @Entity
 */
class User
{
    /**
     * @ManyToMany(targetEntity="Thing")
     */
    private $things;

    // ...
}
Share:
13,675
Piddien
Author by

Piddien

Updated on June 19, 2022

Comments

  • Piddien
    Piddien almost 2 years

    I am new to Symfony2 in general. This issue relates to Doctrine and FOSUserBundle though.

    I have the following User.php Entity created based on FOSUserBundle and a self-referencing many-to many.

    <?php
    
    namespace Pan100\MoodLogBundle\Entity;
    
    use FOS\UserBundle\Entity\User as BaseUser;
    use Doctrine\ORM\Mapping as ORM;
    
        /**
     * @ORM\Entity
     * @ORM\Table(name="fos_user")
     */
    class User extends BaseUser
    {
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    
    
    /**
     * @ManyToMany(targetEntity="User", mappedBy="hasAccessToMe")
     **/
    protected $hasAccessTo;
    
    /**
     * @ManyToMany(targetEntity="User", inversedBy="hasAccessTo")
     * @JoinTable(name="access",
     *      joinColumns={@JoinColumn(name="id", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="accessor_id", referencedColumnName="id")}
     *      )
     **/
    private $hasAccessToMe;    
    
    public function __construct()
    {
        parent::__construct();
            $this->hasAccessTo = new \Doctrine\Common\Collections\ArrayCollection();
            $this->hasAccessToMe = new \Doctrine\Common\Collections\ArrayCollection();
    }
    }
    

    Gives me the following error when attempting to update cache or drop:

    [Doctrine\Common\Annotations\AnnotationException]                           
    [Semantical Error] The annotation "@ManyToMany" in property Pan100\MoodLog  
    Bundle\Entity\User::$hasAccessTo was never imported. Did you maybe forget   
    to add a "use" statement for this annotation?
    

    What is wrong here? And what is a "use statement"?

  • Piddien
    Piddien about 11 years
    success! since I use it as ORM I have to put ORM\ in front of all the annotations.