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:
Would it be an idea perhaps to dynamically create the getProfile() method with the __call() function? Which would basically check if a) association is already loaded, so return it, or b) association not loaded, run a finder (optionally using any supplied parameters such as limit/order) and return the association.PHP Code:
$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:
Which of course makes it impossible to supply any form of conditions. :rolleyes:PHP Code:
Just some food for thought.. As always, interested in hearing how others handle these things.