设计数据库字段常常会使用1,2,3来表明数据记录的状态

1.若是在设计数据库字段常常会使用1,2,3来表明数据记录的状态,在程序中使用过的时候又不容易记住涵义,
 须要将数字转换成为相对应的状态名称。下面有两种方法能够解决这个问题:
 第一种方法:在相对应的model中定义一个函数(例如UserModel)php

 private static $_items = array();
 public static function loadItems($type,$code=null)
{
    self::$_items = array(
        'status' => array(
            '1' => Yii::t('dh','开启'),
            '2' => Yii::t('dh','关闭'),
        ),
         'type' => array(
            '1' => Yii::t('dh','审核'),
            '2' => Yii::t('dh','结束'),
           '3' => Yii::t('dh','待审核'),
        ),
    );
    return $code ? self::$_items[$type][$code] : self::$_items[$type];
}

  



数据获取方法:
User::model()->loadItems('status',2);

第二中方法:定义静态变量数据库

public static $status =  array(
            '1' => Yii::t('dh','开启'),
            '2' => Yii::t('dh','关闭'),
        );
public static $type = array(
            '1' => Yii::t('dh','审核'),
            '2' => Yii::t('dh','结束'),
            '3' => Yii::t('dh','待审核'),
        );


数据获取方法:app

User::$status[2];函数

第三种方法:this

视图设计

<input type="button" class="but_downs" id="shouluzt" 
value="<?php if(isset($_GET['type_id'])) echo $type[$_GET['type_id']]['name']; else echo '所有'; ?>" /> //经过id去取他的值
控制器
$arrType = ContentType::model()->getTypeById();
'type' => $arrType,

 

类    public function getTypeById(){
	 $cache_name=md5('model_ContentType_getTypeById');
	 $data=Yii::app()->memcache->get($cache_name);
	 if(!$data){
	    $result=$this->findAll('status=:status',array(':status'=>1));
		if(!empty($result)){
		  foreach($result as $val){
		      $data[$val->id]['code']=$val->code;
			  $data[$val->id]['name']=$val->name;
		  }
		}
		Yii::app()->memcache->set($cache_name,$data,3000);
	 }
	 return $data;
	}
相关文章
相关标签/搜索