diff --git a/var/Typecho/Db/Query.php b/var/Typecho/Db/Query.php index 3a6f45d9..d110bb92 100644 --- a/var/Typecho/Db/Query.php +++ b/var/Typecho/Db/Query.php @@ -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; + } + /** * 连接表 * diff --git a/var/Widget/Abstract/Contents.php b/var/Widget/Abstract/Contents.php index b4cf541d..66b6a275 100644 --- a/var/Widget/Abstract/Contents.php +++ b/var/Widget/Abstract/Contents.php @@ -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']); diff --git a/var/Widget/Archive.php b/var/Widget/Archive.php index a63cccc8..d4504faf 100644 --- a/var/Widget/Archive.php +++ b/var/Widget/Archive.php @@ -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(