doujiao1984 2016-02-02 13:05
浏览 14
已采纳

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

I'm trying to make a join in my repository class (Symfony 3 with Doctrine).

This is how it looks like:

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

And I've got this error:

[Semantical Error] line 0, col 49 near 'user_role ur': Error: Class 'user_role' is not defined.

There are two entity classes defined - User and Role. And Role is a property of User (ManyToMany).

There is also a joining table - user_role - do I need to create a class for every joining table even if I don't need it for my own purposes?

p.s. I know this error can be already found on Stackoverflow, but people having this issue probably have different problems.

Update:

Here's the User entity class (shortened):

/**
 * @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;
    }



}
  • 写回答

1条回答 默认 最新

  • dqoqnmb163241 2016-02-02 13:27
    关注

    I doctrine, all joined objects must be entities.

    But you can map a many-to-many relationship between the entities User and Role, using user_role as a linkage table

    In user table you might have a property $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;
    

    and a property $users in role table

        /**
    * @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;
    

    I've not tested this code. Surely It gonna need some tweeking. You also need to created the getters and setters for these properties.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程