mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
Think\Model类调整,包括:
_scope属性改为scope _map属性改为map _facade方法改为_write_data 增加_read_data方法 增加_read_datalist 方法 增加map连贯操作 用于动态设置字段映射
This commit is contained in:
@@ -41,7 +41,9 @@ class Model {
|
|||||||
// 查询表达式参数
|
// 查询表达式参数
|
||||||
protected $options = [];
|
protected $options = [];
|
||||||
// 命名范围定义
|
// 命名范围定义
|
||||||
protected $_scope = [];
|
protected $scope = [];
|
||||||
|
// 字段映射定义
|
||||||
|
protected $map = [];
|
||||||
// 是否自动检测数据表字段信息
|
// 是否自动检测数据表字段信息
|
||||||
protected $autoCheckFields = false;
|
protected $autoCheckFields = false;
|
||||||
|
|
||||||
@@ -138,12 +140,21 @@ class Model {
|
|||||||
protected function _initialize() {}
|
protected function _initialize() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对保存到数据库的数据进行处理
|
* 对写入到数据库的数据进行处理
|
||||||
* @access protected
|
* @access protected
|
||||||
* @param mixed $data 要操作的数据
|
* @param mixed $data 要操作的数据
|
||||||
* @return boolean
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function _facade($data) {
|
protected function _write_data($data) {
|
||||||
|
// 检查字段映射
|
||||||
|
if(!empty($this->map)) {
|
||||||
|
foreach ($this->map as $key=>$val){
|
||||||
|
if(isset($data[$key])) {
|
||||||
|
$data[$val] = $data[$key];
|
||||||
|
unset($data[$key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
// 检查非数据字段
|
// 检查非数据字段
|
||||||
if(!empty($this->fields)) {
|
if(!empty($this->fields)) {
|
||||||
foreach ($data as $key=>$val){
|
foreach ($data as $key=>$val){
|
||||||
@@ -160,10 +171,10 @@ class Model {
|
|||||||
$data = array_map($this->options['filter'],$data);
|
$data = array_map($this->options['filter'],$data);
|
||||||
unset($this->options['filter']);
|
unset($this->options['filter']);
|
||||||
}
|
}
|
||||||
|
// 回调方法
|
||||||
$this->_before_write($data);
|
$this->_before_write($data);
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 写入数据前的回调方法 包括新增和更新
|
// 写入数据前的回调方法 包括新增和更新
|
||||||
protected function _before_write(&$data) {}
|
protected function _before_write(&$data) {}
|
||||||
|
|
||||||
@@ -189,7 +200,7 @@ class Model {
|
|||||||
// 分析表达式
|
// 分析表达式
|
||||||
$options = $this->_parseOptions();
|
$options = $this->_parseOptions();
|
||||||
// 数据处理
|
// 数据处理
|
||||||
$data = $this->_facade($data);
|
$data = $this->_write_data($data);
|
||||||
if(false === $this->_before_insert($data,$options)) {
|
if(false === $this->_before_insert($data,$options)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -231,7 +242,7 @@ class Model {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 数据处理
|
// 数据处理
|
||||||
$data = $this->_facade($data);
|
$data = $this->_write_data($data);
|
||||||
// 分析表达式
|
// 分析表达式
|
||||||
$options = $this->_parseOptions();
|
$options = $this->_parseOptions();
|
||||||
if(false === $this->_before_update($data,$options)) {
|
if(false === $this->_before_update($data,$options)) {
|
||||||
@@ -335,11 +346,24 @@ class Model {
|
|||||||
if(empty($resultSet)) { // 查询结果为空
|
if(empty($resultSet)) { // 查询结果为空
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$this->_after_select($resultSet,$options);
|
// 数据列表读取后的处理
|
||||||
|
$resultSet = $this->_read_datalist($resultSet);
|
||||||
|
return $resultSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据列表读取后的处理
|
||||||
|
* @access protected
|
||||||
|
* @param array $data 当前数据
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function _read_datalist($resultSet) {
|
||||||
|
$resultSet = array_map([$this,'_read_data'],$resultSet);
|
||||||
|
$this->_after_select($resultSet);
|
||||||
return $resultSet;
|
return $resultSet;
|
||||||
}
|
}
|
||||||
// 查询成功后的回调方法
|
// 查询成功后的回调方法
|
||||||
protected function _after_select(&$resultSet,$options) {}
|
protected function _after_select(&$resultSet) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成查询SQL 可用于子查询
|
* 生成查询SQL 可用于子查询
|
||||||
@@ -354,7 +378,7 @@ class Model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 分析表达式
|
* 分析表达式(可用于查询或者写入操作)
|
||||||
* @access protected
|
* @access protected
|
||||||
* @param array $options 表达式参数
|
* @param array $options 表达式参数
|
||||||
* @return array
|
* @return array
|
||||||
@@ -392,8 +416,6 @@ class Model {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 表达式过滤
|
// 表达式过滤
|
||||||
$this->_options_filter($options);
|
$this->_options_filter($options);
|
||||||
return $options;
|
return $options;
|
||||||
@@ -444,12 +466,34 @@ class Model {
|
|||||||
if(empty($resultSet)) {// 查询结果为空
|
if(empty($resultSet)) {// 查询结果为空
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$this->data = $resultSet[0];
|
// 数据处理
|
||||||
$this->_after_find($this->data,$options);
|
$data = $this->_read_data($resultSet[0]);
|
||||||
|
// 数据对象赋值
|
||||||
|
$this->data = $data;
|
||||||
return $this->data;
|
return $this->data;
|
||||||
}
|
}
|
||||||
// 查询成功的回调方法
|
|
||||||
protected function _after_find(&$result,$options) {}
|
/**
|
||||||
|
* 数据读取后的处理
|
||||||
|
* @access protected
|
||||||
|
* @param array $data 当前数据
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function _read_data($data) {
|
||||||
|
// 检查字段映射
|
||||||
|
if(!empty($this->map)) {
|
||||||
|
foreach ($this->map as $key=>$val){
|
||||||
|
if(isset($data[$val])) {
|
||||||
|
$data[$key] = $data[$val];
|
||||||
|
unset($data[$val]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->_after_find($data);
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
// 数据读取成功后的回调方法
|
||||||
|
protected function _after_find(&$result) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建数据对象 但不保存到数据库
|
* 创建数据对象 但不保存到数据库
|
||||||
@@ -759,9 +803,9 @@ class Model {
|
|||||||
*/
|
*/
|
||||||
public function scope($scope='',$args=NULL){
|
public function scope($scope='',$args=NULL){
|
||||||
if('' === $scope) {
|
if('' === $scope) {
|
||||||
if(isset($this->_scope['default'])) {
|
if(isset($this->scope['default'])) {
|
||||||
// 默认的命名范围
|
// 默认的命名范围
|
||||||
$options = $this->_scope['default'];
|
$options = $this->scope['default'];
|
||||||
}else{
|
}else{
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@@ -769,8 +813,8 @@ class Model {
|
|||||||
$scopes = explode(',',$scope);
|
$scopes = explode(',',$scope);
|
||||||
$options = [];
|
$options = [];
|
||||||
foreach ($scopes as $name){
|
foreach ($scopes as $name){
|
||||||
if(!isset($this->_scope[$name])) continue;
|
if(!isset($this->scope[$name])) continue;
|
||||||
$options = array_merge($options,$this->_scope[$name]);
|
$options = array_merge($options,$this->scope[$name]);
|
||||||
}
|
}
|
||||||
if(!empty($args) && is_array($args)) {
|
if(!empty($args) && is_array($args)) {
|
||||||
$options = array_merge($options,$args);
|
$options = array_merge($options,$args);
|
||||||
@@ -950,4 +994,14 @@ class Model {
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置字段映射
|
||||||
|
* @access public
|
||||||
|
* @param array $map 映射
|
||||||
|
* @return Model
|
||||||
|
*/
|
||||||
|
public function map($map){
|
||||||
|
$this->map = array_merge($this->map,$map);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -37,7 +37,7 @@ trait Extend {
|
|||||||
$name = parse_name(substr($method,10));
|
$name = parse_name(substr($method,10));
|
||||||
$where[$name] =$args[0];
|
$where[$name] =$args[0];
|
||||||
return $this->where($where)->getField($args[1]);
|
return $this->where($where)->getField($args[1]);
|
||||||
}elseif(isset($this->_scope[$method])){// 命名范围的单独调用支持
|
}elseif(isset($this->scope[$method])){// 命名范围的单独调用支持
|
||||||
return $this->scope($method,$args[0]);
|
return $this->scope($method,$args[0]);
|
||||||
}else{
|
}else{
|
||||||
E(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_'));
|
E(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_'));
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ trait Relation {
|
|||||||
* @param mixed $data 要操作的数据
|
* @param mixed $data 要操作的数据
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
protected function _facade($data) {
|
protected function _write_data($data) {
|
||||||
$this->_before_write($data);
|
$this->_before_write($data);
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user