mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 15:12:48 +08:00
修正聚合模型 以及模型方法和属性同名的BUG
This commit is contained in:
@@ -1083,11 +1083,14 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
{
|
{
|
||||||
$relation = Loader::parseName($attr, 1, false);
|
$relation = Loader::parseName($attr, 1, false);
|
||||||
|
|
||||||
if (method_exists($this, $relation) && $this->$relation() instanceof Relation) {
|
if (method_exists($this, $relation)) {
|
||||||
return $relation;
|
$reflect = new \ReflectionMethod($this, $relation);
|
||||||
} else {
|
if (0 == $reflect->getNumberOfParameters() && $this->$relation() instanceof Relation) {
|
||||||
return false;
|
return $relation;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1522,9 +1525,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public static function scope($name)
|
public static function scope($name)
|
||||||
{
|
{
|
||||||
if ($name instanceof Query) {
|
|
||||||
return $name;
|
|
||||||
}
|
|
||||||
$model = new static();
|
$model = new static();
|
||||||
$query = $model->db();
|
$query = $model->db();
|
||||||
$params = func_get_args();
|
$params = func_get_args();
|
||||||
@@ -1542,7 +1542,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $query;
|
return $model;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1937,7 +1937,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
array_unshift($args, $query);
|
array_unshift($args, $query);
|
||||||
|
|
||||||
call_user_func_array([$model, $method], $args);
|
call_user_func_array([$model, $method], $args);
|
||||||
return $query;
|
return $model;
|
||||||
} else {
|
} else {
|
||||||
return call_user_func_array([$query, $method], $args);
|
return call_user_func_array([$query, $method], $args);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
namespace think\model;
|
namespace think\model;
|
||||||
|
|
||||||
|
use think\Db;
|
||||||
use think\db\Query;
|
use think\db\Query;
|
||||||
use think\Model;
|
use think\Model;
|
||||||
|
|
||||||
@@ -120,22 +121,19 @@ class Merge extends Model
|
|||||||
* @access public
|
* @access public
|
||||||
* @param string $model 模型名称
|
* @param string $model 模型名称
|
||||||
* @param array $data 数据
|
* @param array $data 数据
|
||||||
* @param bool $insert 是否新增
|
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function parseData($model, $data, $insert = false)
|
protected function parseData($model, $data)
|
||||||
{
|
{
|
||||||
$item = [];
|
$item = [];
|
||||||
foreach ($data as $key => $val) {
|
foreach ($data as $key => $val) {
|
||||||
if ($insert || in_array($key, $this->change) || $this->isPk($key)) {
|
if ($this->fk != $key && array_key_exists($key, $this->mapFields)) {
|
||||||
if ($this->fk != $key && array_key_exists($key, $this->mapFields)) {
|
list($name, $key) = explode('.', $this->mapFields[$key]);
|
||||||
list($name, $key) = explode('.', $this->mapFields[$key]);
|
if ($model == $name) {
|
||||||
if ($model == $name) {
|
|
||||||
$item[$key] = $val;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$item[$key] = $val;
|
$item[$key] = $val;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
$item[$key] = $val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $item;
|
return $item;
|
||||||
@@ -174,6 +172,11 @@ class Merge extends Model
|
|||||||
$this->setAttr($this->updateTime, null);
|
$this->setAttr($this->updateTime, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 事件回调
|
||||||
|
if (false === $this->trigger('before_write', $this)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$db = $this->db();
|
$db = $this->db();
|
||||||
$db->startTrans();
|
$db->startTrans();
|
||||||
$pk = $this->getPk();
|
$pk = $this->getPk();
|
||||||
@@ -190,8 +193,16 @@ class Merge extends Model
|
|||||||
$where = $this->updateWhere;
|
$where = $this->updateWhere;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取有更新的数据
|
||||||
|
$data = $this->getChangedData();
|
||||||
|
// 保留主键数据
|
||||||
|
foreach ($this->data as $key => $val) {
|
||||||
|
if ($this->isPk($key)) {
|
||||||
|
$data[$key] = $val;
|
||||||
|
}
|
||||||
|
}
|
||||||
// 处理模型数据
|
// 处理模型数据
|
||||||
$data = $this->parseData($this->name, $this->data);
|
$data = $this->parseData($this->name, $data);
|
||||||
if (is_string($pk) && isset($data[$pk])) {
|
if (is_string($pk) && isset($data[$pk])) {
|
||||||
if (!isset($where[$pk])) {
|
if (!isset($where[$pk])) {
|
||||||
unset($where);
|
unset($where);
|
||||||
@@ -207,14 +218,12 @@ class Merge extends Model
|
|||||||
$name = is_int($key) ? $model : $key;
|
$name = is_int($key) ? $model : $key;
|
||||||
$table = is_int($key) ? $db->getTable($model) : $model;
|
$table = is_int($key) ? $db->getTable($model) : $model;
|
||||||
// 处理关联模型数据
|
// 处理关联模型数据
|
||||||
$data = $this->parseData($name, $this->data);
|
$data = $this->parseData($name, $data);
|
||||||
$query = new Query;
|
if (Db::table($table)->strict(false)->where($this->fk, $this->data[$this->getPk()])->update($data)) {
|
||||||
if ($query->table($table)->strict(false)->where($this->fk, $this->data[$this->getPk()])->update($data)) {
|
|
||||||
$result = 1;
|
$result = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 清空change
|
|
||||||
$this->change = [];
|
|
||||||
// 新增回调
|
// 新增回调
|
||||||
$this->trigger('after_update', $this);
|
$this->trigger('after_update', $this);
|
||||||
} else {
|
} else {
|
||||||
@@ -231,7 +240,7 @@ class Merge extends Model
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 处理模型数据
|
// 处理模型数据
|
||||||
$data = $this->parseData($this->name, $this->data, true);
|
$data = $this->parseData($this->name, $this->data);
|
||||||
// 写入主表数据
|
// 写入主表数据
|
||||||
$result = $db->name($this->name)->strict(false)->insert($data);
|
$result = $db->name($this->name)->strict(false)->insert($data);
|
||||||
if ($result) {
|
if ($result) {
|
||||||
@@ -240,9 +249,6 @@ class Merge extends Model
|
|||||||
if ($insertId) {
|
if ($insertId) {
|
||||||
if (is_string($pk)) {
|
if (is_string($pk)) {
|
||||||
$this->data[$pk] = $insertId;
|
$this->data[$pk] = $insertId;
|
||||||
if ($this->fk == $pk) {
|
|
||||||
$this->change[] = $pk;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
$this->data[$this->fk] = $insertId;
|
$this->data[$this->fk] = $insertId;
|
||||||
}
|
}
|
||||||
@@ -256,19 +262,20 @@ class Merge extends Model
|
|||||||
$name = is_int($key) ? $model : $key;
|
$name = is_int($key) ? $model : $key;
|
||||||
$table = is_int($key) ? $db->getTable($model) : $model;
|
$table = is_int($key) ? $db->getTable($model) : $model;
|
||||||
// 处理关联模型数据
|
// 处理关联模型数据
|
||||||
$data = $this->parseData($name, $source, true);
|
$data = $this->parseData($name, $source);
|
||||||
$query = new Query;
|
Db::table($table)->strict(false)->insert($data);
|
||||||
$query->table($table)->strict(false)->insert($data);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 标记为更新
|
// 标记为更新
|
||||||
$this->isUpdate = true;
|
$this->isUpdate = true;
|
||||||
// 清空change
|
|
||||||
$this->change = [];
|
|
||||||
// 新增回调
|
// 新增回调
|
||||||
$this->trigger('after_insert', $this);
|
$this->trigger('after_insert', $this);
|
||||||
}
|
}
|
||||||
$db->commit();
|
$db->commit();
|
||||||
|
// 写入回调
|
||||||
|
$this->trigger('after_write', $this);
|
||||||
|
|
||||||
|
$this->origin = $this->data;
|
||||||
return $result;
|
return $result;
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$db->rollback();
|
$db->rollback();
|
||||||
|
|||||||
Reference in New Issue
Block a user