修正聚合模型 以及模型方法和属性同名的BUG

This commit is contained in:
thinkphp
2017-04-19 14:24:44 +08:00
parent 628b37aab1
commit 77dbb1315b
2 changed files with 40 additions and 33 deletions

View File

@@ -1083,11 +1083,14 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
{
$relation = Loader::parseName($attr, 1, false);
if (method_exists($this, $relation) && $this->$relation() instanceof Relation) {
return $relation;
} else {
return false;
if (method_exists($this, $relation)) {
$reflect = new \ReflectionMethod($this, $relation);
if (0 == $reflect->getNumberOfParameters() && $this->$relation() instanceof Relation) {
return $relation;
}
}
return false;
}
/**
@@ -1522,9 +1525,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/
public static function scope($name)
{
if ($name instanceof Query) {
return $name;
}
$model = new static();
$query = $model->db();
$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);
call_user_func_array([$model, $method], $args);
return $query;
return $model;
} else {
return call_user_func_array([$query, $method], $args);
}

View File

@@ -11,6 +11,7 @@
namespace think\model;
use think\Db;
use think\db\Query;
use think\Model;
@@ -120,22 +121,19 @@ class Merge extends Model
* @access public
* @param string $model 模型名称
* @param array $data 数据
* @param bool $insert 是否新增
* @return array
*/
protected function parseData($model, $data, $insert = false)
protected function parseData($model, $data)
{
$item = [];
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)) {
list($name, $key) = explode('.', $this->mapFields[$key]);
if ($model == $name) {
$item[$key] = $val;
}
} else {
if ($this->fk != $key && array_key_exists($key, $this->mapFields)) {
list($name, $key) = explode('.', $this->mapFields[$key]);
if ($model == $name) {
$item[$key] = $val;
}
} else {
$item[$key] = $val;
}
}
return $item;
@@ -174,6 +172,11 @@ class Merge extends Model
$this->setAttr($this->updateTime, null);
}
// 事件回调
if (false === $this->trigger('before_write', $this)) {
return false;
}
$db = $this->db();
$db->startTrans();
$pk = $this->getPk();
@@ -190,8 +193,16 @@ class Merge extends Model
$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 (!isset($where[$pk])) {
unset($where);
@@ -207,14 +218,12 @@ class Merge extends Model
$name = is_int($key) ? $model : $key;
$table = is_int($key) ? $db->getTable($model) : $model;
// 处理关联模型数据
$data = $this->parseData($name, $this->data);
$query = new Query;
if ($query->table($table)->strict(false)->where($this->fk, $this->data[$this->getPk()])->update($data)) {
$data = $this->parseData($name, $data);
if (Db::table($table)->strict(false)->where($this->fk, $this->data[$this->getPk()])->update($data)) {
$result = 1;
}
}
// 清空change
$this->change = [];
// 新增回调
$this->trigger('after_update', $this);
} 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);
if ($result) {
@@ -240,9 +249,6 @@ class Merge extends Model
if ($insertId) {
if (is_string($pk)) {
$this->data[$pk] = $insertId;
if ($this->fk == $pk) {
$this->change[] = $pk;
}
}
$this->data[$this->fk] = $insertId;
}
@@ -256,19 +262,20 @@ class Merge extends Model
$name = is_int($key) ? $model : $key;
$table = is_int($key) ? $db->getTable($model) : $model;
// 处理关联模型数据
$data = $this->parseData($name, $source, true);
$query = new Query;
$query->table($table)->strict(false)->insert($data);
$data = $this->parseData($name, $source);
Db::table($table)->strict(false)->insert($data);
}
}
// 标记为更新
$this->isUpdate = true;
// 清空change
$this->change = [];
// 新增回调
$this->trigger('after_insert', $this);
}
$db->commit();
// 写入回调
$this->trigger('after_write', $this);
$this->origin = $this->data;
return $result;
} catch (\Exception $e) {
$db->rollback();