-
Notifications
You must be signed in to change notification settings - Fork 15
#65: optimize search #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
src/Traits/SearchTrait.php
Outdated
'from', | ||
'to' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are these reserved filters?
src/Traits/SearchTrait.php
Outdated
|
||
if (!empty($this->filter)) { | ||
foreach ($this->filter as $field => $values) { | ||
if (!in_array($field, $this->reservedFilters) && is_array($values) || !in_array($field, $this->reservedFilters) && is_string($values)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (!in_array($field, $this->reservedFilters) && is_array($values) || !in_array($field, $this->reservedFilters) && is_string($values)) { | |
if (!in_array($field, $this->reservedFilters) && (is_array($values) || is_string($values)) { |
src/Traits/SearchTrait.php
Outdated
protected function filter($postfix, $where, $field, $values) | ||
{ | ||
$fieldWithoutPostfix = Str::replace($postfix, '', $field); | ||
|
||
if (!is_array($values) || !Arr::isAssoc($values)) { | ||
$this->query->{$where}($fieldWithoutPostfix, Arr::wrap($values)); | ||
} else { | ||
foreach ($values as $relationFiled => $value) { | ||
$fieldWithRelation = $fieldWithoutPostfix . '.' . $relationFiled; | ||
} | ||
|
||
$this->applyWhereCallback($this->query, $fieldWithRelation, function (&$query, $conditionField) use ($value, $where) { | ||
$query->{$where}($conditionField, Arr::wrap($value)); | ||
}); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
something strange happens here, let's implement simple logic
public function searchQuery(array $filter): self
{
// some initial logic
foreach($filters as $fieldName => $value) {
if ($isNotReserverFilter || Str::endWith($field, 'in_list')) {
$field = Str::replace($field, 'in_list', '');
$this->filterBy($field, $value);
} elseif (Str::endWith($field, 'not_in_list')) {
$this->query->whereNotIn($field, $value);
} elseif (Str::endWith($field, '_from')) {
$field = Str::replace($field, '_from', '');
$this->filterFrom($field, $value);
} elseif (Str::endWith($field, '_to')) {
$field = Str::replace($field, '_to', '');
$this->filterTo($field, $value);
}
}
}
src/Traits/SearchTrait.php
Outdated
|
||
$this->filter = $filter; | ||
|
||
if (!empty($filter)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's set the default value for the $filter
argument to []
it will allow to skip thid check for empty
src/Traits/SearchTrait.php
Outdated
foreach($filter as $fieldName => $value) { | ||
$isNotReservedFilter = (!in_array($fieldName, $this->reservedFilters)); | ||
|
||
$isValidValue = (is_array($value) || is_string($value) || is_integer($value) || is_double($value) || is_bool($value)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please remove as we discussed
src/Traits/SearchTrait.php
Outdated
$this->filter[$field] = $value; | ||
$this->filterFrom($field, true, $field); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$this->filter[$field] = $value; | |
$this->filterFrom($field, true, $field); | |
$this->filterFrom($field, false, $fieldName); |
src/Traits/SearchTrait.php
Outdated
$this->filterTo($field, true, $field); | ||
} elseif ($isNotReservedFilter || Str::endsWith($fieldName, '_in_list')) { | ||
$field = Str::replace('_in_list', '', $fieldName); | ||
$this->filter[$field] = $value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$this->filter[$field] = $value; |
src/Traits/SearchTrait.php
Outdated
return $this; | ||
} | ||
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No description provided.