关于qeephp 框架分页函数getPagination存在问题的修改方法
时间:2010-08-17 来源:旋风
将qeephp\library\db\select.php函数getPagination()中的
$count = (int)$this->_conn->execute($this->__toString())->fetchOne();
替换为
if(empty($this->_parts[self::GROUP])){
$count = (int)$this->_conn->execute($this->__toString())->fetchOne();
}else{
$count_ar = $this->_conn->execute($this->__toString())->fetchAll();
$count = count($count_ar);
}
原因我只贴出代码前面一段(原版)
function getPagination()
{
$this->_query_params[self::PAGED_QUERY] = true;
//此属性只是用来 判断是否是获取分页 的
if (! empty($this->_parts[self::ORDER]))
{
$order = $this->_parts[self::ORDER];
unset($this->_parts[self::ORDER]);
}//去掉不必须的order参数
$count = (int)$this->_conn->execute($this->__toString())->fetchOne();
//以项为重点分析对象见下面,此时sql语句为select COUNT(*) ...,得到COUNT(*)的值,
当sql语句中group by 时,必然只会得到第一个group by 的COUNT(*)值是不对,修改上面已提到!
$this->_query_params[self::PAGED_QUERY] = false;//关闭分页查询
if (isset($order))
{
$this->_parts[self::ORDER] = $order;
}...
}
下面来分析在未关闭分布查询之前,也就是 $this->_query_params[self::PAGED_QUERY] = false;以前的
到数据库查询sql语句是什么?我们来分析__toString()函数得到是什么吧!
function __toString()
{
$sql = array(
self::SQL_SELECT
);//sql=array('SELECT'),我们在定义中可以找到
/**
此时的下面的self::$parts_init值是什么呢?可以找到定义
protected static $_parts_init = array
(
self::DISTINCT => false,//"distinct"=>false,
self::COLUMNS => array(),//'columns'=>array(),
self::AGGREGATE => array(),//下面就不写了....
self::UNION => array(),
self::FROM => array(),
self::WHERE => null,
self::GROUP => array(),
self::HAVING => null,
self::ORDER => array(),
self::LIMIT_COUNT => 1,
self::LIMIT_OFFSET => null,
self::LIMIT_QUERY => false,
self::FOR_UPDATE => false
);
*/
foreach (array_keys(self::$_parts_init) as $part)
{
if ($part == self::FROM)
{
$sql[self::FROM] = '';
}
else
{//我们只看下$part='columns'的情况
$method = '_render' . ucfirst($part);
if (method_exists($this, $method))
{
$sql[$part] = $this->$method();
//此时$sql['columns']=$this->_renderColumns();下面我再看_renderColumns函数吧!
}
}
}
$sql[self::FROM] = $this->_renderFrom();
foreach ($sql as $offset => $part)
{
if (trim($part) == '')
{
unset($sql[$offset]);
}
}
return implode(' ', $sql);
}
函数 protected function _renderColumns()
{
if (empty($this->_parts[self::COLUMNS]))
{
return '';
}
if ($this->_query_params[self::PAGED_QUERY])//我们可以看到$this->_query_params['paged_query']的作用了
{
return 'COUNT(*)';
}//相当于sql语名为select COUNT(*) ...
// $this->_parts[self::COLUMNS] 每个元素的格式
$columns = array();
foreach ($this->_parts[self::COLUMNS] as $entry)//这是为正常情况下columns处理
{.....
}
}
关于修改后
if(empty($this->_parts[self::GROUP])){//判断是否添加了group条件,没有执行以前的
$count = (int)$this->_conn->execute($this->__toString())->fetchOne();
}else{//有的话,就用下面的,应该很容易理解吧!
$count_ar = $this->_conn->execute($this->__toString())->fetchAll();
$count = count($count_ar);
}
$count = (int)$this->_conn->execute($this->__toString())->fetchOne();
替换为
if(empty($this->_parts[self::GROUP])){
$count = (int)$this->_conn->execute($this->__toString())->fetchOne();
}else{
$count_ar = $this->_conn->execute($this->__toString())->fetchAll();
$count = count($count_ar);
}
原因我只贴出代码前面一段(原版)
function getPagination()
{
$this->_query_params[self::PAGED_QUERY] = true;
//此属性只是用来 判断是否是获取分页 的
if (! empty($this->_parts[self::ORDER]))
{
$order = $this->_parts[self::ORDER];
unset($this->_parts[self::ORDER]);
}//去掉不必须的order参数
$count = (int)$this->_conn->execute($this->__toString())->fetchOne();
//以项为重点分析对象见下面,此时sql语句为select COUNT(*) ...,得到COUNT(*)的值,
当sql语句中group by 时,必然只会得到第一个group by 的COUNT(*)值是不对,修改上面已提到!
$this->_query_params[self::PAGED_QUERY] = false;//关闭分页查询
if (isset($order))
{
$this->_parts[self::ORDER] = $order;
}...
}
下面来分析在未关闭分布查询之前,也就是 $this->_query_params[self::PAGED_QUERY] = false;以前的
到数据库查询sql语句是什么?我们来分析__toString()函数得到是什么吧!
function __toString()
{
$sql = array(
self::SQL_SELECT
);//sql=array('SELECT'),我们在定义中可以找到
/**
此时的下面的self::$parts_init值是什么呢?可以找到定义
protected static $_parts_init = array
(
self::DISTINCT => false,//"distinct"=>false,
self::COLUMNS => array(),//'columns'=>array(),
self::AGGREGATE => array(),//下面就不写了....
self::UNION => array(),
self::FROM => array(),
self::WHERE => null,
self::GROUP => array(),
self::HAVING => null,
self::ORDER => array(),
self::LIMIT_COUNT => 1,
self::LIMIT_OFFSET => null,
self::LIMIT_QUERY => false,
self::FOR_UPDATE => false
);
*/
foreach (array_keys(self::$_parts_init) as $part)
{
if ($part == self::FROM)
{
$sql[self::FROM] = '';
}
else
{//我们只看下$part='columns'的情况
$method = '_render' . ucfirst($part);
if (method_exists($this, $method))
{
$sql[$part] = $this->$method();
//此时$sql['columns']=$this->_renderColumns();下面我再看_renderColumns函数吧!
}
}
}
$sql[self::FROM] = $this->_renderFrom();
foreach ($sql as $offset => $part)
{
if (trim($part) == '')
{
unset($sql[$offset]);
}
}
return implode(' ', $sql);
}
函数 protected function _renderColumns()
{
if (empty($this->_parts[self::COLUMNS]))
{
return '';
}
if ($this->_query_params[self::PAGED_QUERY])//我们可以看到$this->_query_params['paged_query']的作用了
{
return 'COUNT(*)';
}//相当于sql语名为select COUNT(*) ...
// $this->_parts[self::COLUMNS] 每个元素的格式
$columns = array();
foreach ($this->_parts[self::COLUMNS] as $entry)//这是为正常情况下columns处理
{.....
}
}
关于修改后
if(empty($this->_parts[self::GROUP])){//判断是否添加了group条件,没有执行以前的
$count = (int)$this->_conn->execute($this->__toString())->fetchOne();
}else{//有的话,就用下面的,应该很容易理解吧!
$count_ar = $this->_conn->execute($this->__toString())->fetchAll();
$count = count($count_ar);
}
相关阅读 更多 +