2009-08-16 4 views
1
public function getWorksheetData($id) { 

/** create the following query using select object: 

    SELECT wc.label, wd.notes FROM worksheet_data wd 
    LEFT JOIN worksheet_columns wc ON wd.column_id = wc.id; 

*/ 
    $id = (int) $id; 

    $select = $this->_db->select() 
    ->from(array('wd'=>'worksheet_data'), 
      array('wc.label','wd.notes')) 
    ->join(array('wc'=>'worksheet_columns','wd.column_id = wc.id')) 
    ->where("wd.id = :worksheet_id"); 

    $results = $this->_db->fetchAll($select, array('worksheet_id' => $id),Zend_Db::FETCH_ASSOC); 


    return array('results'=>$results); 

}Zend_DB_Select: pourquoi tous les champs sont-ils retournés?

Pourquoi cette requête devient:

SELECT wc. label, wd. . notes, wc * FROM worksheet_data AS wd INNER JOIN worksheet_columns AS wc OU (wd.id =: worksheet_id)

et retour wc *.?

Répondre

3

Vous devez placer un tableau vide comme troisième argument de la méthode de jointure, la condition de jointure ne doit pas faire partie du tableau du premier argument, mais plutôt du second argument (vous remarquerez qu'il n'y a pas de jointure condition dans votre requête). Joignez-vous à des besoins au moins les deux premiers arguments, c'est la signature de la méthode:

rejoindre (table, jointure, [colonnes])

Application que votre méthode join:

->join(array('wc'=>'worksheet_columns'),'wd.column_id = wc.id', array()) 

donc:

$select = $this->_db->select() 
     ->from(array('wd'=>'worksheet_data'), 
         array('wc.label','wd.notes')) 
     ->join(array('wc'=>'worksheet_columns'),'wd.column_id = wc.id', array()) 
     ->where("wd.id = :worksheet_id"); 

donne la sortie:

SELECT `wc`.`label`, `wd`.`notes` FROM `worksheet_data` AS `wd` INNER JOIN `worksheet_columns` AS `wc` ON wd.column_id = wc.id WHERE (wd.id = :worksheet_id) 

Le manual dit:

Pour sélectionner aucune colonne d'une table, utilisez un tableau vide pour la liste des colonnes.

+0

Merci. Le manuel indique également les méthodes joinInner (table, join, [columns]). Les crochets ne signifient-ils pas que c'est facultatif? – codecowboy

+1

@codecowboy - oui c'est le cas, j'ai ajouté la signature de la méthode à ma réponse. – karim79

Questions connexes