mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-06 23:02:48 +08:00
Query类的relation方法和with方法一样支持使用闭包进行自定义关联查询
This commit is contained in:
@@ -1281,8 +1281,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
$relations = explode(',', $relations);
|
$relations = explode(',', $relations);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($relations as $relation) {
|
foreach ($relations as $key => $relation) {
|
||||||
$this->data[$relation] = $this->$relation()->getRelation();
|
$closure = null;
|
||||||
|
if ($relation instanceof \Closure) {
|
||||||
|
// 支持闭包查询过滤关联条件
|
||||||
|
$closure = $relation;
|
||||||
|
$relation = $key;
|
||||||
|
}
|
||||||
|
$method = Loader::parseName($relation, 1, false);
|
||||||
|
$this->data[$relation] = $this->$method()->getRelation($closure);
|
||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,12 +38,16 @@ class BelongsTo extends OneToOne
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 延迟获取关联数据
|
* 延迟获取关联数据
|
||||||
|
* @param \Closure $closure 闭包查询条件
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
public function getRelation()
|
public function getRelation($closure = null)
|
||||||
{
|
{
|
||||||
$foreignKey = $this->foreignKey;
|
$foreignKey = $this->foreignKey;
|
||||||
$localKey = $this->localKey;
|
$localKey = $this->localKey;
|
||||||
|
if ($closure) {
|
||||||
|
call_user_func_array($closure, [ & $this->query]);
|
||||||
|
}
|
||||||
return $this->query->where($localKey, $this->parent->$foreignKey)->find();
|
return $this->query->where($localKey, $this->parent->$foreignKey)->find();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -46,13 +46,17 @@ class BelongsToMany extends Relation
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 延迟获取关联数据
|
* 延迟获取关联数据
|
||||||
|
* @param \Closure $closure 闭包查询条件
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
public function getRelation()
|
public function getRelation($closure = null)
|
||||||
{
|
{
|
||||||
$foreignKey = $this->foreignKey;
|
$foreignKey = $this->foreignKey;
|
||||||
$localKey = $this->localKey;
|
$localKey = $this->localKey;
|
||||||
$middle = $this->middle;
|
$middle = $this->middle;
|
||||||
|
if ($closure) {
|
||||||
|
call_user_func_array($closure, [ & $this->query]);
|
||||||
|
}
|
||||||
// 关联查询
|
// 关联查询
|
||||||
$pk = $this->parent->getPk();
|
$pk = $this->parent->getPk();
|
||||||
$condition['pivot.' . $localKey] = $this->parent->$pk;
|
$condition['pivot.' . $localKey] = $this->parent->$pk;
|
||||||
|
|||||||
@@ -39,10 +39,14 @@ class HasMany extends Relation
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 延迟获取关联数据
|
* 延迟获取关联数据
|
||||||
|
* @param \Closure $closure 闭包查询条件
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
public function getRelation()
|
public function getRelation($closure = null)
|
||||||
{
|
{
|
||||||
|
if ($closure) {
|
||||||
|
call_user_func_array($closure, [ & $this->query]);
|
||||||
|
}
|
||||||
return $this->select();
|
return $this->select();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,10 +49,14 @@ class HasManyThrough extends Relation
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 延迟获取关联数据
|
* 延迟获取关联数据
|
||||||
|
* @param \Closure $closure 闭包查询条件
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
public function getRelation()
|
public function getRelation($closure = null)
|
||||||
{
|
{
|
||||||
|
if ($closure) {
|
||||||
|
call_user_func_array($closure, [ & $this->query]);
|
||||||
|
}
|
||||||
return $this->select();
|
return $this->select();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,13 +38,16 @@ class HasOne extends OneToOne
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 延迟获取关联数据
|
* 延迟获取关联数据
|
||||||
|
* @param \Closure $closure 闭包查询条件
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
public function getRelation()
|
public function getRelation($colsure = null)
|
||||||
{
|
{
|
||||||
// 执行关联定义方法
|
// 执行关联定义方法
|
||||||
$localKey = $this->localKey;
|
$localKey = $this->localKey;
|
||||||
|
if ($closure) {
|
||||||
|
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)->find();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,10 +45,14 @@ class MorphMany extends Relation
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 延迟获取关联数据
|
* 延迟获取关联数据
|
||||||
|
* @param \Closure $closure 闭包查询条件
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
public function getRelation()
|
public function getRelation($closure = null)
|
||||||
{
|
{
|
||||||
|
if ($closure) {
|
||||||
|
call_user_func_array($closure, [ & $this->query]);
|
||||||
|
}
|
||||||
return $this->select();
|
return $this->select();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,9 +39,10 @@ class MorphTo extends Relation
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 延迟获取关联数据
|
* 延迟获取关联数据
|
||||||
|
* @param \Closure $closure 闭包查询条件
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
public function getRelation()
|
public function getRelation($closure = null)
|
||||||
{
|
{
|
||||||
$morphKey = $this->morphKey;
|
$morphKey = $this->morphKey;
|
||||||
$morphType = $this->morphType;
|
$morphType = $this->morphType;
|
||||||
|
|||||||
Reference in New Issue
Block a user