简体   繁体   中英

Zend_Db PDO exception arises with a mySQL View?

After updating from Zend 1.12.1 to 1.12.3 and turning x_debug on and making OLD_PASSWORD the default .

I rerun a program that used to run and seamlessly and it now has a fatal PDOException , detailed below:

Error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'place_fcb.userid' in 'group statement'

Actual SQl Query:

SELECT `C`.* 
FROM (SELECT `place_fcb`.* FROM `place_fcb`) 
AS `C` 
WHERE (place_type_id != '176') 
GROUP BY place_fcb.userid
HAVING (place_fcb.pda >= '2009-12-21') 
AND (place_fcb.pda <= '2010-01-20')

Zend_Db_table_Abstract:

 public function getPlacefcb($start_date = '', $end_date = '', $r_id = '')
    {
        $sub_sql = $this->select()->from('place_fcb');

        $select = $this->select()
            ->setIntegrityCheck(false)
            ->from(array('C' => new Zend_Db_Expr('('.$sub_sql.')')))
            ->where("place_type_id != '176'")
            ->group('place_fcb.userid');

        if($start_date != '')
            $select->having('`place_fcb`.`pda` >= ?', $start_date);

        if($end_date != '')
            $select->having('`place_fcb`.`pda` <= ?', $end_date);

        if($region_id != '')
            $select->having('`place_fcb`.`r_id` = ?', $r_id);

        return $rowset = $this->fetchAll($select);
    }

MySQL of place_fcb:

place_fcb is a VIEW not a table. It has the following relevant fields

place_id
r_id
place_type_id
pda
userid

I can remove the exception and return the same results by changing the SQl statement to:

    SELECT `C`.* 
    FROM (SELECT `place_fcb`.* FROM `place_fcb`) 
    AS `C` 
    WHERE (place_type_id != '176') 
    GROUP BY userid
    HAVING (pda >= '2009-12-21') 
    AND (pda <= '2010-01-20')

ie. removing all the table suffixes...

but how will I fix it with Zend? and why is it going wrong all of a sudden?

Why does your query use the subquery:

->from(array('C' => new Zend_Db_Expr('('.$sub_sql.')')))

Instead of simply:

->from('place_fcb')

With the former, there is no place_fcb table in the outer query; hence so-qualified references fail (and always would, so I can't see how it used to work for you prior to this upgrade).

If you insist on keeping the subquery, you could rename the subquery's alias from 'C' to 'place_fcb' , although that is likely to cause confusion in the long term; better to rename the qualified references from 'place_fcb' to 'C' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM