How to use LIKE clause in DQL?

Viewed 276

I'm trying to create a query using the LIKE clause in Doctrine Query Language.

My query is as following:

public function findByName($keyword){
    $em = $this->getEntityManager();
    $consulta = $em->createQuery('SELECT o FROM AppBundle:Items o WHERE o.name LIKE :keyword');
    $consulta->setParameter('keyword', '%'.$keyword.'%');
    return $consulta->getResult();
}

I'm getting the next errors:

Notice: Trying to access array offset on value of type null

While if I do the same query but instead of LIKE i simply use =, the query executes correctly and I get the results. What could I be doing wrong here?

Thanks for the help.

Debug info:

object(Doctrine\ORM\Query)#901 (24) { ["_state":"Doctrine\ORM\Query":private]=> int(2) ["_parsedTypes":"Doctrine\ORM\Query":private]=> array(0) { } ["_dql":"Doctrine\ORM\Query":private]=> string(57) "SELECT o FROM AppBundle:Coche o WHERE o.modelo = :keyword" ["_parserResult":"Doctrine\ORM\Query":private]=> NULL ["_firstResult":"Doctrine\ORM\Query":private]=> NULL ["_maxResults":"Doctrine\ORM\Query":private]=> NULL ["_queryCache":"Doctrine\ORM\Query":private]=> NULL ["_expireQueryCache":"Doctrine\ORM\Query":private]=> bool(false) ["_queryCacheTTL":"Doctrine\ORM\Query":private]=> NULL ["_useQueryCache":"Doctrine\ORM\Query":private]=> bool(true) ["parameters":protected]=> object(Doctrine\Common\Collections\ArrayCollection)#946 (1) { ["elements":"Doctrine\Common\Collections\ArrayCollection":private]=> array(1) { [0]=> object(Doctrine\ORM\Query\Parameter)#913 (3) { ["name":"Doctrine\ORM\Query\Parameter":private]=> string(7) "keyword" ["value":"Doctrine\ORM\Query\Parameter":private]=> int(1) ["type":"Doctrine\ORM\Query\Parameter":private]=> string(7) "integer" } } } ["_resultSetMapping":protected]=> NULL ["_em":protected]=> object(Doctrine\ORM\EntityManager)#740 (11) { ["config":"Doctrine\ORM\EntityManager":private]=> object(Doctrine\ORM\Configuration)#564 (1) { ["_attributes":protected]=> array(14) { ["entityNamespaces"]=> array(1) { ["AppBundle"]=> string(16) "AppBundle\Entity" } ["metadataCacheImpl"]=> object(Doctrine\Common\Cache\ArrayCache)#566 (6) { ["data":"Doctrine\Common\Cache\ArrayCache":private]=> array(0) { } ["hitsCount":"Doctrine\Common\Cache\ArrayCache":private]=> int(0) ["missesCount":"Doctrine\Common\Cache\ArrayCache":private]=> int(0) ["upTime":"Doctrine\Common\Cache\ArrayCache":private]=> int(1598445780) ["namespace":"Doctrine\Common\Cache\CacheProvider":private]=> string(79) "sf_orm_default_061d80f6e71229c042b639f23634872f7045193d957ba060c640bf0458feeae2" ["namespaceVersion":"Doctrine\Common\Cache\CacheProvider":private]=> NULL } ["queryCacheImpl"]=>

    public function LikeExpression()
    {
        $stringExpr = $this->StringExpression();
        $not = false;
        if ($this->lexer->isNextToken(Lexer::T_NOT)) {
            $this->match(Lexer::T_NOT);
            $not = true;
        }
        $this->match(Lexer::T_LIKE);
        if ($this->lexer->isNextToken(Lexer::T_INPUT_PARAMETER)) {
            $this->match(Lexer::T_INPUT_PARAMETER);
            $stringPattern = new AST\InputParameter($this->lexer->token['value']);
        } else {
            $stringPattern = $this->StringPrimary();
        }
        $escapeChar = null;

//////// Highlighted error area//////////
        if ($this->lexer->lookahead['type'] === Lexer::T_ESCAPE) {
            $this->match(Lexer::T_ESCAPE);
            $this->match(Lexer::T_STRING);
            $escapeChar = new AST\Literal(AST\Literal::STRING, $this->lexer->token['value']);
        }
////////////////////////////////////////



        $likeExpr = new AST\LikeExpression($stringExpr, $stringPattern, $escapeChar);
        $likeExpr->not = $not;
        return $likeExpr;
    }
0 Answers
Related