When I do something like the following:
$site = ORM::factory('site')->where('name', '=', 'Test Site')->find();
$users = $site->users;
$deletedusers = $users->where('deleted', '=', '1')->find_all();
$nondeletedusers = $users->where('deleted', '=', '0')->find_all();
The contents of $deletedusers is correct, but the $nondeletedusers contains every non-deleted user, not just the ones in the loaded $site.
What am I doing wrong?
Its because of
find_all()andfind()methods will reset your model state. For example,$userhas awhere('site_id', '=', <site_id>)condition (it was applied in the line#1 of your code). When you callfind_all(), ORM resets all conditions, so$nondeletedusersworks with empty model.To avoid this behavior, you can try to
clone$users, or retrieve all users ids from$userand addAND WHERE id IN <id list>condition.