新增query方法
This commit is contained in:
parent
cffcf11192
commit
2cc2ecae40
@ -11,15 +11,21 @@ class CatchQuery extends Query
|
||||
* @param string $model
|
||||
* @param string $joinField
|
||||
* @param string $currentJoinField
|
||||
* @param array $field
|
||||
* @param string $type
|
||||
* @param array $bind
|
||||
* @return CatchQuery
|
||||
*/
|
||||
public function catchJoin(string $model, string $joinField, string $currentJoinField, string $type = 'INNER', array $bind = [])
|
||||
public function catchJoin(string $model, string $joinField, string $currentJoinField, array $field = [], string $type = 'INNER', array $bind = []): CatchQuery
|
||||
{
|
||||
$table = app($model)->getTable();
|
||||
|
||||
return $this->join($table, sprintf('%s.%s=%s.%s', $table, $joinField, $this->getTable(), $currentJoinField), $type, $bind);
|
||||
// 合并字段
|
||||
$this->options['field'] = array_merge($this->options['field'], array_map(function ($value) use ($table) {
|
||||
return $table . '.' . $value;
|
||||
}, $field));
|
||||
|
||||
return $this->join($table, sprintf('%s.%s=%s.%s', $table, $joinField, $this->getAlias(), $currentJoinField), $type, $bind);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -28,12 +34,13 @@ class CatchQuery extends Query
|
||||
* @param string $model
|
||||
* @param string $joinField
|
||||
* @param string $currentJoinField
|
||||
* @param array $field
|
||||
* @param array $bind
|
||||
* @return CatchQuery
|
||||
*/
|
||||
public function catchLeftJoin(string $model, string $joinField, string $currentJoinField, array $bind = [])
|
||||
public function catchLeftJoin(string $model, string $joinField, string $currentJoinField, array $field = [], array $bind = []): CatchQuery
|
||||
{
|
||||
return $this->catchJoin($model, $joinField, $currentJoinField, 'LEFT', $bind);
|
||||
return $this->catchJoin($model, $joinField, $currentJoinField, $field,'LEFT', $bind);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -42,11 +49,100 @@ class CatchQuery extends Query
|
||||
* @param string $model
|
||||
* @param string $joinField
|
||||
* @param string $currentJoinField
|
||||
* @param array $field
|
||||
* @param array $bind
|
||||
* @return CatchQuery
|
||||
*/
|
||||
public function catchRightJoin(string $model, string $joinField, string $currentJoinField, array $bind = [])
|
||||
public function catchRightJoin(string $model, string $joinField, string $currentJoinField, array $field = [], array $bind = []): CatchQuery
|
||||
{
|
||||
return $this->catchJoin($model, $joinField, $currentJoinField, 'RIGHT', $bind);
|
||||
return $this->catchJoin($model, $joinField, $currentJoinField, $field,'RIGHT', $bind);
|
||||
}
|
||||
|
||||
/**
|
||||
* rewrite
|
||||
*
|
||||
* @time 2020年01月13日
|
||||
* @param array|string $field
|
||||
* @param bool $needAlias
|
||||
* @return $this|Query
|
||||
*/
|
||||
public function withoutField($field, $needAlias = false)
|
||||
{
|
||||
if (empty($field)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (is_string($field)) {
|
||||
$field = array_map('trim', explode(',', $field));
|
||||
}
|
||||
|
||||
// 过滤软删除字段
|
||||
$field[] = $this->model->getDeleteAtField();
|
||||
|
||||
// 字段排除
|
||||
$fields = $this->getTableFields();
|
||||
$field = $fields ? array_diff($fields, $field) : $field;
|
||||
|
||||
if (isset($this->options['field'])) {
|
||||
$field = array_merge((array) $this->options['field'], $field);
|
||||
}
|
||||
|
||||
if ($needAlias) {
|
||||
$alias = $this->getAlias();
|
||||
|
||||
$this->options['field'] = array_map(function ($field) use ($alias) {
|
||||
return $alias . '.' . $field;
|
||||
}, array_unique($field));
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2020年01月13日
|
||||
* @return CatchQuery
|
||||
*/
|
||||
public function catchSearch(): CatchQuery
|
||||
{
|
||||
$params = \request()->param();
|
||||
|
||||
return $this->withSearch(array_keys($params), Utils::filterSearchParams($params));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2020年01月13日
|
||||
* @return mixed
|
||||
*/
|
||||
public function getAlias()
|
||||
{
|
||||
return isset($this->options['alias']) ? $this->options['alias'][$this->getTable()] : $this->getTable();
|
||||
}
|
||||
|
||||
/**
|
||||
* rewrite
|
||||
*
|
||||
* @time 2020年01月13日
|
||||
* @param string $field
|
||||
* @param mixed $condition
|
||||
* @param string $option
|
||||
* @param string $logic
|
||||
* @return Query
|
||||
*/
|
||||
public function whereLike(string $field, $condition, $option = 'both', string $logic = 'AND'): Query
|
||||
{
|
||||
switch ($option) {
|
||||
case 'both':
|
||||
$condition = '%' . $condition . '%';
|
||||
break;
|
||||
case 'left':
|
||||
$condition = '%' . $condition;
|
||||
break;
|
||||
default:
|
||||
$condition .= '%';
|
||||
}
|
||||
|
||||
return parent::whereLike($this->getAlias() . '.' . $field, $condition, $logic);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user