10

一般来说,我是 Symfony2 的新手。这个问题与 Doctrine 和 FOSUserBundle 相关。

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

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

尝试更新缓存或删除时给我以下错误:

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

这里有什么问题?什么是“使用声明”?

4

2 回答 2

48

您忘记@ORM\在注释中添加前缀:

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

应该

/**
 * @ORM\ManyToMany(targetEntity="User", mappedBy="hasAccessToMe")
 **/
于 2013-02-25T14:05:24.297 回答
3

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

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

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

    // ...
}
于 2013-02-25T16:16:57.527 回答