• 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 );
    }
    }

  • 相关阅读:
    e.target和e.event和event.srcElement
    js代码优化
    史上最全的CSS hack方式一览
    学习NodeJS第一天:node.js引言
    响应式布局
    HTML+CSS编写规范
    英文SEO外部链接资源收集之常用的footprints
    判别木马
    php简单命令代码集锦
    zencart 新页面调用好功能代码集:
  • 原文地址:https://www.cnblogs.com/fengzhiqiangcaisangzi/p/3361003.html
Copyright © 2020-2023  润新知