改进Model类的has和hasWhere方法

This commit is contained in:
thinkphp
2017-02-09 14:26:36 +08:00
parent 2639db4f9e
commit a01c43472d
8 changed files with 172 additions and 25 deletions

View File

@@ -1426,16 +1426,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/
public static function has($relation, $operator = '>=', $count = 1, $id = '*')
{
$model = new static();
$relation = $model->$relation();
if ($relation instanceof HasMany) {
if (is_array($operator) || $operator instanceof \Closure) {
return $relation->hasWhere($operator);
}
return $relation->has($operator, $count, $id);
} else {
return $relation;
$relation = (new static())->$relation();
if (is_array($operator) || $operator instanceof \Closure) {
return $relation->hasWhere($operator);
}
return $relation->has($operator, $count, $id);
}
/**
@@ -1447,13 +1442,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/
public static function hasWhere($relation, $where = [])
{
$model = new static();
$relation = $model->$relation();
if ($relation instanceof HasMany) {
return $relation->hasWhere($where);
} else {
return $relation;
}
return (new static())->$relation()->hasWhere($where);
}
/**