2011-12-01 73 views
0

我正在研究一個CakePHP應用程序,該應用程序是應用程序--如在申請大學的應用程序中。它由一系列存儲在數據庫表中的表單以及一些管理工具組成。CakePHP:查詢缺少相關記錄

將所有表綁在一起是一個應用程序表,它在應用程序上存儲元數據。所有構成未來學生應用程序的各種表都屬於應用程序表 - 換句話說,它們共享一對一的關係。

該應用程序允許人們在完成它的過程中走開,並在稍後再回來。因此,管理界面隨着不完整的應用程序而變得混亂。界面的用戶只希望看到完成到某一點的不完整的應用程序。他們想要的信息來自某個表格中的必填表格字段,因此如果某人完成了該步驟,所需的信息將會出現。

這一切都是說我需要一種方法來過濾掉與applications關聯的表格中沒有記錄的應用程序。簡單的做法是在從數據庫返回後將其過濾出來,實際上,這是我第一次嘗試過。但是,我不只是查詢,我正在使用paginator,因此在返回結果集後會過濾分頁。我需要能夠在$paginate配置數組的conditions參數中將其過濾掉。

下面是相關的控制器代碼,包括當前的過濾機制,它的工作原理,但與分頁食堂:

var $paginate = array(
     'limit'=>100, 
     // We want apps that are complete at the top of the list, ordered by the date they were finished. 
     // We want to order incomplete apps by dateStarted because they don't have a dateFinished yet. 
     'order'=>array('stepsCompleted'=>'desc', 'dateFinished'=>'desc', 'dateStarted'=>'desc'), 
    ); 

function admin_index() { 
    $this->Nsapp->recursive = 0; 
    $this->Nsapp->unbindModel(array('hasOne'=>array('Essay', 'Religion', 'SpecialNeed')), false); 
    $nsapps = $this->paginate('Nsapp'); 
    // Filter out the apps that are too incomplete to do anything about 
    foreach ($nsapps as $key => $nsapp) { 
     // This fairly horrible conditional says, "If they provided us with at least one name and an email, do not filter them out" 
     if ((empty($nsapp['Person']['name1']) && 
      empty($nsapp['Person']['name2']) && 
      empty($nsapp['Person']['surName'])) || 
      empty($nsapp['Person']['email'])) 
     { 
      unset($nsapps[$key]); 
     } 
    } 

    $this->set('nsapps', $nsapps); 
} 

所以,總結並重申:我目前的分頁查詢返回空字段的數組用於沒有記錄的關聯表。我需要一種方法來刪除缺少某個關聯記錄的結果,而不是通過後期過濾(因爲分頁混亂),但是使用$paginate配置數組中的條件參數,但我不知道如何查詢缺席的記錄。

在此先感謝您的幫助!

+0

也許某種形式的左外連接?我建議僅僅在SQL上工作 - 甚至將你的問題改爲只包含SQL。一旦你有這個工作,它應該很容易移植到蛋糕。如果您遇到問題,請將其作爲新問題發佈。 – Dave

回答

0

嘗試在執行數據庫查詢時設置過濾器。

function admin_index() { 
    $this->Nsapp->recursive = 0; 
    $this->Nsapp->unbindModel(array('hasOne'=>array('Essay', 'Religion', 'SpecialNeed')), false); 
    $nsapps = $this->paginate('Nsapp'); 
    $this->paginate['Nsapp']['conditions'] = 
      array("not"=>array("Person.name1"=>null, "Person.name2"=>null)); 
    pr($this->paginate('Nsapp')); 

    $this->set('nsapps', $this->paginate('Nsapp')); 
}