在 Doctrine 中,我是否需要创建一个类来做一个连接


In Doctrine, do I need do create a class only to make a join?

我正在尝试加入我的存储库类(Symfony 3 with Doctrine)。

这是它的样子:

public function findByRole($roles){
        $qb = $this->createQueryBuilder('u')
            ->join('user_role', 'ur', Join::ON, 'ur.id = u.customerId');
        $q = $qb->getQuery();
        $users = $q->getArrayResult();
        dump($users);
    }

我有这个错误:

[语义错误] 第 0 行,第 49 行靠近"user_role您的":错误:类 未定义"user_role"。

定义了两个实体类 - 用户和角色。角色是用户(ManyToMany)的属性。

还有一个连接表 - user_role - 我是否需要为每个连接表创建一个类,即使我不需要它用于我自己的目的?

附言我知道这个错误已经在Stackoverflow上找到,但是遇到这个问题的人可能有不同的问题。

更新:

下面是用户实体类(缩短):

/**
 * @ORM'Table(name="user")
 * @ORM'Entity(repositoryClass="AppBundle'Repository'UserRepository")
 */
class User implements AdvancedUserInterface, 'Serializable {
    /**
     * @ORM'Column(type="integer")
     * @ORM'Id
     * @ORM'GeneratedValue(strategy="AUTO")
     */
    private $id;
    //irrelevant code removed
    /**
     *
     * @ORM'ManyToMany(targetEntity="AppBundle'Entity'Role", cascade = {"persist"}, inversedBy="users")
    *  @ORM'JoinTable(name="user_role",
    *      joinColumns={@ORM'JoinColumn(name="user_id",
    * referencedColumnName="id")},
    *      inverseJoinColumns={@ORM'JoinColumn(name="role_id",
    * referencedColumnName="id")}
    *      )
     */
    private $roles;
//////////////////////////////////////////////////////////////////////
    public function __construct() {
        $this->roles = new ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId() {
        return $this->id;
    }
    ////////////////////////////////
    //roles
    /**
     * Add role
     * @param 'AppBundle'Entity'Role $role
     *
     * @return User
     */
    public function addRole('AppBundle'Entity'Role $role) {
        $this->roles[] = $role;
        return $this;
    }
    public function setRoles('AppBundle'Entity'Role $role){
        $this->addRole($role);
        return $this;
    }
    /**
     * Remove role
     * @param 'AppBundle'Entity'Role $role
     */
    public function removeRole('AppBundle'Entity'Role $role) {
        $this->roles->removeElement($role);
    }
    /**
     * I know it probably should return simply $this->roles, but the interface I'm implementing requires an array of strings... But everything works fine, except joining user+roles  
     * @return 'Doctrine'Common'Collections'Collection
     */
    public function getRoles() {
        return $this->roles->toArray();
    }
    /**
     * Get roles
     * @return 'Doctrine'Common'Collections'Collection
     */
    public function getRoleEntities() {
        return $this->roles;
    }

}

I 学说,所有连接的对象都必须是实体。

但是,您可以使用user_role作为链接表来映射实体用户和角色之间的多对多关系

在用户表中,您可能有一个属性$roles

/**
* @ManyToMany(targetEntity="Role", inversedBy="users")
* @JoinTable(name="user_role",
*      joinColumns={@JoinColumn(name="user_id",
* referencedColumnName="id")},
*      inverseJoinColumns={@JoinColumn(name="role_id",
* referencedColumnName="id")}
*      )
*/
private $roles;

和角色表中的属性$users

    /**
* @ManyToMany(targetEntity="User", inversedBy="roles")
* @JoinTable(name="user_role",
*      joinColumns={@JoinColumn(name="role_id",
* referencedColumnName="id")},
*      inverseJoinColumns={@JoinColumn(name="user_id",
* referencedColumnName="id")}
*      )
*/
private $users;

我还没有测试过这段代码。当然,它需要一些周转。您还需要为这些属性创建 getter 和 setter。