去掉Model类的query属性 避免序列化出问题 直接使用$this->db() 操作

This commit is contained in:
thinkphp
2016-05-19 10:48:50 +08:00
parent 7ff02c61cd
commit 0fd61503a8
2 changed files with 29 additions and 28 deletions

View File

@@ -34,8 +34,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
protected $name;
// 数据表名称
protected $table;
// 查询对象
protected $query;
// 回调事件
protected static $event = [];
@@ -102,12 +100,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$this->data = $data;
}
// 当前类名
$this->class = get_class($this);
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();
}
@@ -239,7 +238,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
public function getPk($table = '')
{
if (empty($this->pk)) {
$this->pk = $this->query->getTableInfo($table, 'pk');
$this->pk = $this->db()->getTableInfo($table, '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);
@@ -352,11 +351,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return false;
}
$result = $this->query->insert($this->data);
$result = $this->db()->insert($this->data);
// 获取自动增长主键
if ($result && $getId) {
$insertId = $this->query->getLastInsID();
$insertId = $this->db()->getLastInsID();
$pk = $this->getPk();
if (is_string($pk) && $insertId) {
$this->data[$pk] = $insertId;
@@ -448,7 +447,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return false;
}
$result = $this->query->delete($this->data);
$result = $this->db()->delete($this->data);
$this->trigger('after_delete', $this);
return $result;
@@ -882,7 +881,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/
public function db()
{
$model = get_called_class();
$model = $this->class;
if (!isset(self::$links[$model])) {
// 设置当前模型 确保查询返回模型对象
$query = Db::connect($this->connection)->model($model);
@@ -905,11 +904,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
if (method_exists($this, 'scope' . $method)) {
// 动态调用命名范围
$method = 'scope' . $method;
array_unshift($args, $this->query);
array_unshift($args, $this->db());
call_user_func_array([$this, $method], $args);
return $this;
} 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)) {
// 获取字段类型信息并缓存
$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])) {
// 自动写入的时间戳字段

View File

@@ -173,7 +173,8 @@ class Merge extends Model
// 处理模型数据
$data = $this->parseData($this->name, $this->data);
$this->query->startTrans('merge_save_' . $this->name);
$db = $this->db();
$db->startTrans('merge_save_' . $this->name);
try {
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) {
$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);
$query = clone $this->query;
$query = clone $db;
$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) {
$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);
$query = clone $this->query;
$query = clone $db;
$query->table($table)->strict(false)->insert($data);
}
$result = $insertId;
@@ -231,10 +232,10 @@ class Merge extends Model
// 新增回调
$this->trigger('after_insert', $this);
}
$this->query->commit('merge_save_' . $this->name);
$db->commit('merge_save_' . $this->name);
return $result;
} catch (\PDOException $e) {
$this->query->rollback();
$db->rollback();
return false;
}
}
@@ -250,25 +251,26 @@ class Merge extends Model
return false;
}
$this->query->startTrans('merge_delete_' . $this->name);
$db = $this->query;
$db->startTrans('merge_delete_' . $this->name);
try {
$result = $this->query->delete($this->data);
$result = $db->delete($this->data);
if ($result) {
// 获取主键数据
$pk = $this->data[$this->getPk()];
// 删除关联数据
foreach (static::$relationModel as $key => $model) {
$table = is_int($key) ? $this->query->getTable($model) : $model;
$query = clone $this->query;
$table = is_int($key) ? $db->getTable($model) : $model;
$query = clone $db;
$query->table($table)->where($this->fk, $pk)->delete();
}
}
$this->trigger('after_delete', $this);
$this->query->commit('merge_delete_' . $this->name);
$db->commit('merge_delete_' . $this->name);
return $result;
} catch (\PDOException $e) {
$this->query->rollback();
$db->rollback();
return false;
}
}