I want to display in users search results also their photos. Not only avatar photo but 3 photos. Photos are in extra table user_photos. If I would get single row per user the answer would be clear - inner join. But I will get multirows for each user.
First method I can use is join:
SELECT * FROM users INNER JOIN photos ON photos.user_id=users.user_id
In this case I need some extra php code to merge results with same user_id. Something like this:
foreach($results as $result){
if(isset($user[$result['user_id']]['age'])){
$user[$result['user_id']]['photos'][] = $result['photo'];
continue;
}
$user[$result['user_id']]['age'] = $result['age'];
$user[$result['user_id']]['photos'][] = $result['photo'];
//...
}
Then in view I need first first level loop for users and then for each user second level loop for $user[$result['user_id']]['photos'] array.
or I can use for each result subquery. None of options seems elegant, so I am wondering if there is any other way. If not, probably first option is a way to go, correct?