2013-02-25 127 views
9

我是Symfony2的新手。虽然这个问题与Doctrine和FOSUserBundle有关。Symfony2 - Doctrine和FOSUserBundle - 错误注释

我有以下User.php实体创建基于FOSU​​serBundle和自引用多对多。

[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? 

什么是错在这里:

<?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(); 
} 
} 

尝试更新缓存或下降时,给了我下面的错误?什么是“使用声明”?

回答

42

你忘了加上@ORM\前缀您的注释:

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

应该

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

成功!因为我使用它作为ORM,所以我必须将ORM \放在所有注释之前。 – Piddien 2013-02-25 14:13:44

3

你也可以单独导入每个注释 - 我喜欢的方式:

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

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

    // ... 
}