我很好奇如何使用Symfony中的查询构建器构建一个学说查询,它可以基于相当标准化的权限连接表排除项目。
就我而言,
User
实体
Template
实体
userTemplatePermissionTable
在userTemplatePermissions实体中,我们具有以下属性:
[userToCheck
,template
,read
,write
,delete
而且我想要的东西等同于:
return $this->createQueryBuilder('t')
[where count of t.userDocumentPermissions > 0
where canRead = true, canWrite = true, canDelete = true
AND userDocumentpermissions.user = :user]
这最终是一个相当简单的左联接和排除检查设置,我在TemplateRespository.php
中总结了以下内容:>
public function findUserAuthorized($user, $read = null, $write = null, $delete = null)
{
$query = $this->createQueryBuilder('t')
->leftJoin('t.userTemplatePermissions', 'utp')
->where('utp.userToCheck = :user')
->setParameter('user',$user);
if ($read !== null) {
$query->andWhere('utp.read = :read')
->setParameter('read',$read);
}
if ($write !== null) {
$query->andWhere('utp.write = :write')
->setParameter('write',$write);
}
if ($delete !== null) {
$query->andWhere('utp.delete = :delete')
->setParameter('delete',$delete);
}
return $query->getQuery()->getResult();
}