Kohana 3 ORM find_all() 返回所有行,无论 where 子句如何

发布于 2024-10-23 21:01:16 字数 827 浏览 3 评论 0原文

我有一个简单的 users 表,我想查找 email_notifications = 1 的所有用户。

逻辑表明以下操作应该有效:

class Controller_Test extends Controller {

    public function action_index()
    {
        $user = ORM::factory('user');
        $user = $user->where('email_notifications', '=', 1);
        $total = $user->count_all();
        $users = $user->find_all();

        echo $total." records found.<br/>";

        foreach ($users as $v)
        {
            echo $v->id;
            echo $v->first_name;
            echo $v->last_name;
            echo $v->email;
        }
    }
}


然而,发生的情况是,我正在从数据库中恢复所有用户,而不仅仅是那些打开了 email_notifications 的用户。有趣的是,返回的$total值是这次查询的准确数字结果。

我很困惑,我不知道问题出在哪里。如果有人能提供一些线索,我将非常感激。

谢谢,
布莱恩

I have one simple users table, and I want to find all users where email_notifications = 1.

Logic dictates that the following should work:

class Controller_Test extends Controller {

    public function action_index()
    {
        $user = ORM::factory('user');
        $user = $user->where('email_notifications', '=', 1);
        $total = $user->count_all();
        $users = $user->find_all();

        echo $total." records found.<br/>";

        foreach ($users as $v)
        {
            echo $v->id;
            echo $v->first_name;
            echo $v->last_name;
            echo $v->email;
        }
    }
}

However, what's happening is that I am getting ALL of my users back from the DB, not just the ones with email_notifications turned on. The funny thing is, the $total value returned is the accurate number result of this query.

I am so stumped, I have no idea what the problem is here. If anyone could shed some light, I'd really appreciate it.

Thanks,
Brian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

墨落画卷 2024-10-30 21:01:16

调用 count_all() 将重置您的模型条件。尝试使用 reset(FALSE) 来避免这种情况:

    $user = ORM::factory('user');
    $user = $user->where('email_notifications', '=', 1);
    $user->reset(FALSE); 
    $total = $user->count_all();
    $users = $user->find_all();

Calling count_all() will reset your model conditions. Try to use reset(FALSE) to avoid this:

    $user = ORM::factory('user');
    $user = $user->where('email_notifications', '=', 1);
    $user->reset(FALSE); 
    $total = $user->count_all();
    $users = $user->find_all();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文