ZF-1100: Refactor Zend_Db_Select::__toString() method


Extending behavior of Zend_Db_Select for rendering a query to a string, or supporting RDBMS-specific syntax, requires rewriting the whole __toString() method.

What I think we need to do is make it easier to extend Zend_Db_Select by breaking __toString() down into separate methods for each section of the statement, and allow setting of the class returned by select() in the adapter.

Then for situations such as DISTINCT ON support, the solution would be the following:

class MyExtendedSelect extends Zend_Db_Select 
     public function distinctOn(array $columns)
         $this->_parts[self::DISTINCT] = $columns;
         return $this;

     protected function _distinctToString() {
         if (is_array($this->_parts[self::DISTINCT])) {
             $sql .= " DISTINCT ON " . /* serialize columns here */;
    } else if ($this->_parts[self::DISTINCT] === true) {
             $sql .= " DISTINCT";


Assigning to Mark Gibson. Thanks Mark!

I wrote a prototype implementation of this refactoring. It's in the ZF Laboratory svn repository:


Resolved in trunk r8084

NOTE: The method names are simply:-

_renderFrom() _renderWhere() ...etc..

(the 'toString' seemed superfluous)