改进append方法支持一对一关联的bind设置

This commit is contained in:
thinkphp
2017-09-15 17:32:50 +08:00
parent 8d39aa9b05
commit 2a2b3882af
4 changed files with 42 additions and 10 deletions

View File

@@ -826,7 +826,27 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$relation = $this->getAttr($key);
$item[$key] = $relation->append([$attr])->toArray();
} else {
$item[$name] = $this->getAttr($name);
$relation = Loader::parseName($name, 1, false);
if (method_exists($this, $relation)) {
$modelRelation = $this->$relation();
$value = $this->getRelationData($modelRelation);
if (method_exists($modelRelation, 'getBindAttr')) {
$bindAttr = $modelRelation->getBindAttr();
foreach ($bindAttr as $key => $attr) {
$key = is_numeric($key) ? $attr : $key;
if (isset($this->data[$key])) {
throw new Exception('bind attr has exists:' . $key);
} else {
$item[$key] = $value ? $value->$attr : null;
}
}
} else {
$item[$name] = $value;
}
} else {
$item[$name] = $this->getAttr($name);
}
}
}
}