Results 1 to 25 of 118
Sep 28, 2007, 13:04 #38
- Join Date
- Aug 2007
- 0 Post(s)
- 0 Thread(s)
Okey, I worked on this some more, and got the whole column-meta business out of the way. Back to philosophizing about associations..
There is an interesting problem with eager loaded has many associations when combined with a limit. Since you only want the limit to affect your main objects, not the associated ones, you basically have to make one extra query, or at least that's how I solved it.
The first query selects all unique IDs with SELECT DISTINCT and applies the limit. It then supplies this list of IDs to the main query, which runs a condition that looks something like WHERE id IN (1, 2, 3, 4...) where the numbers are the list of IDs from the first query.
This brings out another matter. Eager loaded associations can't be limited. This of course only applies to one-to-many associations. I tried to think of a way around it but hasn't come up with anything so far. Maybe it doesn't matter. If you need limiting capabilities for a one-to-many, perhaps you are better off lazy loading it.
Speaking of normal loading, or lazy loading which I think is the correct term (I hope). 33degrees wrote something similar to this a while back:
$user = new User();
$user = findByPk(1);
You could of course override this method with a custom one in your model.
My current implementation of "lazy" loading is quite horrible, I just reused the standard syntax so I'd do:
Just some food for thought.. As always, interested in hearing how others handle these things.