改进一对一关联预载入

This commit is contained in:
thinkphp
2017-01-22 17:36:56 +08:00
parent b1fa683e3d
commit e2e217f1d6
2 changed files with 8 additions and 10 deletions

View File

@@ -11,7 +11,6 @@
namespace think\model\relation;
use think\Exception;
use think\Loader;
use think\Model;
@@ -86,12 +85,12 @@ class BelongsTo extends OneToOne
foreach ($resultSet as $result) {
// 关联模型
if (!isset($data[$result->$localKey])) {
throw new Exception('relation data not exists :' . $this->model);
$relationModel = null;
} else {
$relationModel = $data[$result->$localKey];
}
if (!empty($this->bindAttr)) {
if ($relationModel && !empty($this->bindAttr)) {
// 绑定关联属性
$this->bindAttr($relationModel, $result, $this->bindAttr);
}
@@ -117,11 +116,11 @@ class BelongsTo extends OneToOne
$data = $this->eagerlyWhere($this, [$localKey => $result->$foreignKey], $localKey, $relation, $subRelation, $closure);
// 关联模型
if (!isset($data[$result->$localKey])) {
throw new Exception('relation data not exists :' . $this->model);
$relationModel = null;
} else {
$relationModel = $data[$result->$localKey];
}
if (!empty($this->bindAttr)) {
if ($relationModel && !empty($this->bindAttr)) {
// 绑定关联属性
$this->bindAttr($relationModel, $result, $this->bindAttr);
}

View File

@@ -11,7 +11,6 @@
namespace think\model\relation;
use think\Exception;
use think\Loader;
use think\Model;
@@ -113,11 +112,11 @@ class HasOne extends OneToOne
foreach ($resultSet as $result) {
// 关联模型
if (!isset($data[$result->$localKey])) {
throw new Exception('relation data not exists : ' . $this->model);
$relationModel = null;
} else {
$relationModel = $data[$result->$localKey];
}
if (!empty($this->bindAttr)) {
if ($relationModel && !empty($this->bindAttr)) {
// 绑定关联属性
$this->bindAttr($relationModel, $result, $this->bindAttr);
}
@@ -144,12 +143,12 @@ class HasOne extends OneToOne
// 关联模型
if (!isset($data[$result->$localKey])) {
throw new Exception('relation data not exists :' . $this->model);
$relationModel = null;
} else {
$relationModel = $data[$result->$localKey];
}
if (!empty($this->bindAttr)) {
if ($relationModel && !empty($this->bindAttr)) {
// 绑定关联属性
$this->bindAttr($relationModel, $result, $this->bindAttr);
}