Search code examples
cakephpcakephp-3.0cakephp-3.xcakephp-3.6

CakePHP 3.x OR In Find Condition With Multiple Same Fields


I would like to query the database for

Plates.page_number => 1 OR Plates.page_number => Cover

I am attempting to use the following code, but I am not getting the results I am looking for because of a duplicate array key, how can I search the same field for two different values?

   $query = $this->Ledgers->find('all', array(
        'contain' => array(
            'Plates' => [
                'conditions' => [
                    'OR' => [
                        'Plates.plate_title' => 'Front Cover',
                        'Plates.page_number' => '1',
                        'Plates.page_number' => 'Cover' // Duplicate Array Key
                    ]
                ]
            ], 'Plates.PlateImages', 'Tribes'
        ),
        'conditions' => array(
            'Ledgers.disabled' => 'n', 'Ledgers.id IN' => $ledgerIds
        )
    ))->orderAsc('ledger_title');

Solution

  • Please try wrapping your conditions in separate arrays, eg:

    'OR' => [
        ['Plates.page_number' => '1'],
        ['Plates.page_number' => 'cover'],
        ...
    ]
    

    More info can be found in CakePHP docs:

    Query Builder -> Advanced Conditions