2011-09-20 43 views
0

類似this question - 但由於我的無能與CakePHP的,我似乎無法想出解決辦法。這是我的設置。CakePHP:使用查詢約束對相關hasMany字段進行分頁?

我有其中有許多Comments型號名稱User。我想要至少有一個Comment的隱形Users列表。我們的控制器的方法設置爲將records設置爲$this->paginate()而沒有任何值傳遞給它;相反,$paginate變種設置了條件和類似的東西。

class UsersController extends AppController { 
    var $name = 'Users'; 
    var $paginate; 

    function new_users() { 
     $this->User->recursive = 1; 
     $this->paginate['User']['contain'][] = 'Comment'; 
     $this->paginate['User']['order'] = 'User.DateCreated DESC'; 

     $conditions = array(); 
     // Get invisible users... 
     $conditions[] = array('User.Status = ?' => 'Invisible'); 
     $conditions[] = array('User.Enabled' => true); 
     // ...with at least one active comment 
     $conditions[] = array('Comment.Status' => 'Active'); 

     // Load these conditions and fire pagination to render the view 
     $this->paginate['User']['conditions'] = $conditions; 
     $this->set('records', $this->paginate()); 
    } 
} 

我對CakePHP很新,很多這些東西仍然在我的頭上。我只想要一個Users列表,其中包含活動的Comments。現在,此查詢結果爲0個用戶。如果我參加了關於Comment.Status行,查詢經過,但我得到的所有無形Users,無論他們是否有意見的。

到處都在線,我看到paginate()電話,事情已經傳遞給它(如paginate('User', array('Comment.Status' => 'Active')),我已經嘗試過,並沒有任何網絡)。

編輯:我有下面的代碼,雖然更乾淨,但仍給我相同的結果。

$this->paginate = array(
    'conditions' => array(
    'User.Status' => 'Invisible', 
    'User.Enabled' => true, 
), 
    'contain' => array(
    'Comment' => array(
     'conditions' => array(
     'Comment.Status' => 'Active', 
    ) 
    ) 
), 
    'recursive' => 1, 
    'order' => 'User.DateCreated DESC', 
); 
$data = $this->paginate('User'); 

回答

1

我通常使用分頁變化對即時嘿嘿。這裏是一些示例代碼。

$this->paginate = array(
    'conditions' => array('Recipe.title LIKE' => 'a%'), 
    'limit' => 10 ); 
$data = $this->paginate('Recipe'); 

Here你可以閱讀更多關於控制器設置的分頁。但是如果你已經有模型中的關聯(hasMany等),那麼如果你遞歸到1而不需要包含(你仍然可以使用它來獲取你想要的數據),那麼數據就會被自動獲取。它將如何爲你找到一個例子:

$this->paginate = array(
    'conditions' => array(
     'User.Status' => 'Invisible', 
     'User.Enabled' => true 
    ), 
    'contain' => array('Comment' => array(
      'conditions' => array('Comment.Status' => 'Active') 
    )), 
    'recursive' => 1, 
    'order' => 'User.DateCreated DESC' 
); 
$data = $this->paginate('User'); 

這是你在你的文章中。如果你粘貼你想要的sql查詢,我可能會幫助你更多。另外,請嘗試檢查sql輸出,以便知道條件中是否有錯誤。如果你沒有,也不知道如何把sql輸出做到這一點。

var_dump($this->User->getQuery(
    'conditions' => array(
     'User.Status' => 'Invisible', 
     'User.Enabled' => true, 
     'Comment.Status' => 'Active' 
    ), 
    'contain' => 'Comment', 
    'order' => 'User.DateCreated DESC' 
)); 

希望它可以幫助你,如果沒有,給我留言。

編輯:這是你需要做的,以得到你想要的。 :D更改表格和條件以適應您的需求。

$this->paginate = array(
    'conditions' => array(
     'User.Status' => 'Invisible', 
     'User.Enabled' => true 
    ), 
    'joins' => array(
     array(
      'table' => 'name_of_comment_table', 
      'alias' => 'Comment', 
      'type' => 'inner', 
      'conditions' => array(
       'Comment.user_id = User.id', 
       'Comment.Status' => 'Active' 
      ) 
     ) 
    ), 
    'contains' => array('Comment'), 
    'group' => 'User.id', 
    'recursive' => 1, 
    'order' => 'User.DateCreated DESC' 
); 
$data = $this->paginate('User'); 
+0

有了您的幫助,我越來越密切。在我看來,我似乎無法訪問我的數據。我使用'$ this-> set('records',$ data)' - 並且我的分頁計數似乎是準確的(我在我的視圖中計算了超過1300個記錄) - 但我似乎無法訪問在我看來,'$ records'(在那裏似乎沒有任何東西)。 –

+0

@BenjaminKreeger發送到視圖之前嘗試var_dumo($數據),所以我們知道,如果它是獲取數據或不...(記得從PAGINATE使用的代碼,而不是getQuery最後只返回查詢,而不做) – api55

+0

做一個'的var_dump($這個 - > PAGINATE( '用戶'))''顯示布爾(假)',所以有些事情正在發生。我設置了'$ this-> paginate ='就像你在答案中顯示的一樣。我準備把這東西扔出窗外。 –

相關問題