У меня есть небольшая проблема, чтобы мой запрос работал правильно.
Мой запрос должен дать мне все лекции в текущем семестре, но только те, в которых я еще не участвую. Так что в 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? Может быть, кто-то из вас может помочь? И извините за мой английский, если здесь есть какие-либо ошибки 🙂
Сначала найдите список идентификаторов, которые вы хотите исключить из запроса, а затем используйте этот список идентификаторов с условием, подобным ‘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));
Других решений пока нет …