mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-04 14:22:48 +08:00
Model类属性tableName改为table Connection类getTableName 更改为 getTable
This commit is contained in:
@@ -20,14 +20,12 @@ use think\model\Relation;
|
|||||||
abstract class Model implements \JsonSerializable, \ArrayAccess
|
abstract class Model implements \JsonSerializable, \ArrayAccess
|
||||||
{
|
{
|
||||||
|
|
||||||
// 当前实例
|
|
||||||
private static $instance;
|
|
||||||
// 数据库对象池
|
// 数据库对象池
|
||||||
private static $links = [];
|
private static $links = [];
|
||||||
// 数据库配置
|
// 数据库配置
|
||||||
protected static $connection;
|
protected static $connection;
|
||||||
// 数据表名称
|
// 数据表名称
|
||||||
protected static $tableName;
|
protected static $table;
|
||||||
// 回调事件
|
// 回调事件
|
||||||
protected static $event = [];
|
protected static $event = [];
|
||||||
|
|
||||||
@@ -116,33 +114,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
protected static function init()
|
protected static function init()
|
||||||
{}
|
{}
|
||||||
|
|
||||||
// JsonSerializable
|
|
||||||
public function jsonSerialize()
|
|
||||||
{
|
|
||||||
return $this->toArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
// ArrayAccess
|
|
||||||
public function offsetSet($name, $value)
|
|
||||||
{
|
|
||||||
$this->__set($name, $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function offsetExists($name)
|
|
||||||
{
|
|
||||||
return $this->__isset($name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function offsetUnset($name)
|
|
||||||
{
|
|
||||||
$this->__unset($name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function offsetGet($name)
|
|
||||||
{
|
|
||||||
return $this->__get($name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置数据对象值
|
* 设置数据对象值
|
||||||
* @access public
|
* @access public
|
||||||
@@ -195,139 +166,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
return !empty($item) ? $item : [];
|
return !empty($item) ? $item : [];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 修改器 设置数据对象的值
|
|
||||||
* @access public
|
|
||||||
* @param string $name 名称
|
|
||||||
* @param mixed $value 值
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function __set($name, $value)
|
|
||||||
{
|
|
||||||
if (is_null($value) && in_array($name, $this->autoTimeField)) {
|
|
||||||
// 自动写入的时间戳字段
|
|
||||||
$value = NOW_TIME;
|
|
||||||
} else {
|
|
||||||
// 检测修改器
|
|
||||||
$method = 'set' . Loader::parseName($name, 1) . 'Attr';
|
|
||||||
if (method_exists($this, $method)) {
|
|
||||||
$value = $this->$method($value, $this->data);
|
|
||||||
} elseif (isset($this->type[$name])) {
|
|
||||||
// 类型转换
|
|
||||||
$type = $this->type[$name];
|
|
||||||
switch ($type) {
|
|
||||||
case 'integer':
|
|
||||||
$value = (int) $value;
|
|
||||||
break;
|
|
||||||
case 'float':
|
|
||||||
$value = (float) $value;
|
|
||||||
break;
|
|
||||||
case 'boolean':
|
|
||||||
$value = (bool) $value;
|
|
||||||
break;
|
|
||||||
case 'datetime':
|
|
||||||
$value = strtotime($value);
|
|
||||||
break;
|
|
||||||
case 'object':
|
|
||||||
if (is_object($value)) {
|
|
||||||
$value = json_encode($value, JSON_FORCE_OBJECT);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'array':
|
|
||||||
if (is_array($value)) {
|
|
||||||
$value = json_encode($value, JSON_UNESCAPED_UNICODE);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 标记字段更改
|
|
||||||
if (!isset($this->data[$name]) || ($this->data[$name] != $value && !in_array($name, $this->change))) {
|
|
||||||
$this->change[] = $name;
|
|
||||||
}
|
|
||||||
// 设置数据对象属性
|
|
||||||
$this->data[$name] = $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取器 获取数据对象的值
|
|
||||||
* @access public
|
|
||||||
* @param string $name 名称
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function __get($name)
|
|
||||||
{
|
|
||||||
$value = isset($this->data[$name]) ? $this->data[$name] : null;
|
|
||||||
|
|
||||||
// 检测属性获取器
|
|
||||||
$method = 'get' . Loader::parseName($name, 1) . 'Attr';
|
|
||||||
if (method_exists($this, $method)) {
|
|
||||||
return $this->$method($value, $this->data);
|
|
||||||
} elseif (!is_null($value) && isset($this->type[$name])) {
|
|
||||||
// 类型转换
|
|
||||||
$type = $this->type[$name];
|
|
||||||
switch ($type) {
|
|
||||||
case 'integer':
|
|
||||||
$value = (int) $value;
|
|
||||||
break;
|
|
||||||
case 'float':
|
|
||||||
$value = (float) $value;
|
|
||||||
break;
|
|
||||||
case 'boolean':
|
|
||||||
$value = (bool) $value;
|
|
||||||
break;
|
|
||||||
case 'datetime':
|
|
||||||
$value = date($this->dateFormat, $value);
|
|
||||||
break;
|
|
||||||
case 'array':
|
|
||||||
$value = json_decode($value, true);
|
|
||||||
break;
|
|
||||||
case 'object':
|
|
||||||
$value = json_decode($value);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} elseif (is_null($value) && method_exists($this, $name)) {
|
|
||||||
// 获取关联数据
|
|
||||||
$value = $this->relation->getRelation($name);
|
|
||||||
// 保存关联对象值
|
|
||||||
$this->data[$name] = $value;
|
|
||||||
}
|
|
||||||
return $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检测数据对象的值
|
|
||||||
* @access public
|
|
||||||
* @param string $name 名称
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
public function __isset($name)
|
|
||||||
{
|
|
||||||
return isset($this->data[$name]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 销毁数据对象的值
|
|
||||||
* @access public
|
|
||||||
* @param string $name 名称
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function __unset($name)
|
|
||||||
{
|
|
||||||
unset($this->data[$name]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前模型对象的主键
|
* 获取当前模型对象的主键
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $tableName 数据表名
|
* @param string $table 数据表名
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function getPk($tableName = '')
|
public function getPk($table = '')
|
||||||
{
|
{
|
||||||
if (!$this->pk) {
|
if (!$this->pk) {
|
||||||
$this->pk = self::db()->getTableInfo($tableName, 'pk');
|
$this->pk = self::db()->getTableInfo($table, 'pk');
|
||||||
}
|
}
|
||||||
return $this->pk;
|
return $this->pk;
|
||||||
}
|
}
|
||||||
@@ -877,7 +725,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
// 记录当前关联信息
|
// 记录当前关联信息
|
||||||
$model = $this->parseModel($model);
|
$model = $this->parseModel($model);
|
||||||
$name = Loader::parseName(basename(str_replace('\\', '/', $model)));
|
$name = Loader::parseName(basename(str_replace('\\', '/', $model)));
|
||||||
$table = $table ?: Db::name(Loader::parseName($this->name) . '_' . $name)->getTableName();
|
$table = $table ?: Db::name(Loader::parseName($this->name) . '_' . $name)->getTable();
|
||||||
$localKey = $localKey ?: $name . '_id';
|
$localKey = $localKey ?: $name . '_id';
|
||||||
$foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id';
|
$foreignKey = $foreignKey ?: Loader::parseName($this->name) . '_id';
|
||||||
|
|
||||||
@@ -896,7 +744,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
if (!isset(self::$links[$model])) {
|
if (!isset(self::$links[$model])) {
|
||||||
self::$links[$model] = Db::connect(static::$connection);
|
self::$links[$model] = Db::connect(static::$connection);
|
||||||
}
|
}
|
||||||
self::$links[$model]->setTable(static::$tableName);
|
self::$links[$model]->setTable(static::$table);
|
||||||
$name = basename(str_replace('\\', '/', $model));
|
$name = basename(str_replace('\\', '/', $model));
|
||||||
self::$links[$model]->name($name);
|
self::$links[$model]->name($name);
|
||||||
// 设置当前模型 确保查询返回模型对象
|
// 设置当前模型 确保查询返回模型对象
|
||||||
@@ -924,11 +772,161 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
return call_user_func_array([self::db(), $method], $params);
|
return call_user_func_array([self::db(), $method], $params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改器 设置数据对象的值
|
||||||
|
* @access public
|
||||||
|
* @param string $name 名称
|
||||||
|
* @param mixed $value 值
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __set($name, $value)
|
||||||
|
{
|
||||||
|
if (is_null($value) && in_array($name, $this->autoTimeField)) {
|
||||||
|
// 自动写入的时间戳字段
|
||||||
|
$value = NOW_TIME;
|
||||||
|
} else {
|
||||||
|
// 检测修改器
|
||||||
|
$method = 'set' . Loader::parseName($name, 1) . 'Attr';
|
||||||
|
if (method_exists($this, $method)) {
|
||||||
|
$value = $this->$method($value, $this->data);
|
||||||
|
} elseif (isset($this->type[$name])) {
|
||||||
|
// 类型转换
|
||||||
|
$type = $this->type[$name];
|
||||||
|
switch ($type) {
|
||||||
|
case 'integer':
|
||||||
|
$value = (int) $value;
|
||||||
|
break;
|
||||||
|
case 'float':
|
||||||
|
$value = (float) $value;
|
||||||
|
break;
|
||||||
|
case 'boolean':
|
||||||
|
$value = (bool) $value;
|
||||||
|
break;
|
||||||
|
case 'datetime':
|
||||||
|
$value = strtotime($value);
|
||||||
|
break;
|
||||||
|
case 'object':
|
||||||
|
if (is_object($value)) {
|
||||||
|
$value = json_encode($value, JSON_FORCE_OBJECT);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'array':
|
||||||
|
if (is_array($value)) {
|
||||||
|
$value = json_encode($value, JSON_UNESCAPED_UNICODE);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 标记字段更改
|
||||||
|
if (!isset($this->data[$name]) || ($this->data[$name] != $value && !in_array($name, $this->change))) {
|
||||||
|
$this->change[] = $name;
|
||||||
|
}
|
||||||
|
// 设置数据对象属性
|
||||||
|
$this->data[$name] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取器 获取数据对象的值
|
||||||
|
* @access public
|
||||||
|
* @param string $name 名称
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function __get($name)
|
||||||
|
{
|
||||||
|
$value = isset($this->data[$name]) ? $this->data[$name] : null;
|
||||||
|
|
||||||
|
// 检测属性获取器
|
||||||
|
$method = 'get' . Loader::parseName($name, 1) . 'Attr';
|
||||||
|
if (method_exists($this, $method)) {
|
||||||
|
return $this->$method($value, $this->data);
|
||||||
|
} elseif (!is_null($value) && isset($this->type[$name])) {
|
||||||
|
// 类型转换
|
||||||
|
$type = $this->type[$name];
|
||||||
|
switch ($type) {
|
||||||
|
case 'integer':
|
||||||
|
$value = (int) $value;
|
||||||
|
break;
|
||||||
|
case 'float':
|
||||||
|
$value = (float) $value;
|
||||||
|
break;
|
||||||
|
case 'boolean':
|
||||||
|
$value = (bool) $value;
|
||||||
|
break;
|
||||||
|
case 'datetime':
|
||||||
|
$value = date($this->dateFormat, $value);
|
||||||
|
break;
|
||||||
|
case 'array':
|
||||||
|
$value = json_decode($value, true);
|
||||||
|
break;
|
||||||
|
case 'object':
|
||||||
|
$value = json_decode($value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} elseif (is_null($value) && method_exists($this, $name)) {
|
||||||
|
// 获取关联数据
|
||||||
|
$value = $this->relation->getRelation($name);
|
||||||
|
// 保存关联对象值
|
||||||
|
$this->data[$name] = $value;
|
||||||
|
}
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检测数据对象的值
|
||||||
|
* @access public
|
||||||
|
* @param string $name 名称
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function __isset($name)
|
||||||
|
{
|
||||||
|
return isset($this->data[$name]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 销毁数据对象的值
|
||||||
|
* @access public
|
||||||
|
* @param string $name 名称
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __unset($name)
|
||||||
|
{
|
||||||
|
unset($this->data[$name]);
|
||||||
|
}
|
||||||
|
|
||||||
public function __toString()
|
public function __toString()
|
||||||
{
|
{
|
||||||
return json_encode($this->toArray());
|
return json_encode($this->toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// JsonSerializable
|
||||||
|
public function jsonSerialize()
|
||||||
|
{
|
||||||
|
return $this->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ArrayAccess
|
||||||
|
public function offsetSet($name, $value)
|
||||||
|
{
|
||||||
|
$this->__set($name, $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetExists($name)
|
||||||
|
{
|
||||||
|
return $this->__isset($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetUnset($name)
|
||||||
|
{
|
||||||
|
$this->__unset($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetGet($name)
|
||||||
|
{
|
||||||
|
return $this->__get($name);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 解序列化后处理
|
* 解序列化后处理
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ abstract class Connection
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 指定当前数据表
|
* 指定当前的数据表
|
||||||
* @param string $table 数据表名称
|
* @param string $table 数据表名称
|
||||||
* @access public
|
* @access public
|
||||||
* @return void
|
* @return void
|
||||||
@@ -146,11 +146,11 @@ abstract class Connection
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 得到完整的数据表名
|
* 得到当前的数据表
|
||||||
* @access public
|
* @access public
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getTableName()
|
public function getTable()
|
||||||
{
|
{
|
||||||
if (!$this->table) {
|
if (!$this->table) {
|
||||||
$tableName = $this->config['prefix'];
|
$tableName = $this->config['prefix'];
|
||||||
@@ -570,7 +570,7 @@ abstract class Connection
|
|||||||
{
|
{
|
||||||
static $_info = [];
|
static $_info = [];
|
||||||
if (!$tableName) {
|
if (!$tableName) {
|
||||||
$tableName = $this->getTableName();
|
$tableName = $this->getTable();
|
||||||
}
|
}
|
||||||
if (is_array($tableName)) {
|
if (is_array($tableName)) {
|
||||||
$tableName = key($tableName) ?: current($tableName);
|
$tableName = key($tableName) ?: current($tableName);
|
||||||
|
|||||||
@@ -850,11 +850,11 @@ class Query
|
|||||||
if (in_array($info['type'], [Relation::HAS_ONE, Relation::BELONGS_TO])) {
|
if (in_array($info['type'], [Relation::HAS_ONE, Relation::BELONGS_TO])) {
|
||||||
if (0 == $i) {
|
if (0 == $i) {
|
||||||
$joinName = Loader::parseName(basename(str_replace('\\', '/', $this->options['model'])));
|
$joinName = Loader::parseName(basename(str_replace('\\', '/', $this->options['model'])));
|
||||||
$joinTable = $this->connection->getTableName();
|
$joinTable = $this->connection->getTable();
|
||||||
$this->table($joinTable)->alias($joinName)->field(true, false, $joinTable, $joinName);
|
$this->table($joinTable)->alias($joinName)->field(true, false, $joinTable, $joinName);
|
||||||
}
|
}
|
||||||
// 预载入封装
|
// 预载入封装
|
||||||
$table = $info['model']::getTableName();
|
$table = $info['model']::getTable();
|
||||||
$name = Loader::parseName(basename(str_replace('\\', '/', $info['model'])));
|
$name = Loader::parseName(basename(str_replace('\\', '/', $info['model'])));
|
||||||
$this->via($name);
|
$this->via($name);
|
||||||
$this->join($table . ' ' . $name, $joinName . '.' . $info['localKey'] . '=' . $name . '.' . $info['foreignKey'])->field(true, false, $table, $name, $name . '__');
|
$this->join($table . ' ' . $name, $joinName . '.' . $info['localKey'] . '=' . $name . '.' . $info['foreignKey'])->field(true, false, $table, $name, $name . '__');
|
||||||
@@ -1269,7 +1269,7 @@ class Query
|
|||||||
|
|
||||||
// 获取数据表
|
// 获取数据表
|
||||||
if (empty($options['table'])) {
|
if (empty($options['table'])) {
|
||||||
$options['table'] = $this->connection->getTableName();
|
$options['table'] = $this->connection->getTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($options['where'])) {
|
if (!isset($options['where'])) {
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ class Pivot extends Model
|
|||||||
* 架构函数
|
* 架构函数
|
||||||
* @access public
|
* @access public
|
||||||
* @param array|object $data 数据
|
* @param array|object $data 数据
|
||||||
|
* @param string $table 中间数据表名
|
||||||
*/
|
*/
|
||||||
public function __construct($data = [], $table = '')
|
public function __construct($data = [], $table = '')
|
||||||
{
|
{
|
||||||
@@ -29,7 +30,7 @@ class Pivot extends Model
|
|||||||
$this->data = $data;
|
$this->data = $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
self::$tableName = $table;
|
self::$table = $table;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -429,7 +429,7 @@ class Relation
|
|||||||
protected function belongsToManyQuery($model, $table, $localKey, $foreignKey, $condition = [])
|
protected function belongsToManyQuery($model, $table, $localKey, $foreignKey, $condition = [])
|
||||||
{
|
{
|
||||||
// 关联查询封装
|
// 关联查询封装
|
||||||
$tableName = $model::getTableName();
|
$tableName = $model::getTable();
|
||||||
$relationFk = (new $model)->getPk();
|
$relationFk = (new $model)->getPk();
|
||||||
return $model::field($tableName . '.*')
|
return $model::field($tableName . '.*')
|
||||||
->field(true, false, $table, 'pivot', 'pivot__')
|
->field(true, false, $table, 'pivot', 'pivot__')
|
||||||
|
|||||||
Reference in New Issue
Block a user