如何通过doctrine2获得“NOT LIKE”的结果

时间:2016-05-10 04:47:01

标签: php symfony doctrine-orm

我正在尝试获取角色不是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();
}

1 个答案:

答案 0 :(得分:2)

您可以使用 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();
    }