Hey,
I am trying to get objects that are connected by a relationship in a single query with the builder, and I wonder whether it's possible or not.
Let's say that I have a model called "users" and each user can have many "media" records. I'm trying to get a row from a query, where each row has all of the user's media records attached, instead of multiple rows that each contain the user data and the media information.
$objects = $this->modelsManager->createBuilder()
->from('Users')
->join('Media')
->where('Users.id=1 AND Media.user_id=Users.id')
->columns(array('Users.*', 'ProfilesMedia.*'))
->getQuery()
->execute();
foreach($objects as $object) {
//access $object['Media'][0]['id']
//access $object->id
}
I could use groupBy for media.id and sort with user.id, and then do some processing to the data..
What's the best practice method to approach this?