1.
试图页面代码:
<?php foreach((array)$this->_catalog as $key=>$row):?>
<?php if($row['parent_id'] == 0):?>
//导航一般只取出父id为0的。
<dd>
<h3><a href="<?php echo $this->createUrl('post/index',array(
'catalog'=>$row['catalog_name_alias'])) ?>">
<?php echo $row['catalog_name']?><i></i></a></h3>
<ul>
<?php foreach((array)Catalog::lite($row['id']) as $key=>$val):?>
<li><a href="<?php echo $this->_navLink($val)?>"><?php echo $val['catalog_name']?></a></li>
<?php endforeach?>
</ul>
</dd>
<?php endif?>
<?php endforeach?>
2.基类控制器的init方法里面(注意:继续父类初始化也要特别说明parent::init)
$this->_catalog = Catalog::get(0, XXcache::system('_catalog'));
//从缓存里面取出id为0,
3.XXcache.php:
<?php
class XXcache{
public static function system( $id, $expirse = 600, $fields = '', $params = array() ) {
$value = Yii::app()->cache->get( $id );
if ( $value === false ) {
return self::_refresh( $id, $expirse, $fields, $params );
} else {
return $value;
}
}
public function _refresh( $id, $expirse, $fields, $params ) {
try {
switch ( $id ) {
case '_link':
$data = (array) self::_base( 'Link', $fields, array ( 'condition' => 'status_is=1' , 'order' => 'sort_order DESC,id DESC' ) );
self::set( $id, $data, $expirse );
break;
case '_pca':
$data = (array) self::_base( 'Pca', $fields, array ( 'condition' => "status_is='Y'" , 'order' => 'id ASC' ) );
self::set( $id, $data, $expirse );
break;
case '_userGroup':
$data = (array) self::_base( 'UserGroup', $fields );
self::set( $id, $data, $expirse );
break;
case '_ad':
$data = (array) self::_base( 'Ad', $fields, array ( 'condition' => 'status_is=1' , 'order' => 'sort_order DESC,id DESC' ) );
self::set( $id, $data, $expirse );
break;
case '_config':
$data = (array) self::_config( $params );
self::set( $id, $data, $expirse );
break;
case '_catalog':
$data = (array) self::_base( 'Catalog', $fields, array ( 'condition' => 'status_is=1' , 'order' => 'sort_order DESC,id DESC' ) );
self::set( $id, $data, $expirse );
break;
default:
throw new Exception( '数据不在接受范围' );
break;
}
return $data;
} catch ( Exception $error ) {
exit( $error->getMessage() );
}
}
protected function _base( $id = '', $fields = '', $condition = '' ) {
$mod = ucfirst( $id );
$model = new $mod();
$dataGet = $model->findAll( $condition );
foreach ( (array) $dataGet as $key => $row ) {
foreach ( (array) self::_attributes( $fields, $model ) as $attr ) {
$returnData[$key][$attr] = $row->$attr;
}
}
return $returnData;
}
protected function _attributes( $fields, $model = '' ) {
if ( empty( $fields ) || trim( $fields ) == '*' ) {
return $model->attributeNames();
} else {
$fields = str_replace( ',', ',', $fields );
return explode( ',', $fields );
}
}
}
4.catalog.php
/**
* 取分类
*/
static public function get($parentid = 0, $array = array(), $level = 0, $add = 2, $repeat = '---') {
$str_repeat = '';
if ($level) {
for($j = 0; $j < $level; $j ++) {
$str_repeat .= $repeat;
}
}
$newarray = array ();
$temparray = array ();
foreach ( ( array ) $array as $v ) {
if ($v ['parent_id'] == $parentid) {
$newarray [] = array ('id' => $v ['id'], 'catalog_name' => $v ['catalog_name'],
'catalog_name_alias' => $v ['catalog_name_alias'],
'parent_id' => $v ['parent_id'], 'level' => $level, 'sort_order' => $v ['sort_order'],
'seo_keywords' => $v ['seo_keywords'], 'seo_description' => $v ['seo_description'],
'attach_file' => $v ['attach_file'], 'attach_thumb' => $v ['attach_thumb'],
'status_is' => $v ['status_is'], 'data_count' => $v ['data_count'] ,
'display_type' => $v ['display_type'], 'menu_is' => $v ['menu_is'],
'template_list' => $v ['template_list'],'acl_browser' => $v ['acl_browser'],
'acl_operate' => $v ['acl_operate'],'template_page' => $v ['template_page'],
'template_show' => $v ['template_show'],'create_time' => $v ['create_time'],
'str_repeat' => $str_repeat, 'page_size'=>$v['page_size'] );
$temparray = self::get ( $v ['id'], $array, ($level + $add) );
if ($temparray) {
$newarray = array_merge ( $newarray, $temparray );
}
}
}
return $newarray;
}
/**
* 获取下级子类,普通模式
*
* @param $parentId
* @param array $array
* @return array
*/
static public function lite ($parentId, array $array = array(), $params = array())
{
if(empty($parentId))
return ;
$eachArr = empty($array)? XXcache::system('_catalog', 86400): $array;
foreach ((array)$eachArr as $row) {
if ($row['parent_id'] == $parentId)
$arr[] = $row;
}
return $arr;
}