Sujit Sujit - 6 months ago 21
PHP Question

How to get results of "NOT LIKE" via doctrine2

I am trying to get the number of results where roles are not

ROLE_ADMIN_USER
. To do that I wrote following function in doctrine 2. But its not filtering the results. Any idea what it should be 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 !LIKE :role ')
->setParameter('query', '%' . $searchQuery . '%')
->setParameter('role','%ROLE_ADMIN_USER%')
->andwhere("u.adminApproved= 'Yes'")
->getQuery()
->getSingleScalarResult();
}


Thanks in advance.

Answer

you can use 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();
    }