2012-04-10 14 views
5

Suponiendo que tengo 2 mesasGROUP_CONCAT con JOINLEFT en Zend Db Seleccionar

articles 
    id    title 
    1    Article 1 
    2    Article 2 


Images 
    id    article_id  image 
    1    1    a.png 
    2    1    b.png 
    3    2    c.png 
    4    2    d.png 

Todo lo que quiero es retreive todos los artículos con sus imágenes.

Por ejemplo:

article_id  title   images 
1    Article 1  a.png, b.png 
2    Article 2  c.png, d.png 

¿Cómo podría hacer eso con Zend_Db_Select?

I intentado algo así, pero no tuvo suerte:

$select = $this->getDbTable()->select()->setIntegrityCheck(false)->distinct(); 
$select->from(array('a'=>'articles')) 
    ->joinLeft(array('i'=>'images'),'i.article_id=a.id',array('images'=> new 
       Zend_Db_Expr('GROUP_CONCAT(i.image)'))); 

Devuelve sólo sólo 1 fila qué campo 'imágenes' contiene imágenes de ambos artículos.

article_id  title   images 
1    Article 1  a.png, b.png, c.png, d.png 

¿Qué estoy haciendo mal aquí?

+1

¿Dónde está cláusula group by? –

Respuesta

7

Usted no ha usado group by cláusula en la consulta.

Trate a continuación:

$select->from(array('a'=>'articles')) 
    ->joinLeft(
     array('i'=>'images'), 
     'i.article_id=a.id', 
     array('images'=> new Zend_Db_Expr('GROUP_CONCAT(i.image)'))) 
    ->group('a.id'); 
+0

Agregar "GROUP BY" resolvió el problema. O usando esto también lo resuelve: $ select-> joinLeft (array ('x' => new Zend_Db_Expr ('(SELECCIONAR i.article_id, GROUP_CONCAT (i.image) como imágenes DE imágenes como i GROUP BY i.article_id) ')), ' x.article_id = a.id ', array (' x.images ')); –

+0

Tu respuesta realmente me ayudó – Zygimantas

Cuestiones relacionadas