2011-03-04 70 views
0

我有一个非常大的查询,它是在脚本中生成的,用于创建类似于视图的东西。对于应用程序。查询在PhpMySQL中执行,但不在Zend_Framework中

这里是正在生成的查询:

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` 

这里是代码,正在生成它:

$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(); 

所以,这样做后,PhpMySQL返回:

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

虽然ZF返回

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

我认为答案应该是相当微不足道,但我无法弄清楚。

回答

3

原因是你使用fetchAll的参数错误。第一个参数应该是Zend_Db_Table_Select对象或进入WHERE子句的字符串(在您的情况下为空)。

为了使这一工作查询,则需要使用不同的方法:

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

除此之外,我建议你重构你的SQL查询。有很多SELECT语句如果不是性能,就会导致可读性下降。

+0

谢谢,这工作! – 2011-03-04 19:54:48

+0

@janis - 重要的是要知道为什么这会起作用。 Zend_Db_Table_Abstract :: fetchAll()期望第一个参数为2种类型中的一种:要么添加一个简单的sting作为where语句,要么添加一个类型为Zend_Db_Table_Select的对象。 getAdpter()方法将返回一个Zend_Db_Adapter_Abstract,而Zend_Db_Adapter_Abstract :: query()需要一个可执行的SQL语句或一个Zend_Db_Select – Fatmuemoo 2011-03-05 01:01:02