mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 23:52:49 +08:00
去掉Model类的query属性 避免序列化出问题 直接使用$this->db() 操作
This commit is contained in:
@@ -34,8 +34,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
protected $name;
|
protected $name;
|
||||||
// 数据表名称
|
// 数据表名称
|
||||||
protected $table;
|
protected $table;
|
||||||
// 查询对象
|
|
||||||
protected $query;
|
|
||||||
// 回调事件
|
// 回调事件
|
||||||
protected static $event = [];
|
protected static $event = [];
|
||||||
|
|
||||||
@@ -102,12 +100,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
$this->data = $data;
|
$this->data = $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 当前类名
|
||||||
|
$this->class = get_class($this);
|
||||||
|
|
||||||
if (empty($this->name)) {
|
if (empty($this->name)) {
|
||||||
$this->name = basename(str_replace('\\', '/', get_class($this)));
|
$this->name = basename(str_replace('\\', '/', $this->class));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 当前模型的查询对象
|
|
||||||
$this->query = $this->db();
|
|
||||||
// 执行初始化操作
|
// 执行初始化操作
|
||||||
$this->initialize();
|
$this->initialize();
|
||||||
}
|
}
|
||||||
@@ -239,7 +238,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
public function getPk($table = '')
|
public function getPk($table = '')
|
||||||
{
|
{
|
||||||
if (empty($this->pk)) {
|
if (empty($this->pk)) {
|
||||||
$this->pk = $this->query->getTableInfo($table, 'pk');
|
$this->pk = $this->db()->getTableInfo($table, 'pk');
|
||||||
}
|
}
|
||||||
return $this->pk;
|
return $this->pk;
|
||||||
}
|
}
|
||||||
@@ -335,7 +334,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = $this->query->where($where)->update($data);
|
$result = $this->db()->where($where)->update($data);
|
||||||
|
|
||||||
// 更新回调
|
// 更新回调
|
||||||
$this->trigger('after_update', $this);
|
$this->trigger('after_update', $this);
|
||||||
@@ -352,11 +351,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = $this->query->insert($this->data);
|
$result = $this->db()->insert($this->data);
|
||||||
|
|
||||||
// 获取自动增长主键
|
// 获取自动增长主键
|
||||||
if ($result && $getId) {
|
if ($result && $getId) {
|
||||||
$insertId = $this->query->getLastInsID();
|
$insertId = $this->db()->getLastInsID();
|
||||||
$pk = $this->getPk();
|
$pk = $this->getPk();
|
||||||
if (is_string($pk) && $insertId) {
|
if (is_string($pk) && $insertId) {
|
||||||
$this->data[$pk] = $insertId;
|
$this->data[$pk] = $insertId;
|
||||||
@@ -448,7 +447,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = $this->query->delete($this->data);
|
$result = $this->db()->delete($this->data);
|
||||||
|
|
||||||
$this->trigger('after_delete', $this);
|
$this->trigger('after_delete', $this);
|
||||||
return $result;
|
return $result;
|
||||||
@@ -882,7 +881,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function db()
|
public function db()
|
||||||
{
|
{
|
||||||
$model = get_called_class();
|
$model = $this->class;
|
||||||
if (!isset(self::$links[$model])) {
|
if (!isset(self::$links[$model])) {
|
||||||
// 设置当前模型 确保查询返回模型对象
|
// 设置当前模型 确保查询返回模型对象
|
||||||
$query = Db::connect($this->connection)->model($model);
|
$query = Db::connect($this->connection)->model($model);
|
||||||
@@ -905,11 +904,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
if (method_exists($this, 'scope' . $method)) {
|
if (method_exists($this, 'scope' . $method)) {
|
||||||
// 动态调用命名范围
|
// 动态调用命名范围
|
||||||
$method = 'scope' . $method;
|
$method = 'scope' . $method;
|
||||||
array_unshift($args, $this->query);
|
array_unshift($args, $this->db());
|
||||||
call_user_func_array([$this, $method], $args);
|
call_user_func_array([$this, $method], $args);
|
||||||
return $this;
|
return $this;
|
||||||
} else {
|
} else {
|
||||||
return call_user_func_array([$this->query, $method], $args);
|
return call_user_func_array([$this->db(), $method], $args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -934,7 +933,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
{
|
{
|
||||||
if (is_null($this->fieldType)) {
|
if (is_null($this->fieldType)) {
|
||||||
// 获取字段类型信息并缓存
|
// 获取字段类型信息并缓存
|
||||||
$this->fieldType = $this->query->getTableInfo('', 'type');
|
$this->fieldType = $this->db()->getTableInfo('', 'type');
|
||||||
}
|
}
|
||||||
if (is_null($value) && $this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime, $this->deleteTime])) {
|
if (is_null($value) && $this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime, $this->deleteTime])) {
|
||||||
// 自动写入的时间戳字段
|
// 自动写入的时间戳字段
|
||||||
|
|||||||
@@ -173,7 +173,8 @@ class Merge extends Model
|
|||||||
// 处理模型数据
|
// 处理模型数据
|
||||||
$data = $this->parseData($this->name, $this->data);
|
$data = $this->parseData($this->name, $this->data);
|
||||||
|
|
||||||
$this->query->startTrans('merge_save_' . $this->name);
|
$db = $this->db();
|
||||||
|
$db->startTrans('merge_save_' . $this->name);
|
||||||
try {
|
try {
|
||||||
if ($this->isUpdate) {
|
if ($this->isUpdate) {
|
||||||
// 自动写入
|
// 自动写入
|
||||||
@@ -184,15 +185,15 @@ class Merge extends Model
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 写入主表数据
|
// 写入主表数据
|
||||||
$result = $this->query->strict(false)->update($data);
|
$result = $db->strict(false)->update($data);
|
||||||
|
|
||||||
// 写入附表数据
|
// 写入附表数据
|
||||||
foreach (static::$relationModel as $key => $model) {
|
foreach (static::$relationModel as $key => $model) {
|
||||||
$name = is_int($key) ? $model : $key;
|
$name = is_int($key) ? $model : $key;
|
||||||
$table = is_int($key) ? $this->query->getTable($model) : $model;
|
$table = is_int($key) ? $db->getTable($model) : $model;
|
||||||
// 处理关联模型数据
|
// 处理关联模型数据
|
||||||
$data = $this->parseData($name, $this->data);
|
$data = $this->parseData($name, $this->data);
|
||||||
$query = clone $this->query;
|
$query = clone $db;
|
||||||
$query->table($table)->strict(false)->where($this->fk, $this->data[$this->getPk()])->update($data);
|
$query->table($table)->strict(false)->where($this->fk, $this->data[$this->getPk()])->update($data);
|
||||||
}
|
}
|
||||||
// 新增回调
|
// 新增回调
|
||||||
@@ -220,10 +221,10 @@ class Merge extends Model
|
|||||||
// 写入附表数据
|
// 写入附表数据
|
||||||
foreach (static::$relationModel as $key => $model) {
|
foreach (static::$relationModel as $key => $model) {
|
||||||
$name = is_int($key) ? $model : $key;
|
$name = is_int($key) ? $model : $key;
|
||||||
$table = is_int($key) ? $this->query->getTable($model) : $model;
|
$table = is_int($key) ? $db->getTable($model) : $model;
|
||||||
// 处理关联模型数据
|
// 处理关联模型数据
|
||||||
$data = $this->parseData($name, $this->data, true);
|
$data = $this->parseData($name, $this->data, true);
|
||||||
$query = clone $this->query;
|
$query = clone $db;
|
||||||
$query->table($table)->strict(false)->insert($data);
|
$query->table($table)->strict(false)->insert($data);
|
||||||
}
|
}
|
||||||
$result = $insertId;
|
$result = $insertId;
|
||||||
@@ -231,10 +232,10 @@ class Merge extends Model
|
|||||||
// 新增回调
|
// 新增回调
|
||||||
$this->trigger('after_insert', $this);
|
$this->trigger('after_insert', $this);
|
||||||
}
|
}
|
||||||
$this->query->commit('merge_save_' . $this->name);
|
$db->commit('merge_save_' . $this->name);
|
||||||
return $result;
|
return $result;
|
||||||
} catch (\PDOException $e) {
|
} catch (\PDOException $e) {
|
||||||
$this->query->rollback();
|
$db->rollback();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -250,25 +251,26 @@ class Merge extends Model
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->query->startTrans('merge_delete_' . $this->name);
|
$db = $this->query;
|
||||||
|
$db->startTrans('merge_delete_' . $this->name);
|
||||||
try {
|
try {
|
||||||
$result = $this->query->delete($this->data);
|
$result = $db->delete($this->data);
|
||||||
if ($result) {
|
if ($result) {
|
||||||
// 获取主键数据
|
// 获取主键数据
|
||||||
$pk = $this->data[$this->getPk()];
|
$pk = $this->data[$this->getPk()];
|
||||||
|
|
||||||
// 删除关联数据
|
// 删除关联数据
|
||||||
foreach (static::$relationModel as $key => $model) {
|
foreach (static::$relationModel as $key => $model) {
|
||||||
$table = is_int($key) ? $this->query->getTable($model) : $model;
|
$table = is_int($key) ? $db->getTable($model) : $model;
|
||||||
$query = clone $this->query;
|
$query = clone $db;
|
||||||
$query->table($table)->where($this->fk, $pk)->delete();
|
$query->table($table)->where($this->fk, $pk)->delete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->trigger('after_delete', $this);
|
$this->trigger('after_delete', $this);
|
||||||
$this->query->commit('merge_delete_' . $this->name);
|
$db->commit('merge_delete_' . $this->name);
|
||||||
return $result;
|
return $result;
|
||||||
} catch (\PDOException $e) {
|
} catch (\PDOException $e) {
|
||||||
$this->query->rollback();
|
$db->rollback();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user