Subcondiciones y Subconsultas en Doctrine

531 views
Skip to first unread message

Thor

unread,
Oct 6, 2009, 7:55:44 AM10/6/09
to symfony-es
Buenas,

Necesito vuestra ayuda para montar un Query a través de Doctrine, por
ahora solo he conseguido montar la select directamente, pero esto no
será muy editable en un futuro y me gustaría mejorarlo para tener algo
standard:

$q = Doctrine_Query::create()
->from('Edificio e')
->where(
'(e.id_raz = ?) and '.
'(e.id_pre_edi is null or '.
'(e.id_pre_edi in (Select id_edi from tbl_pla_edi where
activo = ? and id_pla = ?) and e.unico = ?) or '.
'(e.id_pre_edi not in (Select id_edi from tbl_pla_edi where
activo = ? and id_pla = ?) and e.unico = ?) '.
') and '.
'(e.id_pre_tec is null or '.
'(e.id_pre_tec in (Select id_tec from tbl_usu_tec where
id_usu = ? and activo = ?))'.
')',
array(
$id_raz,
true, $id_pla, false,
true, $id_pla, true,
$id_usu, true
)
);

Hay 2 cosas que me está dando dolores de cabeza:
- ¿Cómo poner sub condiciones tipo ( 1=1 AND ( 1=1 OR 1=1 ) AND
1=1 ) ?
- ¿Hay forma de mejorar los Subqueries? ¿Ponerlo como un LeftJoin o
haciendo una select antes para obtener un array de PKs?

¿Me podéis echar una mano con esta select?
Muchas gracias.
Un saludo.

Thor

unread,
Oct 6, 2009, 4:31:48 PM10/6/09
to symfony-es
Al final he mejorado la query de esta forma:

$qPE = 'Select id_edi from tbl_pla_edi where activo = :true and
id_pla = :id_pla';
$q = Doctrine_Query::create()
->from('Edificio e')
->where(
'(e.id_raz = :id_raz) AND '.
'(e.id_pre_edi IS NULL OR '.
'(e.id_pre_edi IN ('.$qPE.') and e.unico = :false) OR '.
'(e.id_pre_edi NOT IN ('.$qPE.') and e.unico = :true)) AND '.
'(e.id_pre_tec IS NULL OR '.
'(e.id_pre_tec IN (Select id_tec from tbl_usu_tec where id_usu
= :id_usu and activo = :true)))',
array(
':true' => true,
':false' => false,
':id_raz' => $id_raz,
':id_pla' => $id_pla,
':id_usu' => $id_usu
)
);

¿Cómo montáis las consultas complejas vosotros?

Saludos.
Reply all
Reply to author
Forward
0 new messages