mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-03-04 06:19:36 +00:00
Update PartSearchFilter.php
This commit is contained in:
parent
0d80ac024c
commit
1d6b63405b
1 changed files with 18 additions and 20 deletions
|
|
@ -139,28 +139,23 @@ class PartSearchFilter implements FilterInterface
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expressions = new array();
|
||||||
|
params = new array();
|
||||||
|
|
||||||
//Use equal expression to just search for exact numeric matches
|
//Use equal expression to just search for exact numeric matches
|
||||||
if ($search_dbId) {
|
if ($search_dbId) {
|
||||||
$expressions[] = $queryBuilder->expr()->eq('part.id', ':id_exact');
|
$expressions[] = $queryBuilder->expr()->eq('part.id', ':id_exact');
|
||||||
$queryBuilder->setParameter('id_exact', (int) $this->keyword,
|
params[] = new \Doctrine\ORM\Query\Parameter('id_exact', (int) $this->keyword,
|
||||||
ParameterType::INTEGER);
|
ParameterType::INTEGER);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
else if($this->regex) {
|
||||||
if($this->regex) {
|
|
||||||
//Convert the fields to search to a list of expressions
|
//Convert the fields to search to a list of expressions
|
||||||
$expressions = array_map(function (string $field): string {
|
$expressions = array_map(function (string $field): string {
|
||||||
return sprintf("REGEXP(%s, :search_query) = TRUE", $field);
|
return sprintf("REGEXP(%s, :search_query) = TRUE", $field);
|
||||||
}, $fields_to_search);
|
}, $fields_to_search);
|
||||||
|
|
||||||
//Add Or concatenation of the expressions to our query
|
|
||||||
$queryBuilder->andWhere(
|
|
||||||
$queryBuilder->expr()->orX(...$expressions)
|
|
||||||
);
|
|
||||||
|
|
||||||
//For regex, we pass the query as is, save html special chars
|
//For regex, we pass the query as is, save html special chars
|
||||||
$queryBuilder->setParameter('search_query', $this->keyword);
|
params[] = new \Doctrine\ORM\Query\Parameter('search_query', $this->keyword);
|
||||||
return;
|
|
||||||
} else {
|
} else {
|
||||||
//Escape % and _ characters in the keyword
|
//Escape % and _ characters in the keyword
|
||||||
$this->keyword = str_replace(['%', '_'], ['\%', '\_'], $this->keyword);
|
$this->keyword = str_replace(['%', '_'], ['\%', '\_'], $this->keyword);
|
||||||
|
|
@ -169,7 +164,7 @@ class PartSearchFilter implements FilterInterface
|
||||||
$tokens = explode(' ', $this->keyword, 5);
|
$tokens = explode(' ', $this->keyword, 5);
|
||||||
|
|
||||||
//Perform search of every single token in every selected field
|
//Perform search of every single token in every selected field
|
||||||
//AND-combine the results (all tokens must be present in any result, but the order does not matter)
|
//AND-combine the results (all tokens must be present in any of the results)
|
||||||
for ($i = 0; $i < sizeof($tokens); $i++) {
|
for ($i = 0; $i < sizeof($tokens); $i++) {
|
||||||
$this->it = $i;
|
$this->it = $i;
|
||||||
$tokens[$i] = trim($tokens[$i]);
|
$tokens[$i] = trim($tokens[$i]);
|
||||||
|
|
@ -184,15 +179,18 @@ class PartSearchFilter implements FilterInterface
|
||||||
}, $fields_to_search);
|
}, $fields_to_search);
|
||||||
|
|
||||||
//Aggregate the parameters for consolidated commission
|
//Aggregate the parameters for consolidated commission
|
||||||
$params[] = new \Doctrine\ORM\Query\Parameter('search_query' . $i, '%' . $tokens[$i] . '%');
|
$params[] = new \Doctrine\ORM\Query\Parameter('search_query' . $i,
|
||||||
|
'%' . $tokens[$i] . '%');
|
||||||
//Add Or concatenation of the expressions to our query
|
|
||||||
$queryBuilder->andWhere(
|
|
||||||
$queryBuilder->expr()->orX(...$expressions)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
$queryBuilder->setParameters(new ArrayCollection($params));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Add Or concatenation of the expressions to our query
|
||||||
|
$queryBuilder->andWhere(
|
||||||
|
$queryBuilder->expr()->orX(...$expressions)
|
||||||
|
);
|
||||||
|
$queryBuilder->setParameters(
|
||||||
|
new \Doctrine\Common\Collections\ArrayCollection($params)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getKeyword(): string
|
public function getKeyword(): string
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue