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();
破坏了整个事情。为什么会有这种奇怪的行为? 谢谢。
I wrote in my controller a conditional filter working like this:
$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();
And in the view I have a conditional to display a message if there are no filtered results or rows in database.
<?php if ( ! $collection->count()): ?>
This gives me an exception:
Kohana_Exception [ 0 ]: Invalid method count called in Model_Product
The problem is that before adding the filter, my controller action was:
$this->_view = View::factory('crud/index')
->bind('collection', $collection);
$collection = ORM::factory($this->_model)->find_all();
And $collection->count()
worked just fine in the view. Why is the ORM find_all() method returning a model even if I don't post, even if the code is not entering the conditional? Just breaking $collection = ORM::factory($this->_model)->find_all();
into $collection = ORM::factory($this->_model);
and $collection->find_all();
breaks the whole thing. Why is that strange behavior?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这样做:
find_all()
不会将查询结果保存在 ORM 对象中,您需要将其保存在变量中。Try doing this:
find_all()
doesn't save the query results in the ORM object, you need to save it in a variable.