我有一个小问题让我的查询以正确的方式工作。
我的查询应该给我在实际学期的所有讲座,但只是那些我尚未参与的讲座。所以在SQL中它将是:
SELECT * FROM lectures WHERE semester = $currentSemester AND lectures.id NOT IN (SELECT lectures_id FROM participaters WHERE user_id = $this->Auth->user('id'))
我已经得到的是以下内容:
$this->paginate = array(
'conditions' => array(
'Lecture.semester >=' => $currentSemester,
'not' => array(
'Lecture.id' => array(
),
),
),
'order' => array(
'Lecture.name' => 'asc',
),
'contain' => array(
'Participater',
),
);
$this->set('lectures', $this->paginate($this->Lecture));
如何使用user_id定义条件?也许有人可以帮忙吗?对不起我的英语,如果有任何错误:)
答案 0 :(得分:0)
首先查找要在查询中排除的ID列表,然后使用此列表的ID,其条件类似于' Lecture.id!=' => $ IDS。
假设您将模型参与者链接到模型讲座,则查询可以如下所示。
//retrieve the list of excluded ids
$excluded_ids = $this->Lecture->Participater->find('list',array(
'conditions' => array(
'Participater.user_id' => $this->Auth->user('id')
),
'fields' => array('lecture_id')
);
$this->paginate = array(
'conditions' => array(
'Lecture.semester >=' => $currentSemester,
'Lecture.id !=' => $excluded_ids,//put the excluded ids here
),
'order' => array(
'Lecture.name' => 'asc',
),
'contain' => array(
'Participater',
),
);
$this->set('lectures', $this->paginate($this->Lecture));