修正Relation的save方法

This commit is contained in:
thinkphp
2016-05-06 23:31:01 +08:00
parent 5bcc13a273
commit ac97882da9

View File

@@ -158,8 +158,8 @@ class Relation
$data = $this->eagerlyOneToMany($model, [ $data = $this->eagerlyOneToMany($model, [
$foreignKey => [ $foreignKey => [
'in', 'in',
$range $range,
] ],
], $relation, $subRelation, $closure); ], $relation, $subRelation, $closure);
// 关联数据封装 // 关联数据封装
@@ -186,8 +186,8 @@ class Relation
$data = $this->eagerlyManyToMany($model, [ $data = $this->eagerlyManyToMany($model, [
'pivot.' . $foreignKey => [ 'pivot.' . $foreignKey => [
'in', 'in',
$range $range,
] ],
], $relation, $subRelation); ], $relation, $subRelation);
// 关联数据封装 // 关联数据封装
@@ -465,14 +465,14 @@ class Relation
*/ */
public function save($data, array $pivot = []) public function save($data, array $pivot = [])
{ {
if ($data instanceof Model) {
$data = $data->toArray();
}
// 判断关联类型 // 判断关联类型
switch ($this->type) { switch ($this->type) {
case self::HAS_ONE: case self::HAS_ONE:
case self::BELONGS_TO: case self::BELONGS_TO:
case self::HAS_MANY: case self::HAS_MANY:
if ($data instanceof Model) {
$data = $data->toArray();
}
// 保存关联表数据 // 保存关联表数据
$data[$this->foreignKey] = $this->parent->{$this->localKey}; $data[$this->foreignKey] = $this->parent->{$this->localKey};
$model = new $this->model; $model = new $this->model;