我在控制器中编写了一个条件过滤器,工作原理如下:
$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();
破坏了整个事情。为什么会有这种奇怪的行为?谢谢。
尝试这样做:
$collection = $collection->find_all();
find_all()
不将查询结果保存在 ORM 对象中,则需要将其保存在变量中。