Yii缓存方法使用(二)

1)))))取出一系列数据:

1.视图页面:

<?php $indexAd = BasketBall::getList('ad','index_ad',array(

'where'=>"status_is='Y' AND title_alias='index_banner'",
'order'=>'sort_order DESC'))?>
<div class="banner">
<div class="bd">
<ul>


<?php foreach((array)$indexAd as $ad):?>
<?php if($ad['link_url']):?>


<li _src="url(<?php if($ad['image_url']):?><?php echo $ad['image_url']?>
<?php else:?><?php echo $this->_baseUrl?>/<?php echo $ad['attach_file']?>
<?php endif?>)" style="background:#DED5A1 center 0 no-repeat;">
<a target="_blank" href="<?php echo $ad['link_url']?>"></a></li>
<?php else:?>


<li _src="url(<?php if($ad['image_url']):?><?php echo $ad['image_url']?>
<?php else:?><?php echo $this->_baseUrl?>/<?php echo $ad['attach_file']?>
<?php endif?>)" style="background:#DED5A1 center 0 no-repeat;"></li>
<?php endif?>
<?php endforeach?>
</ul>
</div>
<div class="hd">
<ul>
</ul>
</div>
<span class="prev"></span><span class="next"></span></div>

2.取数据地方已经缓存使用BasketBall.php

public static function getList($model,$id='',$params=array()){
if(isset($params['cache'])){
$value=Yii::app()->cache->get($id);
if($value===false){
return self::_getList($model,$id,$params);
}
}else{
return self::_getList($model,$id,$params);
}

}
protected function _getList( $model = '', $id, $params = '' ) {
$model = ucfirst( $model );

$basektModel = new $model();
$params['limit'] && $array['limit'] = $params['limit'];
$params['where'] && $array['condition'] = $params['where'];
$params['order'] && $array['order'] = $params['order'];
$params['select'] && $array['select'] = $params['select'];
$params['offset'] && $array['offset'] = $params['offset'];
$params['with'] && $array['with'] = $params['with'];
$params['alias'] && $array['alias'] = $params['alias'];
$params['params'] && $array['params'] = $params['params'];
$params['joinType'] && $array['joinType'] = $params['joinType'];
$params['group'] && $array['group'] = $params['group'];

try {
if ( $params['xsql'] ) {
$dataGet = Yii::app()->db->createCommand( $params['xsql'] )->queryAll();
} else {
$dataGet = $basektModel->findAll( $array );
}
if ( $dataGet ) {
foreach ( (array) $dataGet as $key => $row ) {
foreach ( (array) self::_attributes( $params['select'], $basektModel ) as $attr ) {
$returnData[$key][$attr] = $row->$attr;
}
}
if ( $params['cache'] ) {
$cacheTime = empty( $params['cacheTime'] ) ? 3600 : $params['cacheTime'];
Yii::app()->cache->set( $id, $returnData, $cacheTime );
}
return (array) $returnData;
}else
return ;
} catch ( Exception $error ) {
echo __CLASS__.' 调用错误 --> 表名称: '. $model . '&nbsp;&nbsp;标识:' . $id ;
}
}

protected function _attributes( $fields = '', $model = '' ) {
if ( empty( $fields ) || trim( $fields ) == '*' ) {
return $model->attributeNames();
} else {
$fields = str_replace( ',', ',', $fields );
return explode( ',', $fields );
}
}

原文地址:https://www.cnblogs.com/fengzhiqiangcaisangzi/p/3361003.html