修正分类归档计数错误

This commit is contained in:
joyqi 2014-02-25 11:58:20 +08:00
parent 8e7bf64397
commit 4cd7184b5b
3 changed files with 57 additions and 24 deletions

View File

@ -26,6 +26,26 @@ class Typecho_Db_Query
/** 数据库关键字 */
const KEYWORDS = '*PRIMARY|AND|OR|LIKE|BINARY|BY|DISTINCT|AS|IN|IS|NULL';
/**
* 默认字段
*
* @var array
* @access private
*/
private $_default = array(
'action' => NULL,
'table' => NULL,
'fields' => '*',
'join' => array(),
'where' => NULL,
'limit' => NULL,
'offset' => NULL,
'order' => NULL,
'group' => NULL,
'having' => NULL,
'rows' => array(),
);
/**
* 数据库适配器
*
@ -60,19 +80,7 @@ class Typecho_Db_Query
$this->_adapter = &$adapter;
$this->_prefix = $prefix;
$this->_sqlPreBuild = array(
'action' => NULL,
'table' => NULL,
'fields' => '*',
'join' => array(),
'where' => NULL,
'limit' => NULL,
'offset' => NULL,
'order' => NULL,
'group' => NULL,
'having' => NULL,
'rows' => array(),
);
$this->_sqlPreBuild = $this->_default;
}
/**
@ -203,6 +211,21 @@ class Typecho_Db_Query
return isset($this->_sqlPreBuild[$attributeName]) ? $this->_sqlPreBuild[$attributeName] : NULL;
}
/**
* 清除查询字串属性值
*
* @access public
* @param string $attributeName 属性名称
* @return Typecho_Db_Query
*/
public function cleanAttribute($attributeName)
{
if (isset($this->_sqlPreBuild[$attributeName])) {
$this->_sqlPreBuild[$attributeName] = $this->_default[$attributeName];
}
return $this;
}
/**
* 连接表
*

View File

@ -576,7 +576,10 @@ class Widget_Abstract_Contents extends Widget_Abstract
*/
public function size(Typecho_Db_Query $condition)
{
return $this->db->fetchObject($condition->select(array('COUNT(table.contents.cid)' => 'num'))->from('table.contents'))->num;
return $this->db->fetchObject($condition
->select(array('COUNT(DISTINCT table.contents.cid)' => 'num'))
->from('table.contents')
->cleanAttribute('group'))->num;
}
/**
@ -612,18 +615,24 @@ class Widget_Abstract_Contents extends Widget_Abstract
public function filter(array $value)
{
/** 取出所有分类 */
$value['categories'] = $this->db->fetchAll($this->db
->select()->from('table.metas')
->join('table.relationships', 'table.relationships.mid = table.metas.mid')
->where('table.relationships.cid = ?', $value['cid'])
->where('table.metas.type = ?', 'category')
->order('table.metas.order', Typecho_Db::SORT_ASC), array($this->widget('Widget_Metas_Category_List'), 'filter'));
if ('post' == $value['type']) {
$value['categories'] = $this->db->fetchAll($this->db
->select()->from('table.metas')
->join('table.relationships', 'table.relationships.mid = table.metas.mid')
->where('table.relationships.cid = ?', $value['cid'])
->where('table.metas.type = ?', 'category')
->order('table.metas.order', Typecho_Db::SORT_ASC), array($this->widget('Widget_Metas_Category_List'), 'filter'));
}
$value['category'] = NULL;
$value['directory'] = array();
/** 取出第一个分类作为slug条件 */
$value['category'] = $value['categories'][0]['slug'];
if (!empty($value['categories'])) {
$value['category'] = $value['categories'][0]['slug'];
$value['directory'] = $this->widget('Widget_Metas_Category_List')->getAllParents($value['category']['mid']);
$value['directory'][] = $value['category'];
$value['directory'] = $this->widget('Widget_Metas_Category_List')->getAllParents($value['categories'][0]['mid']);
$value['directory'][] = $value['category'];
}
$value['date'] = new Typecho_Date($value['created']);

View File

@ -890,7 +890,8 @@ class Widget_Archive extends Widget_Abstract_Contents
/** fix sql92 by 70 */
$select->join('table.relationships', 'table.contents.cid = table.relationships.cid')
->where('table.relationships.mid IN ?', $children)
->where('table.contents.type = ?', 'post');
->where('table.contents.type = ?', 'post')
->group('table.contents.cid');
/** 设置分页 */
$this->_pageRow = array_merge($category, array(