我有一个带连接的现有查询:
public function findClientsAndTheirUsers($user_id, $search) {
$query = $this->getEntityManager()->createQueryBuilder()
->select('c.client_id, c.title, u.user_id, u.email')
->from('Application\Entity\User', 'u')
->innerJoin('u.clients', 'c')
->innerJoin('c.users', 'cu')
->andWhere("cu.user_id = {$user_id}")
->groupBy('c.client_id')
->orderBy('c.title, u.email', 'ASC');
return $query->getQuery()->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
}
搁在这些桌子上:
client
| client_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| title | tinytext | NO | | NULL | |
user
| user_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| email | varchar(255) | NO | UNI | NULL | |
user_has_client
| user_id | int(10) unsigned | NO | PRI | NULL | |
| client_id | int(10) unsigned | NO | PRI | NULL | |
使用这些实体:
Client
/**
* @ORM\ManyToMany(targetEntity="User", inversedBy="clients")
* @ORM\JoinTable(name="user_has_client",
* joinColumns={@ORM\JoinColumn(name="client_id", referencedColumnName="client_id")},
* inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="user_id")}
* )
*/
protected $users;
User
/**
* @ORM\ManyToMany(targetEntity="Client", mappedBy="users")
*/
protected $clients;
UserHasClient
/**
* @ORM\Id
* @ORM\Column(type="integer", options={"unsigned"=true})
*/
protected $user_id;
/**
* @ORM\Id
* @ORM\Column(type="integer", options={"unsigned"=true})
*/
protected $client_id;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="userHasClients")
* @ORM\JoinColumn(name="user_id", referencedColumnName="user_id", nullable=false)
*/
protected $user;
/**
* @ORM\ManyToOne(targetEntity="Client", inversedBy="userHasClients")
* @ORM\JoinColumn(name="client_id", referencedColumnName="client_id", nullable=false)
*/
protected $client;
现在,我刚刚在roles
表中添加了user_has_client
列:
| user_id | int(10) unsigned | NO | PRI | NULL | |
| client_id | int(10) unsigned | NO | PRI | NULL | |
| roles | tinytext | YES | | NULL | |
我想更新查询,以便它只返回那些具有admin
角色的用户。
答案 0 :(得分:0)
我认为你需要这样的东西(未经测试,DQL不是我最强的观点):
public function findClientsAndTheirUsers($user_id, $search)
{
$query = $this->getEntityManager()->createQueryBuilder()
->select('c.client_id, c.title, u.user_id, u.email')
->from('Application\Entity\User', 'u')
->innerJoin('u.userHasClients', 'uhc')
->innerJoin('uhc.client', 'c')
->andWhere("u.user_id = {$user_id}")
->andWhere("uhc.roles = {$role}")
->groupBy('c.client_id')
->orderBy('c.title, u.email', 'ASC');
return $query->getQuery()->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
}
您需要在DQL中使用ManyToOne和OneToMany以及UserHasClient实体来获取角色。
我希望这能指出你正确的方向。
答案 1 :(得分:0)
试试这个,希望它会有所帮助 -
public function findClientsAndTheirUsers($user_id, $search)
{
$query = $this->getEntityManager()->createQueryBuilder()
->select('c.client_id, c.title, u.user_id, u.email')
->from('Application\Entity\User', 'u')
->innerJoin('u.userHasClients', 'uhc')
->innerJoin('uhc.client', 'c')
->where("u.user_id = {$user_id}")
->andWhere("uhc.roles = :role")->setParameter('role', $role)
->groupBy('c.client_id')
->orderBy('c.title, u.email', 'ASC');
return $query->getQuery()->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
}
如果您仍然遇到问题,请告诉我。
答案 2 :(得分:0)
感谢Gautam Rai的回答,我可以在我的User类中编写缺少的映射:
/**
* @ORM\OneToMany(targetEntity="UserHasClient", mappedBy="user")
* @ORM\JoinColumn(name="user_id", referencedColumnName="user_id", nullable=true)
*/
protected $userHasClients;
public function __construct()
{
$this->userHasClients = new ArrayCollection();
}
/**
* Add UserHasClient entity to collection (one to many).
*
* @param \Application\Entity\UserHasClient $userHasClient
* @return \Application\Entity\User
*/
public function addUserHasClient(UserHasClient $userHasClient)
{
$this->userHasClients[] = $userHasClient;
return $this;
}
/**
* Get UserHasClient entity collection (one to many).
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getUserHasClients()
{
return $this->userHasClients;
}
然后可以将实体从用户遍历到其角色和客户端:
->from('Application\Entity\User', 'u')
->innerJoin('u.userHasClients', 'uhc')
->innerJoin('uhc.client', 'c')
->andWhere("uhc.roles like :role")
->setParameter('role', '%' . $role . '%')