/** * 数据库操作基类,基于Yii框架。继承后即可使用Yii框架中的操作数据库的方式。 * 操作数据库可以分为如下三类(可以参考Yii2中的使用说明) * 一、Command操作,以commandDone函数结尾。 * Model::getInstance()->createCommand({查询语句})->queryAll()->commandDone(); * Model::getInstance()->createCommand({修改语句})->execute()->commandDone(); * Model::getInstance()->createCommand()->insert('{表名}', ['{字段}'=>'{值}'], '{条件}')->execute()->commandDone(); * Model::getInstance()->createCommand()->update('{表名}', ['{字段}'=>'{值}'], '{条件}')->execute()->commandDone(); * Model::getInstance()->createCommand()->delete('{表名}','{条件}')->execute()->commandDone(); * 二、ActiveRecord操作,以recordDone函数结尾。 * 查询 * Model::find()->select('f_id,name')->where([f_id'=>'5'])->limit(10)->all()->recordDone(); * Model::findOne(3)->recordDone(); * Model::findAll([1,3,5])->recordDone(); * Model::findAll(['f_id'=>3])->recordDone(); * 插入 * $obj = Model::getInstance(); * $obj->name = '西米'; * $obj->save()->recordDone(); * 更新 * $obj = Model::findOne(7); * $obj->name = '西米123'; * $obj->save()->recordDone(); * 删除 * $obj = Model::findOne(7); * $obj->delete()->recordDone(); * 递增 * Model::updateAllCounters(['f_create_time' => 10])->recordDone(); * 三、Query操作,以queryDone函数结尾 * Model::getInstance()->select('f_id,f_name,f_email')->from('t_transact_user_info')->where(['f_id'=>'1513'])->limit(10)->all()->queryDone(); */
class Dg_Db_Yii extends Dg_Db_Hnb {
private $_callArr = array();
public function __call($fun, $arguments) {
$this->_callArr[$fun] = !empty($arguments) ? $arguments : '';
return $this;
}
private static $_callStaticArr = array();
public function __callStatic($fun, $arguments) {
$class = get_called_class();
$self = new $class();
self::$_callStaticArr[$fun] = !empty($arguments) ? $arguments : '';
return $self;
}
private $_callSetArr = array();
public function __set($name, $value) {
$this->_callSetArr[$name] = $value;
}
/** * 执行 */
private function done($type = 'index') {
$this->init();
// 请求
$requestParam = array(
'_call' => $this->_callArr,
'_callStatic' => self::$_callStaticArr,
'_callSet' => $this->_callSetArr,
);
$url = $this->getRequestUrl('query', $type);
$result = $this->getRequestText($url, $requestParam);
$result = json_decode($result, true);
if ( !$result['state'] && !empty($result['data']) ) {
unset($this->_callArr);
self::$_callStaticArr = array();
// 数据解密处理
if ( count($result['data']) == count($result['data'],1) ) {
$result['data'] = current($this->_decryptyData(array($result['data'])));
} else {
$result['data'] = $this->_decryptyData($result['data']);
}
}
return $this->formatResult($result);
}
/** * 返回数据格式化 */
public function format($default = '') {
$this->_formatResult = true;
$this->_formatDefault= $default;
return $this;
}
/** * Yii\db\Query命令请求 */
public function queryDone() {
return $this->done('index');
}
/** * Yii\db\ActiveRecord命令请求 */
public function recordDone() {
return $this->done('record');
}
/** * Yii\db\Command命令请求 */
public function commandDone() {
return $this->done('command');
}
}