mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 15:42:48 +08:00
改进Relation类
This commit is contained in:
@@ -72,41 +72,45 @@ class Relation
|
|||||||
$relation = $this->parent->$name();
|
$relation = $this->parent->$name();
|
||||||
$foreignKey = $this->foreignKey;
|
$foreignKey = $this->foreignKey;
|
||||||
$localKey = $this->localKey;
|
$localKey = $this->localKey;
|
||||||
// 判断关联类型执行查询
|
if ($relation instanceof Relation) {
|
||||||
switch ($this->type) {
|
// 判断关联类型执行查询
|
||||||
case self::HAS_ONE:
|
switch ($this->type) {
|
||||||
$result = $relation->where($foreignKey, $this->parent->$localKey)->find();
|
case self::HAS_ONE:
|
||||||
break;
|
$result = $relation->where($foreignKey, $this->parent->$localKey)->find();
|
||||||
case self::BELONGS_TO:
|
break;
|
||||||
$result = $relation->where($localKey, $this->parent->$foreignKey)->find();
|
case self::BELONGS_TO:
|
||||||
break;
|
$result = $relation->where($localKey, $this->parent->$foreignKey)->find();
|
||||||
case self::HAS_MANY:
|
break;
|
||||||
$result = $relation->where($foreignKey, $this->parent->$localKey)->select();
|
case self::HAS_MANY:
|
||||||
break;
|
$result = $relation->where($foreignKey, $this->parent->$localKey)->select();
|
||||||
case self::BELONGS_TO_MANY:
|
break;
|
||||||
// 关联查询
|
case self::BELONGS_TO_MANY:
|
||||||
$pk = $this->parent->getPk();
|
// 关联查询
|
||||||
$condition['pivot.' . $localKey] = $this->parent->$pk;
|
$pk = $this->parent->getPk();
|
||||||
$result = $this->belongsToManyQuery($relation, $this->middle, $foreignKey, $localKey, $condition)->select();
|
$condition['pivot.' . $localKey] = $this->parent->$pk;
|
||||||
foreach ($result as $set) {
|
$result = $this->belongsToManyQuery($relation, $this->middle, $foreignKey, $localKey, $condition)->select();
|
||||||
$pivot = [];
|
foreach ($result as $set) {
|
||||||
foreach ($set->toArray() as $key => $val) {
|
$pivot = [];
|
||||||
if (strpos($key, '__')) {
|
foreach ($set->toArray() as $key => $val) {
|
||||||
list($name, $attr) = explode('__', $key, 2);
|
if (strpos($key, '__')) {
|
||||||
if ('pivot' == $name) {
|
list($name, $attr) = explode('__', $key, 2);
|
||||||
$pivot[$attr] = $val;
|
if ('pivot' == $name) {
|
||||||
unset($set->$key);
|
$pivot[$attr] = $val;
|
||||||
|
unset($set->$key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$set->pivot = new Pivot($pivot, $this->middle);
|
||||||
}
|
}
|
||||||
$set->pivot = new Pivot($pivot, $this->middle);
|
break;
|
||||||
}
|
default:
|
||||||
break;
|
// 直接返回
|
||||||
default:
|
$result = $relation;
|
||||||
// 直接返回
|
}
|
||||||
$result = $relation;
|
return $result;
|
||||||
|
} else {
|
||||||
|
return $relation;
|
||||||
}
|
}
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user