類似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');
有了您的幫助,我越來越密切。在我看來,我似乎無法訪問我的數據。我使用'$ this-> set('records',$ data)' - 並且我的分頁計數似乎是準確的(我在我的視圖中計算了超過1300個記錄) - 但我似乎無法訪問在我看來,'$ records'(在那裏似乎沒有任何東西)。 –
@BenjaminKreeger發送到視圖之前嘗試var_dumo($數據),所以我們知道,如果它是獲取數據或不...(記得從PAGINATE使用的代碼,而不是getQuery最後只返回查詢,而不做) – api55
做一個'的var_dump($這個 - > PAGINATE( '用戶'))''顯示布爾(假)',所以有些事情正在發生。我設置了'$ this-> paginate ='就像你在答案中顯示的一樣。我準備把這東西扔出窗外。 –