Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kohana 3 ORM find_all() returns all rows regardless of where clause

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

like image 606
DondeEstaMiCulo Avatar asked Jan 21 '23 05:01

DondeEstaMiCulo


1 Answers

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();
like image 144
biakaveron Avatar answered May 16 '23 08:05

biakaveron