mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
改进Model类的has和hasWhere方法
This commit is contained in:
@@ -1422,15 +1422,20 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
* @param mixed $operator 比较操作符
|
* @param mixed $operator 比较操作符
|
||||||
* @param integer $count 个数
|
* @param integer $count 个数
|
||||||
* @param string $id 关联表的统计字段
|
* @param string $id 关联表的统计字段
|
||||||
* @return Model
|
* @return Relation|Query
|
||||||
*/
|
*/
|
||||||
public static function has($relation, $operator = '>=', $count = 1, $id = '*')
|
public static function has($relation, $operator = '>=', $count = 1, $id = '*')
|
||||||
{
|
{
|
||||||
$model = new static();
|
$model = new static();
|
||||||
|
$relation = $model->$relation();
|
||||||
|
if ($relation instanceof HasMany) {
|
||||||
if (is_array($operator) || $operator instanceof \Closure) {
|
if (is_array($operator) || $operator instanceof \Closure) {
|
||||||
return $model->$relation()->hasWhere($operator);
|
return $relation->hasWhere($operator);
|
||||||
|
}
|
||||||
|
return $relation->has($operator, $count, $id);
|
||||||
|
} else {
|
||||||
|
return $relation;
|
||||||
}
|
}
|
||||||
return $model->$relation()->has($operator, $count, $id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1438,12 +1443,17 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
* @access public
|
* @access public
|
||||||
* @param string $relation 关联方法名
|
* @param string $relation 关联方法名
|
||||||
* @param mixed $where 查询条件(数组或者闭包)
|
* @param mixed $where 查询条件(数组或者闭包)
|
||||||
* @return Model
|
* @return Relation|Query
|
||||||
*/
|
*/
|
||||||
public static function hasWhere($relation, $where = [])
|
public static function hasWhere($relation, $where = [])
|
||||||
{
|
{
|
||||||
$model = new static();
|
$model = new static();
|
||||||
|
$relation = $model->$relation();
|
||||||
|
if ($relation instanceof HasMany) {
|
||||||
return $model->$relation()->hasWhere($where);
|
return $model->$relation()->hasWhere($where);
|
||||||
|
} else {
|
||||||
|
return $relation;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user