1

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.'%')
ndm
  • 59,784
  • 9
  • 71
  • 110
Annabelle
  • 734
  • 9
  • 23

1 Answers1

2

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

ndm
  • 59,784
  • 9
  • 71
  • 110