4

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

user256430
  • 3,575
  • 5
  • 33
  • 33

1 Answers1

9

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();
biakaveron
  • 5,493
  • 1
  • 16
  • 20
  • That was it! It figures it would be something that simple, lol. Do you happen to have a reference that I could look at that talks about this particular issue? There's nothing on Kohana's site... – user256430 Mar 17 '11 at 12:46
  • Just read a source code. There was a `count_last_query()` function in previous versions, so I tried to find an alternate method. – biakaveron Mar 17 '11 at 14:02