Question

I have the following tables aliados (Main table and the one I'm trying to retrieve records from). aliados_direccion which has aliados_id as a foreign key aliados_centro_medico which also has aliados_id as a foreign key and makes possible a Many_Many relation with the following table. centro_medico.

In aliados_direccion and in centro_medico there's a column called "parroquias_id" which comes from a different table that has no importance for this query.

I'm trying to get the grid to show me some "aliados" if they have an aliados_direccion or an aliados_centro_medico related to them where the aliados_direccion.parroquias_id or the centro_medico.parroquias_id is in an array that depends on the user that is logged in.

The array is fine and mysql reads it without a problem, the issue is in the actual query.

$criteria=new CDbCriteria;
$criteria->select='a.*';
$criteria->alias='a';
$criteria->join='RIGHT JOIN (SELECT dir.* FROM aliados_direccion dir
              WHERE dir.parroquias_id IN ('.$zonas.')) ad ON ad.aliados_id=a.id';
$criteria->join='RIGHT JOIN (SELECT many.* FROM aliados_centro_medico many
                        INNER JOIN (SELECT centro_medico.id as idCM
                                              FROM centro_medico
                                              WHERE centro_medico.parroquias_id
                                              IN ('.$zonas.')) cm
                                    ON cm.idCM = many.centro_medico_id) acm
                  ON acm.mAlId = a.id';
$criteria->compare(blahblahblah);
//BTW $zonas is the array that I previously built

What happens here is the grid will either show me all records or none, depending on whether I use left or right join. I tried using the relation's alias too, like>

if($this->aliados_direccion){
//First Criteria Join
elseif($this->aliados_centro_medico){
//Second Criteria Join
}

But when i do that it will still bring me all records, even if i use INNER JOIN instead.

Was it helpful?

Solution 2

So, this was the answer... I had to make the $zonas array which I was already doing as a sql statement that would complete the query, like this...

$zonas = 'SELECT z.parroquias_id
          FROM m_zona_mercadeo z, m_tipo_zona_mercadeo t, m_rrhh r
          WHERE r.usuario_id = '.$user->id.'
          AND r.tipo_zona_id = t.id AND t.id = z.tipo_zona_id';

And the query ended up like this:

$criteria=new CDbCriteria;
$criteria->select='a.*';
$criteria->alias='a';
$criteria->join='INNER JOIN (SELECT ad.aliados_id
                             FROM aliados_direccion ad
                             WHERE ad.parroquias_id IN ('.$zonas.')
                 UNION
                             SELECT acm.aliados_id
                             FROM aliados_centro_medico acm, centro_medico cm
                             WHERE cm.id = acm.centro_medico_id
                             AND cm.parroquias_id IN ('.$zonas.')) u
                 ON u.aliados_id = a.id';

OTHER TIPS

You replaced first join by next.

$criteria->join='RIGHT JOIN (SELECT dir.* FROM aliados_direccion dir
          WHERE dir.parroquias_id IN ('.$zonas.')) ad ON ad.aliados_id=a.id';
$criteria->join='RIGHT JOIN (SELECT many.* FROM aliados_centro_medico many
                    INNER JOIN (SELECT centro_medico.id as idCM
                                          FROM centro_medico
                                          WHERE centro_medico.parroquias_id
                                          IN ('.$zonas.')) cm
                                ON cm.idCM = many.centro_medico_id) acm
              ON acm.mAlId = a.id';

Join is CDbCriteria field, so you need to combine them

$criteria->join='RIGHT JOIN (SELECT dir.* FROM aliados_direccion dir
          WHERE dir.parroquias_id IN ('.$zonas.')) ad ON ad.aliados_id=a.id
          RIGHT JOIN (SELECT many.* FROM aliados_centro_medico many
                    INNER JOIN (SELECT centro_medico.id as idCM
                                          FROM centro_medico
                                          WHERE centro_medico.parroquias_id
                                          IN ('.$zonas.')) cm
                                ON cm.idCM = many.centro_medico_id) acm
              ON acm.mAlId = a.id';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top