在PhpMySQL中执行的查询,但在Zend_Framework中没有

时间:2011-03-04 19:13:38

标签: mysql 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)

我相信答案应该是相当微不足道的,但我无法理解。

1 个答案:

答案 0 :(得分:3)

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

要使此查询起作用,您需要使用其他方法:

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

除此之外,我建议你重构你的SQL查询。拥有这么多SELECT语句会破坏可读性,如果不是性能的话。