Sam
Sam

Reputation: 779

Symfony2 + Doctrine: Retrieve information from 2 entities, linked without member variable . Native query sql? => Hydration/ObjectHydrator error

I want to do something like that into Doctrine SQL...

SELECT N.id as n_id, C.id as c_id, T.id as t_id, C.author_id, C.body as comment FROM `news` N 
LEFT JOIN thread T on T.id = N.id
LEFT JOIN comment C on C.thread_id = T.id
WHERE 1

So I have made this :

    $rsm = new ResultSetMapping;
    $rsm->addEntityResult('App\MyBundle\Entity\News', 'N');
    $cols = array ('id', 'title','date');
    foreach ($cols as $key => $col) {
        $rsm->addFieldResult('N', $col, $col);
    }
    // thread
    $rsm->addJoinedEntityResult('App\MyBundle\Entity\Thread' , 'T', 'N', 'thread');
    $rsm->addFieldResult('T', 't_id', 'id');

    $sql = 'SELECT N.id, N.title, N.date, T.id AS t_id FROM news N ' .
    'LEFT JOIN thread T ON T.id = N.id WHERE N.id = 1';
    $query = $this->_em->createNativeQuery($sql, $rsm);

But I have an error:

    Notice: Undefined index: thread in /vendor/doctrine/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php line 85       

I want to precise that there is no member link beetween the 2 entities.

Any ideas please?

Thanks for all

See ya

Sam

Upvotes: 0

Views: 3157

Answers (1)

Florian Klein
Florian Klein

Reputation: 8915

According to the docs:

The fourth and last parameter is the name of the field on the parent entity result that should contain the joined entity result.

ref: http://www.doctrine-project.org/docs/orm/2.1/en/reference/native-sql.html#joined-entity-results

you maybe missed to define the thread property in the News class that will receive your joined entity.

Upvotes: 2

Related Questions