簡體   English   中英

復雜的分頁與條件不在cakephp 2

[英]Complex paginate with NOT IN condition cakephp 2

我有一個小問題,以使我的查詢以正確的方式工作。

我的詢問應該為我提供實際學期中的所有講座,但僅僅是那些我還沒有參加的講座。 因此在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定義條件? 也許有人可以幫忙嗎? 對於我的英語感到抱歉,如果有任何錯誤:)

首先找到要在查詢中排除的ID列表,然后使用條件為'Lecture.id!='=> $ ids的ID列表。

假設您已將模型參與者連接到模型講座,則查詢可能如下所示。

//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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM