Search code examples
cakephpcakephp-2.x

How to add multiple conditions for a single column?


I have many sentences that I need filter for a same column:

'conditions' => array('Zona.nombre LIKE' => $buscar,
                      'Zona.nombre LIKE' => 'CUPONATIC%',
                      'Zona.nombre LIKE' => 'GROUPON%'
),

Solution

  • your question is not very clear but I suppose the problem is that you use multiple time the same array key

    You don't even mention the cakephp version but it seems cake2

    If I remember well the workaround for cake2 is putting every condition in a different array

    'conditions' => array(
         array('Zona.nombre LIKE' => $buscar),
         array('Zona.nombre LIKE' => 'CUPONATIC%'),
         array('Zona.nombre LIKE' => 'GROUPON%')
    ),
    

    edit: of course this way you'll have the 3 conditions joined in AND.

    It seems more logical to put them in OR so

    'conditions' => array(
        'OR' => array(
             array('Zona.nombre LIKE' => $buscar),
             array('Zona.nombre LIKE' => 'CUPONATIC%'),
             array('Zona.nombre LIKE' => 'GROUPON%')
        )
    ),