Précédent   Forum des professionnels en informatique > PHP > Outils > Zend > Zend Framework > Zend_Db
Zend_Db Forum d'entraide pour le composant Zend_Db du Zend Framework (création de requêtes, abstraction, ORM etc.). Avant de poster -> FAQ Zend_Db.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 15/12/2010, 11h43   #1
Membre à l'essai
 
Inscription : mars 2010
Messages : 213
Détails du profil
Informations forums :
Inscription : mars 2010
Messages : 213
Points : 23
Points : 23
Par défaut Jointure entre 2 tables en utilisant zend

Bonjour,

Je veux afficher le contenu de 2 tables en utilisant zend dans la vue index.phtml ,voici mon code:

Code :
1
2
3
4
5
6
7
8
$table = new Vente();
	$select =$table->select()->from(array('vente'),array
 
('id_cli','id_vente','date_vente','categorie_v'))
->join(array('client'),array('id_cli','nom_cl','prenom_cl'))
	->where('vente.id_cli=client.id_cli','vente.categorie_v = ?','monture');
	$users = $table->fetchAll($select);
	$this->view->ventes= $users;
Et voila ce qui me donne dans l'exécution:
Citation:
Fatal error: Uncaught exception 'Zend_Db_Table_Select_Exception' with message 'Select query cannot join with another table' in C:\wamp\www\Opticien\library\Zend\Db\Table\Select.php:215
Stack trace:
#0 C:\wamp\www\Opticien\library\Zend\Db\Adapter\Abstract.php(456): Zend_Db_Table_Select->assemble()
#1 C:\wamp\www\Opticien\library\Zend\Db\Adapter\Pdo\Abstract.php(238): Zend_Db_Adapter_Abstract->query(Object(Zend_Db_Table_Select), Array)
#2 C:\wamp\www\Opticien\library\Zend\Db\Table\Abstract.php(1505): Zend_Db_Adapter_Pdo_Abstract->query(Object(Zend_Db_Table_Select))
#3 C:\wamp\www\Opticien\library\Zend\Db\Table\Abstract.php(1321): Zend_Db_Table_Abstract->_fetch(Object(Zend_Db_Table_Select))
#4 C:\wamp\www\Opticien\application\default\controllers\VenteController.php(29): Zend_Db_Table_Abstract->fetchAll(Object(Zend_Db_Table_Select))
#5 C:\wamp\www\Opticien\library\Zend\Controller\Action.php(513): VenteController->indexAction()
#6 C:\wamp\www\Opticien\library\Zend\Controller\Dispatcher\Standard.php(289): Zend_Controller_Action in C:\wamp\www\Opticien\library\Zend\Db\Table\Select.php on line 215
Ou est donc le problème?
king_soft est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 15/12/2010, 13h39   #2
Modératrice
 
Avatar de Celira
 
Femme
Développeuse PHP/Java
Inscription : avril 2007
Messages : 3 656
Détails du profil
Informations personnelles :
Sexe : Femme
Âge : 27
Localisation : France

Informations professionnelles :
Activité : Développeuse PHP/Java

Informations forums :
Inscription : avril 2007
Messages : 3 656
Points : 5 359
Points : 5 359
Si tu veux utiliser une jointure, il faut préciser dans join() la condition de jointure
Code :
1
2
3
4
$select = $table->select()
  ->from(array('vente'), array('id_cli','id_vente','date_vente','categorie_v'))
  ->join(array('client'), 'vente.id_cli=client.id_cli', array('nom_cl','prenom_cl'))
  ->where(array('categorie_v = ?' => 'monture'));
__________________
Modératrice PHP
Aucun navigateur ne propose d'extension boule-de-cristal : postez votre code et vos messages d'erreurs. (Rappel : "ça ne marche pas" n'est pas un message d'erreur)

Pour afficher votre code en couleurs : [CODE=php][/CODE] (bouton # de l'éditeur)
Celira est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 15/12/2010, 14h15   #3
Membre confirmé
 
Patrick Barroca
Inscription : mai 2008
Messages : 178
Détails du profil
Informations personnelles :
Nom : Patrick Barroca

Informations forums :
Inscription : mai 2008
Messages : 178
Points : 204
Points : 204
Hello,

Les selects issus d'une Db_Table en particulier sont limités à cette table par défaut.
Il suffit pour dépasser cet état de fait de faire un truc du genre
Code :
1
2
$table->select()->setIntegrityCheck(false)
->from(array('vente'), array('id_cli','id_vente','date_vente','categorie_v'))
à partir de là tu peux faire des jointures.
patbator est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 15/12/2010, 16h02   #4
Membre à l'essai
 
Inscription : mars 2010
Messages : 213
Détails du profil
Informations forums :
Inscription : mars 2010
Messages : 213
Points : 23
Points : 23
J'ai appliqué le code que vous m'avez donné,voila ce qui m'a donné :

Citation:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'where clause'' in C:\wamp\www\Opticien\library\Zend\Db\Statement\Pdo.php:228 Stack trace:
#0 C:\wamp\www\Opticien\library\Zend\Db\Statement\Pdo.php(228): PDOStatement->execute(Array)
#1 C:\wamp\www\Opticien\library\Zend\Db\Statement.php(300): Zend_Db_Statement_Pdo->_execute(Array)
#2 C:\wamp\www\Opticien\library\Zend\Db\Adapter\Abstract.php(468): Zend_Db_Statement->execute(Array)
#3 C:\wamp\www\Opticien\library\Zend\Db\Adapter\Pdo\Abstract.php(238): Zend_Db_Adapter_Abstract->query(Object(Zend_Db_Table_Select), Array)
#4 C:\wamp\www\Opticien\library\Zend\Db\Table\Abstract.php(1505): Zend_Db_Adapter_Pdo_Abstract->query(Object(Zend_Db_Table_Select))
#5 C:\wamp\www\Opticien\library\Zend\Db\Table\Abstract.php(1321): Zend_Db_Table_Abstract->_fetch(Object(Zend_Db_Table_Select))
#6 C:\wamp\www\Opticien\application\default\controllers\VenteController.php(31): Zend_Db_Table_Abstract->fetchAll(Object(Zend_D in C:\wamp\www\Opticien\library\Zend\Db\Statement\Pdo.php on line 234
Ou est donc le problème?
Merci pour vos réponses
king_soft est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 16/12/2010, 08h47   #5
Membre confirmé
 
Patrick Barroca
Inscription : mai 2008
Messages : 178
Détails du profil
Informations personnelles :
Nom : Patrick Barroca

Informations forums :
Inscription : mai 2008
Messages : 178
Points : 204
Points : 204
Hello,

En regardant ton ->where() je le trouve bizarre.
Perso mes where sont comme ça :
Code :
$select->where('champ = ?', $valeur)
Si ton champ est un entier en plus je fais
Code :
$select->where('champ = ?', (int)$valeur, Zend_Db::INT_TYPE);
Comme ça je m'assure que la valeur est castée en int et avec le 3ème param, zend sait qu'il n'a pas à l'échapper.
patbator est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 16/12/2010, 10h13   #6
Membre à l'essai
 
Inscription : mars 2010
Messages : 213
Détails du profil
Informations forums :
Inscription : mars 2010
Messages : 213
Points : 23
Points : 23
patbator,
Mon champ est de type varchar j'ai appliqué votre code et voici ce que j'ai fais:

Code :
1
2
3
4
5
6
7
$valeur='monture';
	$select = $table->select()->setIntegrityCheck(false)
  ->from(array('client'), array('id_cli','nom_cl','prenom_cl'))
  ->join(array('vente'), 'vente.id_cli=client.id_cli',array
 
('id_vente','date_vente','categorie_v'))
  ->where('categorie_v = ?',(varchar)$valeur,Zend_Db::VARCHAR_TYPE));
après exécution voila ce qui m'a donné:
Citation:
Parse error: parse error in C:\wamp\www\Opticien\application\default\controllers\VenteController.php on line 30
Ou est donc le problème?
king_soft est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 16/12/2010, 10h28   #7
Modératrice
 
Avatar de Celira
 
Femme
Développeuse PHP/Java
Inscription : avril 2007
Messages : 3 656
Détails du profil
Informations personnelles :
Sexe : Femme
Âge : 27
Localisation : France

Informations professionnelles :
Activité : Développeuse PHP/Java

Informations forums :
Inscription : avril 2007
Messages : 3 656
Points : 5 359
Points : 5 359
ça, typiquement, c'est une parenthèse en trop ou en moins ou une virgule mal placée...
__________________
Modératrice PHP
Aucun navigateur ne propose d'extension boule-de-cristal : postez votre code et vos messages d'erreurs. (Rappel : "ça ne marche pas" n'est pas un message d'erreur)

Pour afficher votre code en couleurs : [CODE=php][/CODE] (bouton # de l'éditeur)
Celira est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 16/12/2010, 10h42   #8
Membre à l'essai
 
Inscription : mars 2010
Messages : 213
Détails du profil
Informations forums :
Inscription : mars 2010
Messages : 213
Points : 23
Points : 23
Mais ou ça dans mon code?
king_soft est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/12/2010, 09h19   #9
Membre confirmé
 
Patrick Barroca
Inscription : mai 2008
Messages : 178
Détails du profil
Informations personnelles :
Nom : Patrick Barroca

Informations forums :
Inscription : mai 2008
Messages : 178
Points : 204
Points : 204
Hello,

Quand tu écris tu es en train d'écrire du PHP, c'est à dire que tu fais un cast PHP et donc entre les paranthèses tu dois spécifier un type PHP, varchar est un type de colonne MySQL.

Tu devrais donc écrire pour que ça marche.
patbator est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 17/12/2010, 09h52   #10
Membre à l'essai
 
Inscription : mars 2010
Messages : 213
Détails du profil
Informations forums :
Inscription : mars 2010
Messages : 213
Points : 23
Points : 23
Merci patbator pour ton réponse ça fonctionne bien,mais il faut éliminer ça:,Zend_Db::STRING_TYPE
king_soft est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 04h17.


 
 
 
 
Partenaires

Hébergement Web