Dennis Fransen
Dennis Fransen

Reputation: 5

load field result from join in symfony2 parent entity

Does anyone know a way to load the results of a join field into the parent entity?

An example where i need this, is in my article entity. An article can have multiple translations, but one should be loaded according to the selected locale.

The text and title fields are loaded from the article translations bundle, they don't exist in my article entity.

loading all the field's isn't a problem, in my articleRepository is the following code:

....
$oQueryBuilder->select('a, tr');
$oQueryBuilder->from('CommonArticleBundle:Article', 'a');
$oQueryBuilder->andWhere('a.active = 1');

$oQueryBuilder->leftJoin('a.translations', 'tr');
$oQueryBuilder->andWhere('tr.language = 2');
.........

The returned query is a single row containing all article fields, and the translation fields.

Is it possible to access the translation fields in twig like: {{ article.text }}?

I try'd to add private $title; to my article entity, but a var_dump shows a value null.

Upvotes: 0

Views: 344

Answers (1)

manix
manix

Reputation: 14747

Have you tried:

$oQueryBuilder->select(array('a.id', 'tr.title', 'tr.text'));
$articles = $oQueryBuilder->getQuery()->getArrayResult();

and then read the first article fields like:

$article[0]['title'];

If you want children entities you can do that also:

$oQueryBuilder->select(array('a.id', 'tr.title', 'tr.text', 's'));
$oQueryBuilder->leftJoin('a.sessions', 's');

And then:

$sessions = $article[0]['sessions'];
foreach ($sessions as $s)
{
    $text = $s['text'];
}

Upvotes: 1

Related Questions