php cake find()使用OR条件

时间:2017-03-29 06:20:29

标签: php cakephp

我需要在php cake中的if else条件中使用基本的“或”。

$oTourRecords =  $this->Record->find('all', array(
    'conditions' => array(
        'record.builder_name' => 'rey',
            'or' => array(
                'record.builder_name' => 'norbert',
            )
         )
     )
);

基本上我需要得到如果记录包含builder_name = rey或“norbert” 这个返回null。

3 个答案:

答案 0 :(得分:4)

尝试

$oTourRecords =  $this->Record->find('all', array(
            'conditions' => array(
                'OR' => array(
                        'record.builder_name' => 'norbert',
                        'record.builder_name' => 'rey',
                    )
            )
         ));

答案 1 :(得分:2)

您可以通过多种方式实现:

$oTourRecords =  $this->Record->findAllByBuilderNameOrBuilderName('rey', 'norbert');

$oTourRecords =  $this->Record->find('all', array(
        'conditions' => array(
            'OR' => array(
                    'record.builder_name' => 'norbert',
                    'record.builder_name' => 'rey',
                )
        )
     ));

$oTourRecords =  $this->Record->find('all', array(
        'conditions' => array('record.builder_name IN ' => array('norbert','rey'))
 ));

答案 2 :(得分:1)

  

您还可以使用IN检索相似类型的数据,而不是OR条件。

示例:

$oTourRecords =  $this->Record->find('all', array(
        'conditions' => array(
            'record.builder_name IN'=>array(
                 'rey',
                 'norbert'
             )
         )
     )
);