Kohanav3 ORM: calling where->find_all twice
-
26-09-2019 - |
Question
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?
Solution
Its because of find_all()
and find()
methods will reset your model state. For example, $user
has a where('site_id', '=', <site_id>)
condition (it was applied in the line#1 of your code). When you call find_all()
, ORM resets all conditions, so $nondeletedusers
works with empty model.
To avoid this behavior, you can try to clone
$users, or retrieve all users ids from $user
and add AND WHERE id IN <id list>
condition.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow