php-activerecord icon indicating copy to clipboard operation
php-activerecord copied to clipboard

Avoid duplicate foreign_key values when do eager loading (preloading)

Open nguyenbathanh opened this issue 11 years ago • 1 comments

Example:

$posts = Post::all(array('include' => array('author'));

foreach($posts as $post){
    echo $post->author->name;
}

After loading the posts, It will collect the author_id from each one and load all the referenced authors with one query.

But some posts may have the same author_id so we should avoid them being duplicated (in select query).

nguyenbathanh avatar Feb 11 '14 08:02 nguyenbathanh

:+1: Or is this the wrong approach? My log files gets ugly :(

ttrig avatar May 07 '15 15:05 ttrig