我正在尝试获取角色不是ROLE_ADMIN_USER
的结果数。为了做到这一点,我在教义2中写了以下函数。但它并没有过滤结果。你知道它应该是什么样子吗?
public function getApprovedUserSearchNumber($searchQuery)
{
return $this->getEntityManager()->createQueryBuilder('u')
->select('COUNT(u)')
->from('AppBundle:User', 'u')
->andwhere('u.username LIKE :query ')
->andwhere('u.roles !LIKE :role ')
->setParameter('query', '%' . $searchQuery . '%')
->setParameter('role','%ROLE_ADMIN_USER%')
->andwhere("u.adminApproved= 'Yes'")
->getQuery()
->getSingleScalarResult();
}
您可以使用NOT LIKE
public function getApprovedUserSearchNumber($searchQuery)
{
return $this->getEntityManager()->createQueryBuilder('u')
->select('COUNT(u)')
->from('AppBundle:User', 'u')
->andwhere('u.username LIKE :query ')
->andwhere('u.roles NOT LIKE :role ')
->setParameter('query', '%' . $searchQuery . '%')
->setParameter('role','%ROLE_ADMIN_USER%')
->andwhere("u.adminApproved= 'Yes'")
->getQuery()
->getSingleScalarResult();
}