完善关联统计 避免一对一关联报错

This commit is contained in:
thinkphp
2016-12-22 13:52:41 +08:00
parent bde2607442
commit 91e089b7e7
6 changed files with 54 additions and 43 deletions

View File

@@ -128,14 +128,11 @@ class BelongsToMany extends Relation
*/
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
{
$localKey = $this->localKey;
$foreignKey = $this->foreignKey;
$pk = $result->getPk();
if (isset($result->$pk)) {
$pk = $result->$pk;
// 查询管理数据
$data = $this->eagerlyManyToMany(['pivot.' . $localKey => $pk], $relation, $subRelation);
$data = $this->eagerlyManyToMany(['pivot.' . $this->localKey => $pk], $relation, $subRelation);
// 关联数据封装
if (!isset($data[$pk])) {
@@ -154,13 +151,11 @@ class BelongsToMany extends Relation
*/
public function relationCount($result, $closure)
{
$localKey = $this->localKey;
$foreignKey = $this->foreignKey;
$pk = $result->getPk();
$count = 0;
$pk = $result->getPk();
$count = 0;
if (isset($result->$pk)) {
$pk = $result->$pk;
$count = $this->belongsToManyQuery($this->middle, $foreignKey, $localKey, ['pivot.' . $localKey => $pk])->count();
$count = $this->belongsToManyQuery($this->middle, $this->foreignKey, $this->localKey, ['pivot.' . $this->localKey => $pk])->count();
}
return $count;
}
@@ -175,10 +170,8 @@ class BelongsToMany extends Relation
*/
protected function eagerlyManyToMany($where, $relation, $subRelation = '')
{
$foreignKey = $this->foreignKey;
$localKey = $this->localKey;
// 预载入关联查询 支持嵌套预载入
$list = $this->belongsToManyQuery($this->middle, $foreignKey, $localKey, $where)->with($subRelation)->select();
$list = $this->belongsToManyQuery($this->middle, $this->foreignKey, $this->localKey, $where)->with($subRelation)->select();
// 组装模型数据
$data = [];
@@ -193,8 +186,8 @@ class BelongsToMany extends Relation
}
}
}
$set->pivot = new Pivot($pivot, $this->middle);
$data[$pivot[$localKey]][] = $set;
$set->pivot = new Pivot($pivot, $this->middle);
$data[$pivot[$this->localKey]][] = $set;
}
return $data;
}

View File

@@ -58,10 +58,8 @@ class HasMany extends Relation
*/
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
{
$localKey = $this->localKey;
$foreignKey = $this->foreignKey;
$range = [];
$localKey = $this->localKey;
$range = [];
foreach ($resultSet as $result) {
// 获取关联外键列表
if (isset($result->$localKey)) {
@@ -70,9 +68,9 @@ class HasMany extends Relation
}
if (!empty($range)) {
$this->where[$foreignKey] = ['in', $range];
$data = $this->eagerlyOneToMany($this, [
$foreignKey => [
$this->where[$this->foreignKey] = ['in', $range];
$data = $this->eagerlyOneToMany($this, [
$this->foreignKey => [
'in',
$range,
],
@@ -100,11 +98,10 @@ class HasMany extends Relation
*/
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
{
$localKey = $this->localKey;
$foreignKey = $this->foreignKey;
$localKey = $this->localKey;
if (isset($result->$localKey)) {
$data = $this->eagerlyOneToMany($this, [$foreignKey => $result->$localKey], $relation, $subRelation, $closure);
$data = $this->eagerlyOneToMany($this, [$this->foreignKey => $result->$localKey], $relation, $subRelation, $closure);
// 关联数据封装
if (!isset($data[$result->$localKey])) {
$data[$result->$localKey] = [];
@@ -122,14 +119,13 @@ class HasMany extends Relation
*/
public function relationCount($result, $closure)
{
$localKey = $this->localKey;
$foreignKey = $this->foreignKey;
$count = 0;
$localKey = $this->localKey;
$count = 0;
if (isset($result->$localKey)) {
if ($closure) {
call_user_func_array($closure, [ & $this->query]);
}
$count = $this->query->where([$foreignKey => $result->$localKey])->count();
$count = $this->query->where([$this->foreignKey => $result->$localKey])->count();
}
return $count;
}

View File

@@ -67,8 +67,7 @@ class HasManyThrough extends Relation
* @return void
*/
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
{
}
{}
/**
* 预载入关联查询 返回模型对象
@@ -81,8 +80,17 @@ class HasManyThrough extends Relation
* @return void
*/
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
{
}
{}
/**
* 关联统计
* @access public
* @param Model $result 数据对象
* @param \Closure $closure 闭包
* @return integer
*/
public function relationCount($result, $closure)
{}
/**
* 执行基础查询(进执行一次)

View File

@@ -106,12 +106,9 @@ class MorphMany extends Relation
*/
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
{
$morphType = $this->morphType;
$morphKey = $this->morphKey;
$type = $this->type;
$pk = $result->getPk();
$pk = $result->getPk();
if (isset($result->$pk)) {
$data = $this->eagerlyMorphToMany([$morphKey => $result->$pk, $morphType => $type], $relation, $subRelation, $closure);
$data = $this->eagerlyMorphToMany([$this->morphKey => $result->$pk, $this->morphType => $this->type], $relation, $subRelation, $closure);
$result->setAttr($relation, $this->resultSetBuild($data[$result->$pk], $class));
}
}
@@ -125,16 +122,13 @@ class MorphMany extends Relation
*/
public function relationCount($result, $closure)
{
$morphType = $this->morphType;
$morphKey = $this->morphKey;
$type = $this->type;
$pk = $result->getPk();
$count = 0;
$pk = $result->getPk();
$count = 0;
if (isset($result->$pk)) {
if ($closure) {
call_user_func_array($closure, [ & $this->query]);
}
$count = $this->query->where([$morphKey => $result->$pk, $morphType => $type])->count();
$count = $this->query->where([$this->morphKey => $result->$pk, $this->morphType => $this->type])->count();
}
return $count;
}

View File

@@ -136,6 +136,16 @@ class MorphTo extends Relation
$this->eagerlyMorphToOne($model, $relation, $result, $subRelation);
}
/**
* 关联统计
* @access public
* @param Model $result 数据对象
* @param \Closure $closure 闭包
* @return integer
*/
public function relationCount($result, $closure)
{}
/**
* 多态MorphTo 关联模型预查询
* @access public

View File

@@ -179,6 +179,16 @@ abstract class OneToOne extends Relation
return $this;
}
/**
* 关联统计
* @access public
* @param Model $result 数据对象
* @param \Closure $closure 闭包
* @return integer
*/
public function relationCount($result, $closure)
{}
/**
* 一对一 关联模型预查询拼装
* @access public