Bug Report #1166
ORM with() method should wait until find() or __get() is called
| Status: | Closed | Start date: | ||
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assignee: | % Done: | 100% |
||
| Category: | Libraries:ORM | |||
| Target version: | 2.4 | |||
| Resolution: | fixed | Points: |
Description
ORM with method should support the following:
$user = ORM::factory('user', 1);
$school = $user->with('city')->school;
echo $school->city->name;
Normally, this will fail as the with() method tries to bind the city model to the user, and THEN load the school. What this should do is bind the city model to school (after it's constructed). This will require a passive with() method that waits until either find() or +get() is called to 'activate'. If +get is called to load an associated model, the with() method will be in turn called on that model.
Associated revisions
Fixing #1166
History
Updated by John Heathco over 2 years ago
- 11 set to 2.3.4
Ok, my intial implementation for this has been committed. Now all with() calls in ORM are put into a 'pending_binds' array. The binds don't actually take effect (meaning injecting JOIN statements into the query) until load_result is called, or a related object is retrieved.
For instance, say we wanted to get user ID 3's articles, and load each article with it's related photo. Before, we'd have to do the following:
ORM::factory('article')->with('photo')->where('user_id', 3)->find_all();
Now, with delayed binding, we can do:
foreach (ORM::factory('user', 3)->with('photo')->articles as $article)
{
echo $article->photo->location;
}
The with('photo') waits until something is fetched, in this case, the articles. If you did this:
echo ORM::factory('user', 3)->with('photo')->photo->name;
Then this loads the photo with the user (assuming they are related) and displays it.
Updated by John Heathco over 2 years ago
- Status changed from New to Closed
- % Done changed from 0 to 100
- Resolution set to fixed
Updated by Matt Button over 2 years ago
Line 359 (source:/branches/2.4/system/libraries/ORM.php#L359) Is redundent as calling apply_binds() clears $this->pending_binds
Line 571 (source:/branches/2.4/system/libraries/ORM.php#L571) Calls $model->applied_binds which will fail on related models as applied_binds is protected
Line 574 (source:/branches/2.4/system/libraries/ORM.php#L574) Calls $model->apply_bind() which is a protected method, so the call will fail on related objects.
Updated by Matt Button over 2 years ago
- Status changed from Closed to Feedback
- % Done changed from 100 to 90
Updated by John Heathco over 2 years ago
- Status changed from Feedback to Closed
Line 359 was fixed (that shouldn't have been in there), but there is no issue with the other two items. There is no problem accessing the protected members because they are the same class.
Updated by John Heathco over 2 years ago
- % Done changed from 90 to 100