doctrine2 jointable注释错误

时间:2016-02-08 18:21:11

标签: symfony doctrine-orm

我尝试从2个具有多种关系的实体创建一个连接表:

class Entity1
{
    /**
     * @ORM\ManyToMany(targetEntity="Orders")
     * @JoinTable(name="users_orders",
     *   joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
     *   inverseJoinColumns={@JoinColumn(name="order_id", referencedColumnName="id", unique=true)
     */
    private $orders;
}

但每次我尝试生成getter和setter时都会遇到同样的错误:

  

注释@JoinTable ...从未导入过。您是否忘记为此注释添加use语句?

那么如何添加use语句呢?谢谢你的帮助

2 个答案:

答案 0 :(得分:3)

您需要将@ORM\放在所有Doctrine注释的前面:

/**
 * @ORM\ManyToMany(targetEntity="Orders")
 * @ORM\JoinTable(name="users_orders",
 *   joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *   inverseJoinColumns={@ORM\JoinColumn(name="order_id", referencedColumnName="id", unique=true)
 */
private $orders;

答案 1 :(得分:3)

JoinTableJoinColumn

前添加ORM
/**
 * @ORM\ManyToMany(targetEntity="Orders")
 * @ORM\JoinTable(name="users_orders",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="order_id", referencedColumnName="id", unique=true)
 */

并在课程顶部添加'使用' -statement:

use Doctrine\ORM\Mapping as ORM;