views:

304

answers:

1

I'm trying to create a query like this one :

$q = Doctrine_Query::create()
        ->select('p.nombre')
        ->addSelect('(select count(*) from alojamiento a left join                      
                localidad l on a.localidad_id=l.id where p.id=l.provincia_id and        
                a.activo=true)')
        ->from('provincia p');

but it fails : error 500, couldn't find class a.

And :

$q = Doctrine_Query::create()
        ->select('nombre')
        ->addSelect('(select count(*) from alojamiento left join localidad on   
                alojamiento.localidad_id=localidad.id where                                     
                provincia.id=localidad.provincia_id and alojamiento.activo=true)')
        ->from('provincia');

leads to : SQLSTATE[42S22]: Column not found: 1054 Unknown column 'a.localidad_id' in 'on clause'.

Any help would be greatly appreciated !

A: 

assuming you have the alojamiento.localidad relationship configured in your doctrine models, you could try this:

$q = Doctrine_Query::create()
             ->select('p.nombre')
             ->addSelect('(select count(*) from Alojamiento a 
                          left join a.Localidad l
                          where Provincia.id = Localidad.provincia_id 
                          and l.activo=true) as count')
             ->from('Provincia p');
xzyfer