Consultez la FAQ sur le ZF avant de poster une question
Vous n'êtes pas identifié.
Pages: 1
Bonjour,
Je débute avec Zend framework, et je bute sur un problème que je n'arrive pas à résoudre.
Je cherche à effectuer une jointure. Je sais que c'est un sujet récurrent, mais le problème est ici un peu différent.
Voici ma classe :
class Batiments extends Zend_Db_Table_Abstract { protected $_name = 'batiments_colonie'; protected $_primary = array('batiment_id', 'position_id'); public function getBatiments() { $select = $this->select()->setIntegrityCheck(false) ->where('colonie_id = ?', 1) ->join('batiments_types', 'batiments_colonie.batiment_id = batiments_types.batiment_id'); return $this->fetchAll($select); } }
Cela me retourne l'erreur
Fatal error: Uncaught exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[42S22]: Column not found: 1054 Champ 'colonie_id' inconnu dans where clause' in E:\sg-network\sg-civilizations\library\Zend\Db\Statement\Pdo.php:238 Stack trace: #0 E:\sg-network\sg-civilizations\library\Zend\Db\Statement.php(283): Zend_Db_Statement_Pdo->_execute(Array) #1 E:\sg-network\sg-civilizations\library\Zend\Db\Adapter\Abstract.php(430): Zend_Db_Statement->execute(Array) #2 E:\sg-network\sg-civilizations\library\Zend\Db\Adapter\Pdo\Abstract.php(220): Zend_Db_Adapter_Abstract->query(Object(Zend_Db_Table_Select), Array) #3 E:\sg-network\sg-civilizations\library\Zend\Db\Table\Abstract.php(1189): Zend_Db_Adapter_Pdo_Abstract->query(Object(Zend_Db_Table_Select)) #4 E:\sg-network\sg-civilizations\library\Zend\Db\Table\Abstract.php(1044): Zend_Db_Table_Abstract->_fetch(Object(Zend_Db_Table_Select)) #5 E:\sg-network\sg-civilizations\application\models\Batiments.php(32): Zend_Db_Table_Abstract->fetchAll(Object(Zend_Db_Table_Select)) #6 E:\ in E:\sg-network\sg-civilizations\library\Zend\Db\Statement\Pdo.php on line 238
Et pourtant mon code ne doit pas être si mauvais que cela car ces deux instructions fonctionnent avec les résultats attendus dans la vue :
$select = $this->select()->setIntegrityCheck(false) ->where('colonie_id = ?', 1);
$select = $this->select()->setIntegrityCheck(false) ->join('batiments_types', 'batiments_colonie.batiment_id = batiments_types.batiment_id');
Mais pas moyen d'utiliser le join et le where en même temps !!
Merci de votre aide...
Dernière modification par Meardon (10-10-2008 20:58:11)
Hors ligne
'batiments_types.batiment_id = batiments_types.batiment_id'
Tu es sûr de la jointure ?
Hors ligne
Oups petite erreur, c'est ->join('batiments_types', 'batiments_colonies.batiment_id = batiments_types.batiment_id'); j'édite mon premier message.
Mais ça ne vient pas de là ;-)
Dernière modification par Meardon (10-10-2008 17:34:09)
Hors ligne
Je viens de me rendre compte après tests qu'en fait je peux mettre dans la clause where les noms de colonnes de la table liée, et pas ceux des colonnes la table correspondant à la classe ! C'est pas logique ! Comment puis-je y remédier ?
J'ai aussi essayé sans succès : ->where('batiments_colonie.colonie_id = ?', 1)
Hors ligne
Essaye comme ceci :
$select = $this->select() ->setIntegrityCheck(false) ->from(array('BC' => $this->_name), 'colonie_id', .......//tes champs) ->join(array('BT'=>'batiments_types'), 'BC.batiment_id = BT.batiment_id') ->where('BC.colonie_id = ?', 1); return $this->fetchAll($select);
Dernière modification par alien7 (10-10-2008 19:28:24)
Hors ligne
Génial je n'ai plus d'erreur ! Merci beaucoup alien7 !
Le code marche aussi sans spécifier les champs pour le from, à quoi cela sert ?
Par convention, ta façon de faire est-elle à appliquer systématiquement ou uniquement pour ce cas de figure ?
Dernière modification par Meardon (10-10-2008 19:39:05)
Hors ligne
Le code marche aussi sans spécifier les champs pour le from, à quoi cela sert ?
Sans specifier les champs c'est equivalent à SELECT * FROM (tu selctionne tous les champs de ta table), mais parfois on a pas besoin de tous les champs.
Par convention, ta façon de faire est-elle à appliquer systématiquement ou uniquement pour ce cas de figure ?
Toutes mes jointures je les écris comme ça
Hors ligne
Pages: 1