Symfony2 + Doctrine: recupera información de 2 entidades, enlazadas sin variable miembro. SQL de consulta nativa? => Error de hidratación / ObjectHydrator

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);

Pero tengo un 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.

Alguna idea por favor?

Gracias por todo

Nos vemos

Diana

preguntado el 02 de febrero de 12 a las 10:02

1 Respuestas

Según los documentos:

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

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

you maybe missed to define the thread propiedad en el News class that will receive your joined entity.

Respondido 02 Feb 12, 22:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.