博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
yii框架中model映射数据库中不存在的表,做请求转发的接口
阅读量:7107 次
发布时间:2019-06-28

本文共 4086 字,大约阅读时间需要 13 分钟。

  hot3.png

一。将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;	}

转载于:https://my.oschina.net/rongruoxzhl/blog/391731

你可能感兴趣的文章
Java Basis
查看>>
让所有人都能看懂日志,解放开发
查看>>
url传值出现中文乱码的另类解决办法
查看>>
jdk、jre、spring、java ee、java se
查看>>
zabbix自动清理30天前的数据
查看>>
客户区和非客户区
查看>>
Android调用相册拍照控件实现系统控件缩放切割图片
查看>>
【转】Excel2010中绘制流程图
查看>>
"无法启动应用程序,工作组信息文件丢失,或是已被其他用户已独占方式打开"解决办法...
查看>>
【原】oracle11G AWR使用及分析
查看>>
<20190104>关掉一些鸡肋的Win10功能
查看>>
iOS网络篇4-利用NSURLConnection实现GET/POST/HEAD请求
查看>>
linux 环境下 eas console的运行
查看>>
Rainbows! 4.4.3 发布,修复 EventMachine 问题
查看>>
USACO翻译:USACO 2012 JAN三题(2)
查看>>
Java学习之IO流总结
查看>>
LightOJ 1038 Race to 1 Again
查看>>
php set_magic_quotes_runtime() 函数过时解决方法
查看>>
使用IDEA创建java项目(hello word)
查看>>
TCP/IP协议栈---网络基础篇(3)
查看>>