mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
Query类relation支持嵌套关联
This commit is contained in:
@@ -1282,14 +1282,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach ($relations as $key => $relation) {
|
foreach ($relations as $key => $relation) {
|
||||||
|
$subRelation = '';
|
||||||
$closure = null;
|
$closure = null;
|
||||||
if ($relation instanceof \Closure) {
|
if ($relation instanceof \Closure) {
|
||||||
// 支持闭包查询过滤关联条件
|
// 支持闭包查询过滤关联条件
|
||||||
$closure = $relation;
|
$closure = $relation;
|
||||||
$relation = $key;
|
$relation = $key;
|
||||||
}
|
}
|
||||||
|
if (strpos($relation, '.')) {
|
||||||
|
list($relation, $subRelation) = explode('.', $relation);
|
||||||
|
}
|
||||||
$method = Loader::parseName($relation, 1, false);
|
$method = Loader::parseName($relation, 1, false);
|
||||||
$this->data[$relation] = $this->$method()->getRelation($closure);
|
$this->data[$relation] = $this->$method()->getRelation($subRelation, $closure);
|
||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,17 +38,17 @@ class BelongsTo extends OneToOne
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 延迟获取关联数据
|
* 延迟获取关联数据
|
||||||
|
* @param string $subRelation 子关联名
|
||||||
* @param \Closure $closure 闭包查询条件
|
* @param \Closure $closure 闭包查询条件
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
public function getRelation($closure = null)
|
public function getRelation($subRelation = '', $closure = null)
|
||||||
{
|
{
|
||||||
$foreignKey = $this->foreignKey;
|
$foreignKey = $this->foreignKey;
|
||||||
$localKey = $this->localKey;
|
|
||||||
if ($closure) {
|
if ($closure) {
|
||||||
call_user_func_array($closure, [ & $this->query]);
|
call_user_func_array($closure, [ & $this->query]);
|
||||||
}
|
}
|
||||||
return $this->query->where($localKey, $this->parent->$foreignKey)->find();
|
return $this->query->where($this->localKey, $this->parent->$foreignKey)->relation($subRelation)->find();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -46,10 +46,11 @@ class BelongsToMany extends Relation
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 延迟获取关联数据
|
* 延迟获取关联数据
|
||||||
|
* @param string $subRelation 子关联名
|
||||||
* @param \Closure $closure 闭包查询条件
|
* @param \Closure $closure 闭包查询条件
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
public function getRelation($closure = null)
|
public function getRelation($subRelation = '', $closure = null)
|
||||||
{
|
{
|
||||||
$foreignKey = $this->foreignKey;
|
$foreignKey = $this->foreignKey;
|
||||||
$localKey = $this->localKey;
|
$localKey = $this->localKey;
|
||||||
@@ -60,7 +61,7 @@ class BelongsToMany extends Relation
|
|||||||
// 关联查询
|
// 关联查询
|
||||||
$pk = $this->parent->getPk();
|
$pk = $this->parent->getPk();
|
||||||
$condition['pivot.' . $localKey] = $this->parent->$pk;
|
$condition['pivot.' . $localKey] = $this->parent->$pk;
|
||||||
$result = $this->belongsToManyQuery($middle, $foreignKey, $localKey, $condition)->select();
|
$result = $this->belongsToManyQuery($middle, $foreignKey, $localKey, $condition)->relation($subRelation)->select();
|
||||||
foreach ($result as $set) {
|
foreach ($result as $set) {
|
||||||
$pivot = [];
|
$pivot = [];
|
||||||
foreach ($set->getData() as $key => $val) {
|
foreach ($set->getData() as $key => $val) {
|
||||||
|
|||||||
@@ -39,15 +39,16 @@ class HasMany extends Relation
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 延迟获取关联数据
|
* 延迟获取关联数据
|
||||||
|
* @param string $subRelation 子关联名
|
||||||
* @param \Closure $closure 闭包查询条件
|
* @param \Closure $closure 闭包查询条件
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
public function getRelation($closure = null)
|
public function getRelation($subRelation = '', $closure = null)
|
||||||
{
|
{
|
||||||
if ($closure) {
|
if ($closure) {
|
||||||
call_user_func_array($closure, [ & $this->query]);
|
call_user_func_array($closure, [ & $this->query]);
|
||||||
}
|
}
|
||||||
return $this->select();
|
return $this->relation($subRelation)->select();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -49,15 +49,16 @@ class HasManyThrough extends Relation
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 延迟获取关联数据
|
* 延迟获取关联数据
|
||||||
|
* @param string $subRelation 子关联名
|
||||||
* @param \Closure $closure 闭包查询条件
|
* @param \Closure $closure 闭包查询条件
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
public function getRelation($closure = null)
|
public function getRelation($subRelation = '', $closure = null)
|
||||||
{
|
{
|
||||||
if ($closure) {
|
if ($closure) {
|
||||||
call_user_func_array($closure, [ & $this->query]);
|
call_user_func_array($closure, [ & $this->query]);
|
||||||
}
|
}
|
||||||
return $this->select();
|
return $this->relation($subRelation)->select();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -38,10 +38,11 @@ class HasOne extends OneToOne
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 延迟获取关联数据
|
* 延迟获取关联数据
|
||||||
|
* @param string $subRelation 子关联名
|
||||||
* @param \Closure $closure 闭包查询条件
|
* @param \Closure $closure 闭包查询条件
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
public function getRelation($colsure = null)
|
public function getRelation($subRelation = '', $colsure = null)
|
||||||
{
|
{
|
||||||
// 执行关联定义方法
|
// 执行关联定义方法
|
||||||
$localKey = $this->localKey;
|
$localKey = $this->localKey;
|
||||||
@@ -49,7 +50,7 @@ class HasOne extends OneToOne
|
|||||||
call_user_func_array($closure, [ & $this->query]);
|
call_user_func_array($closure, [ & $this->query]);
|
||||||
}
|
}
|
||||||
// 判断关联类型执行查询
|
// 判断关联类型执行查询
|
||||||
return $this->query->where($this->foreignKey, $this->parent->$localKey)->find();
|
return $this->query->where($this->foreignKey, $this->parent->$localKey)->relation($subRelation)->find();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -45,15 +45,16 @@ class MorphMany extends Relation
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 延迟获取关联数据
|
* 延迟获取关联数据
|
||||||
|
* @param string $subRelation 子关联名
|
||||||
* @param \Closure $closure 闭包查询条件
|
* @param \Closure $closure 闭包查询条件
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
public function getRelation($closure = null)
|
public function getRelation($subRelation = '', $closure = null)
|
||||||
{
|
{
|
||||||
if ($closure) {
|
if ($closure) {
|
||||||
call_user_func_array($closure, [ & $this->query]);
|
call_user_func_array($closure, [ & $this->query]);
|
||||||
}
|
}
|
||||||
return $this->select();
|
return $this->relation($subRelation)->select();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -39,10 +39,11 @@ class MorphTo extends Relation
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 延迟获取关联数据
|
* 延迟获取关联数据
|
||||||
|
* @param string $subRelation 子关联名
|
||||||
* @param \Closure $closure 闭包查询条件
|
* @param \Closure $closure 闭包查询条件
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
public function getRelation($closure = null)
|
public function getRelation($subRelation = '', $closure = null)
|
||||||
{
|
{
|
||||||
$morphKey = $this->morphKey;
|
$morphKey = $this->morphKey;
|
||||||
$morphType = $this->morphType;
|
$morphType = $this->morphType;
|
||||||
@@ -50,7 +51,7 @@ class MorphTo extends Relation
|
|||||||
$model = $this->parseModel($this->parent->$morphType);
|
$model = $this->parseModel($this->parent->$morphType);
|
||||||
// 主键数据
|
// 主键数据
|
||||||
$pk = $this->parent->$morphKey;
|
$pk = $this->parent->$morphKey;
|
||||||
return (new $model)->find($pk);
|
return (new $model)->relation($subRelation)->find($pk);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user