2016-03-02 112 views
0

我有一個關於cakephp中的搜索過濾器的問題。沒有使我的問題複雜化,下面是我想要的結構....通過關聯模型的搜索過濾器獲取數據

1)我有一個項目表。

2)另一個是project_funder_names表,它與projects表關聯。 project_idproject_funder_names表中。我製作了project_funder_names表格,因爲我需要一個項目的多個資助者名稱,這就是爲什麼我製作了這張表格。

3)現在的主要觀點是我想如果我在搜索過濾器中搜索多個出資者,並且在複選框中出現下拉列表,我將根據這些值獲取項目詳細信息。所以怎麼會發生。

,這裏是我的CakePHP找到所有查詢....

$project_info = $this->Project->find('all', array(
'conditions' => 
    array(
     'Project.status' => 1, 
     'OR' => array($search)), 
     'fields' => array('id', 'title', 'short_description', 'budget_allocation', 'currency', 'total_comments', 'published_date'), 
     'contain' => array(
       'ProjectFunderName' => array(
        'conditions' => array($search_funder)), 
       'Currency' => array('currency_symbol'), 
       'ProjectBookmark' => array('project_id', 'user_id') 
     ) 
    ) 
); 

問題是$search_funder

請幫我這個..謝謝。

回答

1

看起來您需要根據相關模型搜索結果。使用可容忍行爲的一個缺點是,如果您試圖將條件分配給相關模型,則無論如何都會檢索主模型。

在你想根據相關模型的條件檢索主記錄和關聯記錄的情況下,我建議你使用join。

$joins = array(
    array('table' => 'project_funder_names', 
    'alias' => 'ProjectFunderName', 
    'type' => 'LEFT', 
    'conditions' => array('ProjectFunderName.project_id = Project.id') 
    ), 
    array('table' => 'currencies', 
    'alias' => 'Currency', 
    'type' => 'LEFT', 
    // It's unclear how currencies is associated with the other tables. Use appropriate table join condition here 
    ), 
    array('table' => 'project_bookmarks', 
    'alias' => 'ProjectBookmark', 
    'type' => 'LEFT', 
    'conditions' => array('ProjectBookmark.project_id = Project.id') 
    ) 
) 
); 

$project_info = $this->Project->find('all', 
    array(
     "joins" => $joins, 
     "fields" => array(.........) // Specify all your desired fields here 
     "conditions" => array(....) // Specify all conditions for Project, ProjectFunderName models. 
    ) 
); 

希望這會有所幫助。

和平! xD

相關問題