CakePHP发现WHERE NOT EQUAL

时间:2015-06-11 12:30:14

标签: php cakephp frameworks

你能看到错误的家伙吗?我的AND条件被忽略了!我对这些数组感到非常沮丧..

 $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 个答案:

答案 0 :(得分:13)

您的条件必须为['Transaction.id' => $client_id, 'Transaction.name !=' => 'Facturation']conditions数组的多个条件被解释为' AND'条件。

所以你的查询看起来像: -

$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'
        ]
    ]
);

如果您有重复的条件数组键,则只需按and索引;在您的示例中并非如此,因为您有Transaction.idTransaction.name !=。无论如何,and索引需要是conditions数组中的索引,而不是兄弟。

答案 1 :(得分:3)

 $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')));

如果您希望该名称不应该是其中任何一个,那么您应该按如下方式编写查询:

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