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