2011-05-23 92 views
2

我在我的控制器寫了條件過濾器的工作是這樣的:Kohana的3 find_all收益模型,而不是結果集對象

$this->_view = View::factory('crud/index') 
    ->bind('collection', $collection); 

$collection = ORM::factory($this->_model); 
if (Request::current()->method() === Request::POST) 
{ 
    foreach (Request::current()->post('filter') as $field => $value) 
    { 
     $collection->where($field, '=', $value); 
    } 
} 
$collection->find_all(); 

並在視圖我有一個條件,以顯示一條消息,如果沒有過濾的結果或數據庫中的行。

<?php if (! $collection->count()): ?> 

這給了我一個例外:

Kohana_Exception [ 0 ]: Invalid method count called in Model_Product 

的問題是,加入過濾器之前,我的控制器行動是:

$this->_view = View::factory('crud/index') 
    ->bind('collection', $collection); 

$collection = ORM::factory($this->_model)->find_all(); 

而且$collection->count()視圖中的工作就好了。爲什麼ORM find_all()方法返回一個模型,即使我沒有發佈,即使代碼沒有輸入條件?剛剛打破$collection = ORM::factory($this->_model)->find_all();$collection = ORM::factory($this->_model);$collection->find_all();打破了整個事情。爲什麼這種奇怪的行爲? 謝謝。

回答

2

嘗試這樣做:

$collection = $collection->find_all(); 

find_all()不保存在ORM對象的查詢結果,你需要將其保存在一個變量。

+0

該死!如此愚蠢的錯誤!非常感謝您指出。 – 2011-05-23 16:12:24