使用not not在cakephp中工作的数据获取数据

时间:2016-07-29 14:26:37

标签: cakephp

我想为下面的id不存在的所有行选择数据。我认为这会有效,但没有错误,下面的ID仍会出现在检索到的记录中。我正在使用cakephp 2.5

  $ids///

array(
(int) 0 => '14721',
(int) 1 => '14731',
(int) 2 => '14905',
(int) 3 => '15808',
(int) 4 => '15818',



 $test=$this->Lessonbak->find('all', array(
      'fields'=>array('id'),
     'recursive' => -1 ) );

 $ids=array();
 foreach(  $test as  $item):
     array_push($ids,$item['Lessonbak']['id']);

 endforeach;  

  $lessonstudent2=$this->LessonsStudent2->find('all', array(
         'conditions' => array(  "NOT" => array( 'LessonsStudent2.lesson_id' => $ids )),
         'recursive' => -1 ) ); 

1 个答案:

答案 0 :(得分:1)

您应该只使用子查询,而不是选择所有Lessonbak ID,将它们推送到数组,并在LessonsStudent2查询中使用它:

$dbo = $this->getDataSource();
$subquery = $dbo->buildStatement(
    array(
        'table' => 'lessonbak',
        'alias' => 'Lessonbak',
        'recursive' => -1,
        'fields' => array(
            'Lessonbak.id'
        )
    ),
    $this
)


$this->LessonsStudent2->find('all', array(
    'conditions' => array(
        'NOT' => array(
            'LessonsStudent2.lesson_id IN (' . $subquery . ')'
        )
    )
)