Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

prebuild

posted by mungiu 7 years ago

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?

posted by mblarsen 7 years ago

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.

posted by mungiu 7 years ago

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?

posted by mblarsen 7 years ago

My ideea is to make one query over the databse not another one for every row.

posted by mungiu 7 years ago

That is what my first examples does. AFAIK.

Isn't this working for you?

posted by mblarsen 7 years ago

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.

posted by mungiu 7 years ago

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.

posted by mblarsen 7 years ago