一。将model映射一个数据库中不存在的表,需要重写父类的model方法:
/**
* Returns the static model of the specified AR class.
* @param String $className:openplatform_log
* @return CActiveRecord the static model class
*/
public static function model($className=__CLASS__){
//此model类只做请求的转发,将读写请求转发至统一日志,此model不对应数据库中具体的类
//model对应的表在RMS数据库中不存在,不能调用父类的model方法,顾重写model方法
//return parent::model($className);
return new $className(null);
}
new $className(null) yii不会向数据库中去查找对应的表
二。如果需要用model类的save方法保存信息到另一个系统的数据库接口中,需要重写getDefaultAttributes、__construct等方法:
//属性数组
private $attributes = array();
/**
* The followings are the available columns in table ‘openplatform_log‘:
* @var integer $id
* @var string $ip
* @var string $log_time
* @var string $type
* @var integer $app_key
* @var integer $user_id
* @var string $http_method
* @var integer $status
* @var integer $time_span
* @var string $content
* @var string $create_time
*/
/**
* @param null
* @return null
*/
public function init(){
}
/**
* @param array $attributes
* @return null
*/
public function __construct($attributes=array()){
$attributes = $attributes ? $attributes : $this->getDefaultAttributes();
foreach($attributes as $key => $val){
$this->{$key} = $val;
}
}
/**
* @param null
* @return array()
*/
public function relations(){
return array();
}
/**
* @param null
* @return array
*/
public function getDefaultAttributes(){
return $this->attributes = array(
‘id‘=>null,
‘ip‘ => null,
‘log_time‘ => null,
‘type‘ => null,
‘app_key‘ => null,
‘user_id‘ => null,
‘http_method‘ => null,
‘status‘ => null,
‘time_span‘ => null,
‘content‘ => null,
‘create_time‘ => null,
);
}
/**
* @param String $name:ip
* @param String $value:1.1.1.1
* @return null
*/
public function __set($name,$value){
if(property_exists($this,$name)){
$this->$name=$value;
}else{
$this->attributes[$name] = $value;
}
}
/**
* @param String $name:ip
* @return null
*/
public function __get($name){
if(property_exists($this,$name)){
return $this->{$name};
}
if(isset($this->attributes[$name])){
return $this->attributes[$name];
}
return null;
}
/**
* @param String $name:ip
* @return bool:true
*/
public function __isset($name){
return property_exists($this,$name) || in_array($name,array_keys($this->attributes));
}
三。重写的save()方法:
/**
* @param bool $runValidation:bool
* @param array $attributes:null
* @param string $log_type:openplatform_log
* @return bool
*/
public function save($runValidation = true,$attributes = null,$log_type = ‘openplatform_log‘){
$url = UNILOG_BASE_URL."api.php";
$AmsUtil = new AmsUtil();
$arrLogParams = array(
‘ip‘ => $this->ip,
‘log_time‘ => $this->log_time,
‘type‘ => $this->type,
‘app_key‘ => $this->app_key,
‘user_id‘ => $this->user_id,
‘http_method‘ => $this->http_method,
‘status‘ => $this->status,
‘time_span‘ => $this->time_span,
‘content‘ => $this->content,
‘create_time‘ => date("Y-m-d H:i:s"),
);
$strLogParams = json_encode($arrLogParams);
$arrSaveParams = array(‘handler‘=>‘saveUnifiedLog‘,‘log‘=>$strLogParams);
$returns = $AmsUtil->post($url,$arrSaveParams,2);
$returns = json_decode($returns);
if(isset($returns) && isset($returns->status) && $returns->status == 0){
return true;
}else{
return json_encode($returns);
}
}
四。重写的findAll()方法:
/**
* @params Object $criteria:
* $criteria = new CDbCriteria();
* $criteria->select = "*";
* $criteria->order = ‘id desc‘;
* @return String $returns:
* {
* {
* id: "22988023",
* ip: "10.81.5.232",
* log_time: "2015-03-25 15:35:10",
* type: "Auth.AccessToken.respsonse",
* app_key: "42",
* user_id: "0",
* http_method: "",
* status: "0",
* time_span: "92",
* create_time: "0000-00-00 00:00:00"
* }
* }
*/
public function findAll($criteria){
$url = UNILOG_BASE_URL."index.php?r=Log/GetOpenPlatformLog";
$strCriteria = serialize($criteria);
$AmsUtil = new AmsUtil();
$arrParams = array(‘criteria‘ => $strCriteria);
$returns = $AmsUtil->post($url,$arrParams,2);
return $returns;
}
yii框架中model映射数据库中不存在的表,做请求转发的接口
原文:http://my.oschina.net/rongruoxzhl/blog/391731