CakePHP find WHERE NOT EQUAL

21.7k views Asked by At

can you see the error guys ? my AND conditions is ignored! I'm getting so frustrated with those arrays..

 $transaction_query = $this->Transaction->find('all',
                [
                    'limit' => $countList,
                    'fields' => ['Transaction.client_id','Transaction.name','Transaction.created','Transaction.message_id','Transaction.credit'],
                    'conditions' => ['Transaction.id' => $client_id],
                    'AND' => ['Transaction.name !=' => 'Facturation']
                ]);
2

There are 2 answers

0
drmonkeyninja On BEST ANSWER

Your conditions need to be ['Transaction.id' => $client_id, 'Transaction.name !=' => 'Facturation']. Multiple conditions of the conditions array are interpreted as 'AND' conditions.

So your query would look like:-

$transaction_query = $this->Transaction->find('all',
    [
        'limit' => $countList,
        'fields' => [
            'Transaction.client_id',
            'Transaction.name',
            'Transaction.created',
            'Transaction.message_id',
            'Transaction.credit'
        ],
        'conditions' => [
            'Transaction.id' => $client_id, 
            'Transaction.name !=' => 'Facturation'
        ]
    ]
);

You only need to index by and if you have duplicate condition array keys; this is not the case in your example as you have Transaction.id and Transaction.name !=. Regardless, the and index would need to be an index inside the conditions array, not a sibling.

0
Deep Kakkar On
 $this->Transaction->find( 'all', array(
'limit' => $countList, 
'fields' => ['Transaction.client_id','Transaction.name','Transaction.created','Transaction.message_id','Transaction.credit'],
'conditions' => array("not" => array ( 'Transaction.id'  => $client_id)),
'AND' => array('not' => ('Transaction.name ' => 'Facturation')));

In case if you want that name should not be any one of them then you should write query as follows:

$this->Transaction->find('all', array('conditions' => array('NOT' => array('Transaction.name' => array('a','aa','aaa')))));