I think a good improvement it is to make available a way to put condition and sort on related records on prebuild method.
Just to understand. You are arguing this case:
$posts->prebuildComments(); // if left out each call to buildComments() will go to the database
foreach ($post as $post) {
$comments = $post->buildComments()->sort('date', 'desc'); // will not fire db lookup, sorting done in memory
foreach ($comments as $comment) {
echo $comment->getTitle();
}
}
vs. something like this (NOTE: this is pseudo code to illustrate my understanding of your request)):
$posts->prebuildComments(array('sort' => array('date', 'desc')); // will fetch all comments and sort in database (not in memory)
foreach ($post as $post) {
$comments = $post->buildComments(); // comments already fetched and sorted
foreach ($comments as $comment) {
echo $comment->getTitle();
}
}
.. and of course similar with filter?
Second question. Is this really a suggestion to prebuilding or is it a general desire to have sort and filtering in relationships, that runs in the DB?
For what i see is that when prebuildComment make one query over the database but when you build comments for every post makes multiple queries.
if you invoke buildComments() without having invoked prebuildComments() it will do so for each post.
But your post is about filtering and sorting, right? You want this done in the prebuild phase right? or is it because you simply want this done in the DB and not done in memory?
My ideea is to make one query over the databse not another one for every row.
That is what my first examples does. AFAIK.
Isn't this working for you?
but when you call $comments = $post->buildComments() in foreach isn't make a new query? or it is playing with object already populated with related, and after put it filter on work. I will play more tonight to see what is happend.
No it will in fact give return the already fetched comments for the post in question.
Yeah, just turn on db debugging the it will be clear.