A query executing in PhpMySQL, but not in Zend_Framework
I have a pretty big query, that is generated in in a script, to create something like a view. For an application.
Here is the query that is being generated:
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`
And here is the code, that is generating it:
$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();
So, after doing this, PhpMySQL returns:
Showing rows 0 - 0 (1 total, Query took 0.0009 sec)
While ZF returns
Mysqli prepare error: Operand should contain 1 column(s)
I believe that the answer should be fairly trivial, but I just can't figure it out.
The reason is that you're using fetchAll
with wrong arguments. The first argument should be either Zend_Db_Table_Select
object or a string that goes into WHERE
clause (which is empty in your case).
To make this query work, you need to use a different method:
$this->getAdapter()->query( $sql );
Apart from that, I'd suggest that you refactor your sql query. Having this many SELECT
statements is killing readability if not performance.
精彩评论