Annabelle
Annabelle

Reputation: 754

CakePHP 3 query builder with two like values

How can I modify my CakePHP 3 query builder so that it could have two "like" functions with values (of "OR" logic function)?

This works:

$exp->eq('b2','my_value')->like('b3','%'.$value.'%')

I would like something like this (it does not work):

$exp->eq('b2','my_value')->like('b3','%'.$valueB3.'%')->like('b4','%'.$valueB4.'%')

Upvotes: 1

Views: 149

Answers (1)

ndm
ndm

Reputation: 60463

Unless stated otherwise, the expression builder will combine statements via AND. If you want to use OR with the expression builder, use the or_() method, and pass the resulting new expression object to wherever you need it, like for example:

$exp
    ->eq('b2', 'my_value')
    ->add($exp->or_(
        function (\Cake\Database\Expression\QueryExpression $or) use ($valueB3, $valueB4) {
            return $or
                ->like('b3', '%' . $valueB3 . '%')
                ->like('b4', '%' . $valueB4 . '%');
        }
    ));

That would produce:

b2 = 'my_value' AND (b3 LIKE '%valueB3%' OR b4 LIKE '%valueB4%')

See also

Upvotes: 2

Related Questions