2011-03-04 7 views
0

J'ai une très grosse requête, qui est générée dans un script, pour créer quelque chose comme une vue. Pour une application.Une requête s'exécutant dans PhpMySQL, mais pas dans Zend_Framework

Voici la requête qui est générée:

SELECT * , if((SELECT `formId` from `ssp_mail_templates` WHERE `templateType`='boss' 
AND `templateIsConfirmation`='0' AND `formId`=`id`) IS NULL,0,1) as bossTemplate , 
if((SELECT `formId` from `ssp_mail_templates` WHERE `templateType`='user' AND 
`templateIsConfirmation`='0' AND `formId`=`id`) IS NULL,0,1) as userTemplate , if((SELECT 
`formId` from `ssp_mail_templates` WHERE `templateType`='superuser' AND 
`templateIsConfirmation`='0' AND `formId`=`id`) IS NULL,0,1) as superuserTemplate , 
if((SELECT `formId` from `ssp_mail_templates` WHERE `templateType`='boss' AND 
`templateIsConfirmation`='1' AND `formId`=`id`) IS NULL,0,1) as bossConfirmation , 
if((SELECT `formId` from `ssp_mail_templates` WHERE `templateType`='user' AND 
`templateIsConfirmation`='1' AND `formId`=`id`) IS NULL,0,1) as userConfirmation , 
if((SELECT `formId` from `ssp_mail_templates` WHERE `templateType`='superuser' AND 
`templateIsConfirmation`='1' AND `formId`=`id`) IS NULL,0,1) as superuserConfirmation FROM 
`ssp_form` 

Et voici le code, qui génère elle:

$valueArray = array('boss', 'user', 'superuser'); 
     $selectString = ""; 
     for ($i = 0; $i < 2; $i ++) { 
      $type="Template"; 
      if($i==1){ 
       $type="Confirmation"; 
      } 
      foreach ($valueArray as $value) { 

       $selectString.=" , if((SELECT `formId` from `".$this->_templateName."` WHERE `templateType`='".$value."' AND `templateIsConfirmation`='".$i."' AND `formId`=`id`) IS NULL,0,1) as ".$value.$type; 
      } 
     } 
     $sql="SELECT *".$selectString." FROM `ssp_form` "; 

     $forms = $this->fetchAll($sql)->toArray(); 

Ainsi, après avoir fait cela, PhpMySQL retourne:

Showing rows 0 - 0 (1 total, Query took 0.0009 sec)

Alors que ZF renvoie

Mysqli prepare error: Operand should contain 1 column(s)

Je crois que la réponse devrait être assez trivial, mais je ne peux pas le comprendre.

Répondre

3

La raison est que vous utilisez fetchAll avec de mauvais arguments. Le premier argument doit être soit Zend_Db_Table_Select objet ou une chaîne qui va dans la clause WHERE (qui est vide dans votre cas).

Pour faire ce travail de recherche, vous devez utiliser une autre méthode:

$this->getAdapter()->query($sql); 

En dehors de cela, je vous suggère que vous refactoring votre requête SQL. Avoir ce nombre d'instructions SELECT est en train de tuer la lisibilité sinon la performance.

+0

Merci, cela a fonctionné! –

+0

@janis - il est important de savoir pourquoi cela fonctionne. Zend_Db_Table_Abstract :: fetchAll() attend le premier param comme 1 de 2 types: soit une simple piqûre à ajouter comme l'instruction where, soit un objet de type Zend_Db_Table_Select. La méthode getAdpter() retournera un Zend_Db_Adapter_Abstract et Zend_Db_Adapter_Abstract :: query() attend une instruction SQL exécutable ou un Zend_Db_Select – Fatmuemoo

Questions connexes