I have Album, Content, File and Comment entities where Comment and File is a relation of Content, and Content is related to Album.
I want to retrieve all relations (File and Comment entities) from a single Content entity (and not load them for any of the other contents). There should be no more than 2 queries to the database.
What’s a good way of retrieving the relations using a single query, and populate the entity object?
E.G
$album = $em->createQuery('SELECT a,c FROM Album JOIN a.contents c WHERE a.id = :id')->getOneOrNullResult();
foreach ($album->getContents() as $content) {
if ($content->getId() == $id) {
// Load all (relevant) relations for this content in a single query with joins
$em->MAGIC($content);
}
}
I found a solution to this. By defining Content, such that it can only be a member of a single album at the time – I can join album by Content as such:
Then Doctrine 2 magic make sure that the returned content, is referencing the same object in the
$album->contentsproperty as well.End result, is a single query.