Kohana 3 find_all 返回模型而不是结果集对象

发布于 2024-11-09 09:02:48 字数 1168 浏览 0 评论 0原文

我在控制器中编写了一个条件过滤器,其工作原理如下:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毅然前行 2024-11-16 09:02:48

尝试这样做:

$collection = $collection->find_all();

find_all() 不会将查询结果保存在 ORM 对象中,您需要将其保存在变量中。

Try doing this:

$collection = $collection->find_all();

find_all() doesn't save the query results in the ORM object, you need to save it in a variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文