mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-10 00:22:48 +08:00
改进Model类 支持设定 主键、字段信息和字段类型 不依赖自动获取
This commit is contained in:
@@ -52,16 +52,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
protected $class;
|
protected $class;
|
||||||
// 回调事件
|
// 回调事件
|
||||||
private static $event = [];
|
private static $event = [];
|
||||||
|
|
||||||
// 数据表主键 复合主键使用数组定义
|
|
||||||
protected $pk;
|
|
||||||
// 错误信息
|
// 错误信息
|
||||||
protected $error;
|
protected $error;
|
||||||
// 字段验证规则
|
// 字段验证规则
|
||||||
protected $validate;
|
protected $validate;
|
||||||
|
// 数据表主键 复合主键使用数组定义
|
||||||
|
protected $pk;
|
||||||
// 字段属性
|
// 字段属性
|
||||||
protected $field = [];
|
protected $field = [];
|
||||||
|
// 字段类型
|
||||||
|
protected $fieldType = [];
|
||||||
// 显示属性
|
// 显示属性
|
||||||
protected $visible = [];
|
protected $visible = [];
|
||||||
// 隐藏属性
|
// 隐藏属性
|
||||||
@@ -155,6 +155,19 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
} else {
|
} else {
|
||||||
$query->name($this->name);
|
$query->name($this->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!empty($this->field)) {
|
||||||
|
$query->allowField($this->field);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($this->fieldType)) {
|
||||||
|
$query->setFieldType($this->fieldType);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($this->pk)) {
|
||||||
|
$query->pk($this->pk);
|
||||||
|
}
|
||||||
|
|
||||||
// 全局作用域
|
// 全局作用域
|
||||||
if (method_exists($this, 'base')) {
|
if (method_exists($this, 'base')) {
|
||||||
call_user_func_array([$this, 'base'], [ & $query]);
|
call_user_func_array([$this, 'base'], [ & $query]);
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ abstract class Builder
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 获取绑定信息
|
// 获取绑定信息
|
||||||
$bind = $this->query->getTableInfo($options['table'], 'bind');
|
$bind = $this->query->getFieldsBind($options);
|
||||||
if ('*' == $options['field']) {
|
if ('*' == $options['field']) {
|
||||||
$fields = array_keys($bind);
|
$fields = array_keys($bind);
|
||||||
} else {
|
} else {
|
||||||
@@ -228,8 +228,8 @@ abstract class Builder
|
|||||||
|
|
||||||
$whereStr = '';
|
$whereStr = '';
|
||||||
// 获取字段信息
|
// 获取字段信息
|
||||||
$fields = $this->query->getTableInfo($options['table'], 'fields');
|
$fields = $this->query->getTableFields($options);
|
||||||
$binds = $this->query->getTableInfo($options['table'], 'bind');
|
$binds = $this->query->getFieldsBind($options);
|
||||||
foreach ($where as $key => $val) {
|
foreach ($where as $key => $val) {
|
||||||
$str = [];
|
$str = [];
|
||||||
foreach ($val as $field => $value) {
|
foreach ($val as $field => $value) {
|
||||||
@@ -369,10 +369,8 @@ abstract class Builder
|
|||||||
protected function parseDateTime($value, $key, $options = [])
|
protected function parseDateTime($value, $key, $options = [])
|
||||||
{
|
{
|
||||||
// 获取时间字段类型
|
// 获取时间字段类型
|
||||||
$type = $this->query->getTableInfo($options['table'], 'type');
|
$type = $this->query->getFieldsType($options);
|
||||||
if (isset($options['field_type'][$key])) {
|
if (isset($type[$key])) {
|
||||||
$info = $options['field_type'][$key];
|
|
||||||
} elseif (isset($type[$key])) {
|
|
||||||
$info = $type[$key];
|
$info = $type[$key];
|
||||||
}
|
}
|
||||||
if (isset($info)) {
|
if (isset($info)) {
|
||||||
@@ -608,7 +606,7 @@ abstract class Builder
|
|||||||
{
|
{
|
||||||
// 获取合法的字段
|
// 获取合法的字段
|
||||||
if ('*' == $options['field']) {
|
if ('*' == $options['field']) {
|
||||||
$fields = $this->query->getTableInfo($options['table'], 'fields');
|
$fields = $this->query->getFieldsType($options);
|
||||||
} else {
|
} else {
|
||||||
$fields = $options['field'];
|
$fields = $options['field'];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -720,11 +720,11 @@ class Query
|
|||||||
}
|
}
|
||||||
if (true === $field) {
|
if (true === $field) {
|
||||||
// 获取全部字段
|
// 获取全部字段
|
||||||
$fields = $this->getTableInfo($tableName, 'fields');
|
$fields = isset($this->options['allow_field']) ? $this->options['allow_field'] : $this->getTableInfo($tableName, 'fields');
|
||||||
$field = $fields ?: ['*'];
|
$field = $fields ?: ['*'];
|
||||||
} elseif ($except) {
|
} elseif ($except) {
|
||||||
// 字段排除
|
// 字段排除
|
||||||
$fields = $this->getTableInfo($tableName, 'fields');
|
$fields = isset($this->options['allow_field']) ? $this->options['allow_field'] : $this->getTableInfo($tableName, 'fields');
|
||||||
$field = $fields ? array_diff($fields, $field) : $field;
|
$field = $fields ? array_diff($fields, $field) : $field;
|
||||||
}
|
}
|
||||||
if ($tableName) {
|
if ($tableName) {
|
||||||
@@ -1235,6 +1235,47 @@ class Query
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置数据表字段
|
||||||
|
* @access public
|
||||||
|
* @param string|array $field 字段信息
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function allowField($field)
|
||||||
|
{
|
||||||
|
if (true === $field) {
|
||||||
|
$field = $this->getTableInfo('', 'fields');
|
||||||
|
} elseif (is_string($field)) {
|
||||||
|
$field = explode(',', $field);
|
||||||
|
}
|
||||||
|
$this->options['allow_field'] = $field;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置字段类型
|
||||||
|
* @access public
|
||||||
|
* @param array $fieldType 字段类型信息
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setFieldType($fieldType = [])
|
||||||
|
{
|
||||||
|
$this->options['field_type'] = $fieldType;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 指定数据表主键
|
||||||
|
* @access public
|
||||||
|
* @param string $pk 主键
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function pk($pk)
|
||||||
|
{
|
||||||
|
$this->options['pk'] = $pk;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询日期或者时间
|
* 查询日期或者时间
|
||||||
* @access public
|
* @access public
|
||||||
@@ -1284,18 +1325,6 @@ class Query
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置字段类型
|
|
||||||
* @access public
|
|
||||||
* @param array $fieldType 字段类型信息
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function setFieldType($fieldType = [])
|
|
||||||
{
|
|
||||||
$this->options['field_type'] = $fieldType;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取数据表信息
|
* 获取数据表信息
|
||||||
* @access public
|
* @access public
|
||||||
@@ -1327,13 +1356,7 @@ class Query
|
|||||||
foreach ($info as $key => $val) {
|
foreach ($info as $key => $val) {
|
||||||
// 记录字段类型
|
// 记录字段类型
|
||||||
$type[$key] = $val['type'];
|
$type[$key] = $val['type'];
|
||||||
if (preg_match('/(int|double|float|decimal|real|numeric|serial)/is', $val['type'])) {
|
$bind[$key] = $this->getFieldBindType($val['type']);
|
||||||
$bind[$key] = PDO::PARAM_INT;
|
|
||||||
} elseif (preg_match('/bool/is', $val['type'])) {
|
|
||||||
$bind[$key] = PDO::PARAM_BOOL;
|
|
||||||
} else {
|
|
||||||
$bind[$key] = PDO::PARAM_STR;
|
|
||||||
}
|
|
||||||
if (!empty($val['primary'])) {
|
if (!empty($val['primary'])) {
|
||||||
$pk[] = $key;
|
$pk[] = $key;
|
||||||
}
|
}
|
||||||
@@ -1352,12 +1375,60 @@ class Query
|
|||||||
/**
|
/**
|
||||||
* 获取当前数据表的主键
|
* 获取当前数据表的主键
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $table 数据表名
|
* @param string|array $options 数据表名或者查询参数
|
||||||
* @return string|array
|
* @return string|array
|
||||||
*/
|
*/
|
||||||
public function getPk($table = '')
|
public function getPk($options = '')
|
||||||
{
|
{
|
||||||
return $this->getTableInfo($table, 'pk');
|
if (!empty($options['pk'])) {
|
||||||
|
$pk = $options['pk'];
|
||||||
|
} elseif (isset($this->options['pk'])) {
|
||||||
|
$pk = $this->options['pk'];
|
||||||
|
} else {
|
||||||
|
$pk = $this->getTableInfo(is_array($options) ? $options['table'] : $options, 'pk');
|
||||||
|
}
|
||||||
|
return $pk;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取当前数据表字段信息
|
||||||
|
public function getTableFields($options)
|
||||||
|
{
|
||||||
|
return !empty($options['allow_field']) ? $options['allow_field'] : $this->getTableInfo($options['table'], 'fields');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取当前数据表字段类型
|
||||||
|
public function getFieldsType($options)
|
||||||
|
{
|
||||||
|
return !empty($options['field_type']) ? $options['field_type'] : $this->getTableInfo($options['table'], 'type');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取当前数据表绑定信息
|
||||||
|
public function getFieldsBind($options)
|
||||||
|
{
|
||||||
|
$types = $this->getFieldsType($options);
|
||||||
|
$bind = [];
|
||||||
|
foreach ($types as $key => $type) {
|
||||||
|
$bind[$key] = $this->getFieldBindType($type);
|
||||||
|
}
|
||||||
|
return $bind;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取字段绑定类型
|
||||||
|
* @access public
|
||||||
|
* @param string $type 字段类型
|
||||||
|
* @return integer
|
||||||
|
*/
|
||||||
|
protected function getFieldBindType($type)
|
||||||
|
{
|
||||||
|
if (preg_match('/(int|double|float|decimal|real|numeric|serial)/is', $type)) {
|
||||||
|
$bind = PDO::PARAM_INT;
|
||||||
|
} elseif (preg_match('/bool/is', $type)) {
|
||||||
|
$bind = PDO::PARAM_BOOL;
|
||||||
|
} else {
|
||||||
|
$bind = PDO::PARAM_STR;
|
||||||
|
}
|
||||||
|
return $bind;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1546,7 +1617,7 @@ class Query
|
|||||||
*/
|
*/
|
||||||
protected function parsePkWhere($data, &$options)
|
protected function parsePkWhere($data, &$options)
|
||||||
{
|
{
|
||||||
$pk = $this->getPk($options['table']);
|
$pk = $this->getPk($options);
|
||||||
// 获取当前数据表
|
// 获取当前数据表
|
||||||
if (!empty($options['alias'])) {
|
if (!empty($options['alias'])) {
|
||||||
$alias = $options['alias'];
|
$alias = $options['alias'];
|
||||||
@@ -1684,7 +1755,7 @@ class Query
|
|||||||
{
|
{
|
||||||
$options = $this->parseExpress();
|
$options = $this->parseExpress();
|
||||||
if (empty($options['where'])) {
|
if (empty($options['where'])) {
|
||||||
$pk = $this->getPk($options['table']);
|
$pk = $this->getPk($options);
|
||||||
// 如果存在主键数据 则自动作为更新条件
|
// 如果存在主键数据 则自动作为更新条件
|
||||||
if (is_string($pk) && isset($data[$pk])) {
|
if (is_string($pk) && isset($data[$pk])) {
|
||||||
$where[$pk] = $data[$pk];
|
$where[$pk] = $data[$pk];
|
||||||
@@ -1951,8 +2022,8 @@ class Query
|
|||||||
*/
|
*/
|
||||||
public function chunk($count, $callback, $column = null)
|
public function chunk($count, $callback, $column = null)
|
||||||
{
|
{
|
||||||
$column = $column ?: $this->getPk();
|
|
||||||
$options = $this->getOptions();
|
$options = $this->getOptions();
|
||||||
|
$column = $column ?: ($options['pk'] ?: $this->getPk());
|
||||||
$bind = $this->bind;
|
$bind = $this->bind;
|
||||||
$resultSet = $this->limit($count)->order($column, 'asc')->select();
|
$resultSet = $this->limit($count)->order($column, 'asc')->select();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user