改进关联模型

This commit is contained in:
thinkphp
2017-04-01 16:13:44 +08:00
parent 2ebf07ba16
commit d3be47e269
7 changed files with 39 additions and 13 deletions

View File

@@ -50,7 +50,13 @@ class BelongsTo extends OneToOne
if ($closure) {
call_user_func_array($closure, [ & $this->query]);
}
return $this->query->where($this->localKey, $this->parent->$foreignKey)->relation($subRelation)->find();
$relationModel = $this->query
->where($this->localKey, $this->parent->$foreignKey)
->relation($subRelation)
->find();
$relationModel->setParent(clone $this->parent);
return $relationModel;
}
/**

View File

@@ -47,7 +47,14 @@ class HasMany extends Relation
if ($closure) {
call_user_func_array($closure, [ & $this->query]);
}
return $this->relation($subRelation)->select();
$list = $this->relation($subRelation)->select();
$parent = clone $this->parent;
foreach ($list as &$model) {
$model->setParent($parent);
}
return $list;
}
/**

View File

@@ -57,6 +57,7 @@ class HasManyThrough extends Relation
if ($closure) {
call_user_func_array($closure, [ & $this->query]);
}
return $this->relation($subRelation)->select();
}
@@ -96,8 +97,7 @@ class HasManyThrough extends Relation
* @return void
*/
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
{
}
{}
/**
* 预载入关联查询 返回模型对象
@@ -110,8 +110,7 @@ class HasManyThrough extends Relation
* @return void
*/
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
{
}
{}
/**
* 关联统计
@@ -121,8 +120,7 @@ class HasManyThrough extends Relation
* @return integer
*/
public function relationCount($result, $closure)
{
}
{}
/**
* 执行基础查询(进执行一次)

View File

@@ -50,7 +50,10 @@ class HasOne extends OneToOne
call_user_func_array($closure, [ & $this->query]);
}
// 判断关联类型执行查询
return $this->query->where($this->foreignKey, $this->parent->$localKey)->relation($subRelation)->find();
$relationModel = $this->query->where($this->foreignKey, $this->parent->$localKey)->relation($subRelation)->find();
$relationModel->setParent(clone $this->parent);
return $relationModel;
}
/**

View File

@@ -56,7 +56,14 @@ class MorphMany extends Relation
if ($closure) {
call_user_func_array($closure, [ & $this->query]);
}
return $this->relation($subRelation)->select();
$list = $this->relation($subRelation)->select();
$parent = clone $this->parent;
foreach ($list as &$model) {
$model->setParent($parent);
}
return $list;
}
/**

View File

@@ -56,7 +56,10 @@ class MorphOne extends Relation
if ($closure) {
call_user_func_array($closure, [ & $this->query]);
}
return $this->relation($subRelation)->find();
$relationModel = $this->relation($subRelation)->find();
$relationModel->setParent(clone $this->parent);
return $relationModel;
}
/**

View File

@@ -56,8 +56,10 @@ class MorphTo extends Relation
// 多态模型
$model = $this->parseModel($this->parent->$morphType);
// 主键数据
$pk = $this->parent->$morphKey;
return (new $model)->relation($subRelation)->find($pk);
$pk = $this->parent->$morphKey;
$relationModel = (new $model)->relation($subRelation)->find($pk);
$relationModel->setParent(clone $this->parent);
return $relationModel;
}
/**