Symfony2 - Doctrine和FOSUserBundle - 错误的注释

时间:2022-10-16 13:36:53

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

我是Symfony2的新手。这个问题与Doctrine和FOSUserBundle有关。

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

我有以下基于FOSU​​serBundle创建的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();
}
}

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

这有什么不对?什么是“使用声明”?

2 个解决方案

#1


44  

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

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

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

should be

应该

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

#2


3  

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;

    // ...
}

#1


44  

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

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

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

should be

应该

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

#2


3  

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;

    // ...
}