Newscoop\Entity\Repository\UserRepository::searchUsers PHP Method

searchUsers() public method

Return Users if any of their searched attributes contain the searched term.
public searchUsers ( string $search, $countOnly, $offset, $limit, array $attributes = ["first_name", "last_name", "username"] ) : array
$search string
$attributes array
return array Newscoop\Entity\User
    public function searchUsers($search, $countOnly, $offset, $limit, $attributes = array("first_name", "last_name", "username"))
    {
        $keywords = explode(" ", $search);
        $qb = $this->getEntityManager()->createQueryBuilder();
        $qb->select('u')->from('Newscoop\\Entity\\User', 'u');
        $outerAnd = $qb->expr()->andx();
        for ($i = 0; $i < count($keywords); $i++) {
            $innerOr = $qb->expr()->orx();
            for ($j = 0; $j < count($attributes); $j++) {
                $innerOr->add($qb->expr()->like("u.{$attributes[$j]}", "'{$keywords[$i]}%'"));
            }
            $outerAnd->add($innerOr);
        }
        $outerAnd->add($qb->expr()->eq("u.status", User::STATUS_ACTIVE));
        $outerAnd->add($qb->expr()->eq("u.is_public", true));
        $qb->where($outerAnd);
        $qb->orderBy('u.last_name', 'ASC');
        $qb->addOrderBy('u.first_name', 'ASC');
        $qb->addOrderBy('u.id', 'DESC');
        $qb->setFirstResult($offset);
        $qb->setMaxResults($limit);
        return $qb->getQuery()->getResult();
    }