mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 23:52:49 +08:00
完善关联统计 避免一对一关联报错
This commit is contained in:
@@ -128,14 +128,11 @@ class BelongsToMany extends Relation
|
|||||||
*/
|
*/
|
||||||
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
|
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
|
||||||
{
|
{
|
||||||
$localKey = $this->localKey;
|
|
||||||
$foreignKey = $this->foreignKey;
|
|
||||||
|
|
||||||
$pk = $result->getPk();
|
$pk = $result->getPk();
|
||||||
if (isset($result->$pk)) {
|
if (isset($result->$pk)) {
|
||||||
$pk = $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])) {
|
if (!isset($data[$pk])) {
|
||||||
@@ -154,13 +151,11 @@ class BelongsToMany extends Relation
|
|||||||
*/
|
*/
|
||||||
public function relationCount($result, $closure)
|
public function relationCount($result, $closure)
|
||||||
{
|
{
|
||||||
$localKey = $this->localKey;
|
$pk = $result->getPk();
|
||||||
$foreignKey = $this->foreignKey;
|
$count = 0;
|
||||||
$pk = $result->getPk();
|
|
||||||
$count = 0;
|
|
||||||
if (isset($result->$pk)) {
|
if (isset($result->$pk)) {
|
||||||
$pk = $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;
|
return $count;
|
||||||
}
|
}
|
||||||
@@ -175,10 +170,8 @@ class BelongsToMany extends Relation
|
|||||||
*/
|
*/
|
||||||
protected function eagerlyManyToMany($where, $relation, $subRelation = '')
|
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 = [];
|
$data = [];
|
||||||
@@ -193,8 +186,8 @@ class BelongsToMany extends Relation
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$set->pivot = new Pivot($pivot, $this->middle);
|
$set->pivot = new Pivot($pivot, $this->middle);
|
||||||
$data[$pivot[$localKey]][] = $set;
|
$data[$pivot[$this->localKey]][] = $set;
|
||||||
}
|
}
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,10 +58,8 @@ class HasMany extends Relation
|
|||||||
*/
|
*/
|
||||||
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
|
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
|
||||||
{
|
{
|
||||||
$localKey = $this->localKey;
|
$localKey = $this->localKey;
|
||||||
$foreignKey = $this->foreignKey;
|
$range = [];
|
||||||
|
|
||||||
$range = [];
|
|
||||||
foreach ($resultSet as $result) {
|
foreach ($resultSet as $result) {
|
||||||
// 获取关联外键列表
|
// 获取关联外键列表
|
||||||
if (isset($result->$localKey)) {
|
if (isset($result->$localKey)) {
|
||||||
@@ -70,9 +68,9 @@ class HasMany extends Relation
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($range)) {
|
if (!empty($range)) {
|
||||||
$this->where[$foreignKey] = ['in', $range];
|
$this->where[$this->foreignKey] = ['in', $range];
|
||||||
$data = $this->eagerlyOneToMany($this, [
|
$data = $this->eagerlyOneToMany($this, [
|
||||||
$foreignKey => [
|
$this->foreignKey => [
|
||||||
'in',
|
'in',
|
||||||
$range,
|
$range,
|
||||||
],
|
],
|
||||||
@@ -100,11 +98,10 @@ class HasMany extends Relation
|
|||||||
*/
|
*/
|
||||||
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
|
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
|
||||||
{
|
{
|
||||||
$localKey = $this->localKey;
|
$localKey = $this->localKey;
|
||||||
$foreignKey = $this->foreignKey;
|
|
||||||
|
|
||||||
if (isset($result->$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])) {
|
if (!isset($data[$result->$localKey])) {
|
||||||
$data[$result->$localKey] = [];
|
$data[$result->$localKey] = [];
|
||||||
@@ -122,14 +119,13 @@ class HasMany extends Relation
|
|||||||
*/
|
*/
|
||||||
public function relationCount($result, $closure)
|
public function relationCount($result, $closure)
|
||||||
{
|
{
|
||||||
$localKey = $this->localKey;
|
$localKey = $this->localKey;
|
||||||
$foreignKey = $this->foreignKey;
|
$count = 0;
|
||||||
$count = 0;
|
|
||||||
if (isset($result->$localKey)) {
|
if (isset($result->$localKey)) {
|
||||||
if ($closure) {
|
if ($closure) {
|
||||||
call_user_func_array($closure, [ & $this->query]);
|
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;
|
return $count;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,8 +67,7 @@ class HasManyThrough extends Relation
|
|||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
|
public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure, $class)
|
||||||
{
|
{}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 预载入关联查询 返回模型对象
|
* 预载入关联查询 返回模型对象
|
||||||
@@ -81,8 +80,17 @@ class HasManyThrough extends Relation
|
|||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
|
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
|
||||||
{
|
{}
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* 关联统计
|
||||||
|
* @access public
|
||||||
|
* @param Model $result 数据对象
|
||||||
|
* @param \Closure $closure 闭包
|
||||||
|
* @return integer
|
||||||
|
*/
|
||||||
|
public function relationCount($result, $closure)
|
||||||
|
{}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行基础查询(进执行一次)
|
* 执行基础查询(进执行一次)
|
||||||
|
|||||||
@@ -106,12 +106,9 @@ class MorphMany extends Relation
|
|||||||
*/
|
*/
|
||||||
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
|
public function eagerlyResult(&$result, $relation, $subRelation, $closure, $class)
|
||||||
{
|
{
|
||||||
$morphType = $this->morphType;
|
$pk = $result->getPk();
|
||||||
$morphKey = $this->morphKey;
|
|
||||||
$type = $this->type;
|
|
||||||
$pk = $result->getPk();
|
|
||||||
if (isset($result->$pk)) {
|
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));
|
$result->setAttr($relation, $this->resultSetBuild($data[$result->$pk], $class));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -125,16 +122,13 @@ class MorphMany extends Relation
|
|||||||
*/
|
*/
|
||||||
public function relationCount($result, $closure)
|
public function relationCount($result, $closure)
|
||||||
{
|
{
|
||||||
$morphType = $this->morphType;
|
$pk = $result->getPk();
|
||||||
$morphKey = $this->morphKey;
|
$count = 0;
|
||||||
$type = $this->type;
|
|
||||||
$pk = $result->getPk();
|
|
||||||
$count = 0;
|
|
||||||
if (isset($result->$pk)) {
|
if (isset($result->$pk)) {
|
||||||
if ($closure) {
|
if ($closure) {
|
||||||
call_user_func_array($closure, [ & $this->query]);
|
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;
|
return $count;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -136,6 +136,16 @@ class MorphTo extends Relation
|
|||||||
$this->eagerlyMorphToOne($model, $relation, $result, $subRelation);
|
$this->eagerlyMorphToOne($model, $relation, $result, $subRelation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关联统计
|
||||||
|
* @access public
|
||||||
|
* @param Model $result 数据对象
|
||||||
|
* @param \Closure $closure 闭包
|
||||||
|
* @return integer
|
||||||
|
*/
|
||||||
|
public function relationCount($result, $closure)
|
||||||
|
{}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 多态MorphTo 关联模型预查询
|
* 多态MorphTo 关联模型预查询
|
||||||
* @access public
|
* @access public
|
||||||
|
|||||||
@@ -179,6 +179,16 @@ abstract class OneToOne extends Relation
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关联统计
|
||||||
|
* @access public
|
||||||
|
* @param Model $result 数据对象
|
||||||
|
* @param \Closure $closure 闭包
|
||||||
|
* @return integer
|
||||||
|
*/
|
||||||
|
public function relationCount($result, $closure)
|
||||||
|
{}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 一对一 关联模型预查询拼装
|
* 一对一 关联模型预查询拼装
|
||||||
* @access public
|
* @access public
|
||||||
|
|||||||
Reference in New Issue
Block a user