« Return to Thread: Adding SELECT options to Zend_Db_Select

Re: Adding SELECT options to Zend_Db_Select

by Bill Karwin :: Rate this Message:

Reply to Author | View in Thread

Hector Virgen wrote:
I've tried implementing your solution but I'm having trouble with the
late static binding of the array Zend_Db_Select::$_partsInit.

<?php

require_once 'Zend/Db/Adapter/Abstract.php';
require_once 'Zend/Db/Select.php';

class Zend_Db_Select_Mysql extends Zend_Db_Select
{

    const SQL_CALC_FOUND_ROWS = 'sqlCalcFoundRows';
    // add other options as needed

    public function __construct(Zend_Db_Adapter_Abstract $adapter)
    {
        /**
         * Use array_merge() instead of simply setting a key
         * because the order of keys is significant to the
         * rendering of the query.
         */
        self::$_partsInit = array_merge(
            array(
                self::SQL_CALC_FOUND_ROWS => false
                // add other options as needed
            ),
            self::$_partsInit
        );
        parent::__construct($adapter);
    }

    public function sqlCalcFoundRows($flag = true)
    {
        $this->_parts[self::SQL_CALC_FOUND_ROWS] = (bool) $flag;
        return $this;
    }

    protected function _renderSqlCalcFoundRows($sql)
    {
        if ($this->_parts[self::SQL_CALC_FOUND_ROWS]) {
            $sql .= ' SQL_CALC_FOUND_ROWS';
        }

        return $sql;
    }

}


Quick & dirty test code:

$db = Zend_Db::factory('mysqli', array('dbname'=>'test', 'username'=>'root', 'password'=>'xxxx'));

$select = new Zend_Db_Select_Mysql($db);
$select->from('foo');
$select->sqlCalcFoundRows();

print "SQL = " . $select . "\n";


Hector Virgen wrote:
And, yes, I actually use the extra options in my select queries.

I'm shocked and amazed!  (see I told you I would be :-)


Regards,
Bill Karwin

 « Return to Thread: Adding SELECT options to Zend_Db_Select