We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to select from Query\Builder with LIKE %something% ?

Having Query\Builder and assigning dynamically parameters, I can't seem to figure out how to do LIKE selects.

// Loop params and assign them to query builder
$iteration = 0;
foreach($params as $param => $paramValue) {
            if ($iteration == 0) {
                $function = 'where';
            } else {
                $function = 'andWhere';
            }

            $equalitySign = ($equal == 'LIKE' ? '%' : '');
            $resultSet->$function($param . ' ' . $equal . ' :' . $param . ':', array($param => ($equalitySign . $paramValue . $equalitySign)));

            $iteration++;
}

This doesn't work.



3.2k
Accepted
answer

I'm currently doing this:

$querybuilder->where("column1 LIKE CONCAT('%', ?0, '%')", array($value));

This works too:

$querybuilder->where('name LIKE ?0', array("%{$name}%"));