mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-06 07:02:47 +08:00
改进Model类的 has和hasWhere方法
This commit is contained in:
@@ -601,13 +601,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public static function has($relation, $operator = '>=', $count = 1, $id = '*')
|
public static function has($relation, $operator = '>=', $count = 1, $id = '*')
|
||||||
{
|
{
|
||||||
$class = new static();
|
$class = new static();
|
||||||
$model = $class->$relation();
|
$model = $class->$relation();
|
||||||
$info = $class->getRelationInfo();
|
$info = $class->getRelationInfo();
|
||||||
$result = $model->group($info['foreignKey'])
|
$table = $info['model']::getTable();
|
||||||
->having('count(' . $id . ')' . $operator . $count)
|
return self::db()->alias('a')->join($table . ' b', 'a.' . $info['localKey'] . '=b.' . $info['foreignKey'])->group('b.' . $info['foreignKey'])
|
||||||
->column($info['foreignKey']);
|
->having('count(' . $id . ')' . $operator . $count);
|
||||||
return self::db()->where($info['localKey'], 'in', $result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -619,13 +618,19 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public static function hasWhere($relation, $where = [])
|
public static function hasWhere($relation, $where = [])
|
||||||
{
|
{
|
||||||
$class = new static();
|
$class = new static();
|
||||||
$model = $class->$relation();
|
$model = $class->$relation();
|
||||||
$info = $class->getRelationInfo();
|
$info = $class->getRelationInfo();
|
||||||
$result = $model->group($info['foreignKey'])
|
$table = $info['model']::getTable();
|
||||||
->where($where)
|
if (is_array($where)) {
|
||||||
->column($info['foreignKey']);
|
foreach ($where as $key => $val) {
|
||||||
return self::db()->where($info['localKey'], 'in', $result);
|
if (false === strpos($key, '.')) {
|
||||||
|
$where['b.' . $key] = $val;
|
||||||
|
unset($where[$key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return self::db()->alias('a')->field('a.*')->join($table . ' b', 'a.' . $info['localKey'] . '=b.' . $info['foreignKey'])->where($where);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user