/** * 取得对应分类下面商品的总数,用于分页显示 * * @return int 商品总数 * * @param int $categoryId 分类的ID * @param int $level 取得多少层,子分类有可能很深,我们只取有限层次 * @param string $systemTag 系统标记 * @param int $ttl 缓存时间 */ public function countGoodsArray($categoryId, $level, $systemTag, $ttl = 0) { // 参数验证 $validator = new Validator(array('categoryId' => $categoryId, 'level' => $level, 'systemTag' => $systemTag, 'ttl' => $ttl)); $categoryId = $validator->digits()->min(0)->validate('categoryId'); $level = $validator->required()->digits()->min(1)->validate('level'); $systemTag = $validator->validate('systemTag'); $ttl = $validator->digits()->min(0)->validate('ttl'); $this->validate($validator); $childrenIdArray = $this->fetchCategoryChildrenIdArray($categoryId, $level, $ttl); $childrenIdArray[] = $categoryId; // 加入父节点 $queryCondArray = array(); $queryCondArray[] = array('is_delete = 0 AND is_on_sale = 1 AND is_alone_sale = 1'); // 构建 SQL 的 in 语句, cat_id in (100,20,30) $queryCondArray[] = array(QueryBuilder::buildInCondition('cat_id', $childrenIdArray, \PDO::PARAM_INT)); if (!empty($systemTag)) { $queryCondArray[] = array('system_tag_list like ? ', '%' . Utils::makeTagString(array($systemTag)) . '%'); } $dataMapper = new DataMapper('goods'); return $dataMapper->count(QueryBuilder::buildAndFilter($queryCondArray), null, $ttl); }