Search code examples
sqljoinzend-db-select

Join with Zend_Db, without having the columns of the joined tables


I use Zend_Db_Select to perform a query with a Join. I end up with the following SQL query :

SELECT `Utilisateur`.*, `Ressource`.*, `Acl_Cache`.*, `Role`.*, `UtilisateurRole`.* FROM `Utilisateur`
INNER JOIN `Ressource` ON Ressource.idJointure = Utilisateur.id
INNER JOIN `Acl_Cache` ON Acl_Cache.idRessource = Ressource.id
INNER JOIN `Role` ON Role.id = Acl_Cache.idRole
INNER JOIN `UtilisateurRole` ON UtilisateurRole.idRole = Role.id
WHERE (Ressource.tableJointure = 'Utilisateur') AND (UtilisateurRole.idUtilisateur = '2')

Why is Zend_Db adding this part is the SELECT clause :

, `Ressource`.*, `Acl_Cache`.*, `Role`.*, `UtilisateurRole`.*

I never asked this, and I don't want that. How to prevent this behavior ?


Solution

  • $db->select()
       ->from(array('alias1'=>'table_i_want_all_cols_on'))
       ->joinLeft(array('alias2'=>'table_i_want_no_cols_on'),
                    'alias1.id = alias2.id',
                    array()
       )
       ->joinLeft(array('alias3'=>'table_i_want_some_cols_on'),
                    'alias3.id = alias1.id',
                    array('col1', 'col2')
       );