_countSelect !== null) { return $this->_countSelect; } $rowCount = clone $this->_select; $rowCount->__toString(); // Workaround for ZF-3719 and related $db = $rowCount->getAdapter(); $countColumn = $db->quoteIdentifier($db->foldCase(self::ROW_COUNT_COLUMN)); $countPart = 'COUNT(1) AS '; $groupPart = null; $unionParts = $rowCount->getPart(Zend_Db_Select::UNION); /** * If we're dealing with a UNION query, execute the UNION as a subquery * to the COUNT query. */ if (!empty($unionParts)) { $expression = new Zend_Db_Expr($countPart . $countColumn); /** * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! * !! UNION FIX START * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ $rowCount->reset(Zend_Db_Select::LIMIT_COUNT); $rowCount->reset(Zend_Db_Select::LIMIT_OFFSET); $rowCount->reset(Zend_Db_Select::ORDER); /** * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! * !! UNION FIX FINISH * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ $rowCount = $db ->select() ->bind($rowCount->getBind()) ->from($rowCount, $expression); } else { $columnParts = $rowCount->getPart(Zend_Db_Select::COLUMNS); $groupParts = $rowCount->getPart(Zend_Db_Select::GROUP); $havingParts = $rowCount->getPart(Zend_Db_Select::HAVING); $isDistinct = $rowCount->getPart(Zend_Db_Select::DISTINCT); /** * If there is more than one column AND it's a DISTINCT query, more * than one group, or if the query has a HAVING clause, then take * the original query and use it as a subquery os the COUNT query. */ if (($isDistinct && ((count($columnParts) == 1 && $columnParts[0][1] == Zend_Db_Select::SQL_WILDCARD) || count($columnParts) > 1)) || count($groupParts) > 1 || !empty($havingParts)) { $rowCount->reset(Zend_Db_Select::ORDER); $rowCount = $db ->select() ->bind($rowCount->getBind()) ->from($rowCount); } else if ($isDistinct) { $part = $columnParts[0]; if ($part[1] !== Zend_Db_Select::SQL_WILDCARD && !($part[1] instanceof Zend_Db_Expr)) { $column = $db->quoteIdentifier($part[1], true); if (!empty($part[0])) { $column = $db->quoteIdentifier($part[0], true) . '.' . $column; } $groupPart = $column; } } else if (!empty($groupParts)) { $groupPart = $db->quoteIdentifier($groupParts[0], true); } /** * If the original query had a GROUP BY or a DISTINCT part and only * one column was specified, create a COUNT(DISTINCT ) query instead * of a regular COUNT query. */ if (!empty($groupPart)) { $countPart = 'COUNT(DISTINCT ' . $groupPart . ') AS '; } /** * Create the COUNT part of the query */ $expression = new Zend_Db_Expr($countPart . $countColumn); $rowCount->reset(Zend_Db_Select::COLUMNS) ->reset(Zend_Db_Select::ORDER) ->reset(Zend_Db_Select::LIMIT_OFFSET) ->reset(Zend_Db_Select::GROUP) ->reset(Zend_Db_Select::DISTINCT) ->reset(Zend_Db_Select::HAVING) ->columns($expression); } $this->_countSelect = $rowCount; return $rowCount; } }