Fix: Searching for multiple terms on assets (#5860)

* Give advancedTextSearch all search terms at one

The additional conditions for assets had some problems, since they were joining tables for the additional attributes. The method was called once for every search term, so the join was added multiple times if the user entered multiple search terms.

* Allows search to handle multiple search terms better

The search now better handles multiple search terms, adding additional orWhere clauses, instead of duplicating all queries.

* Fixing typo
This commit is contained in:
Till Deeke
2018-07-17 02:44:31 +02:00
committed by snipe
parent 638a7b2d91
commit b5de5ac19c
3 changed files with 92 additions and 73 deletions
+26 -11
View File
@@ -621,10 +621,12 @@ class Asset extends Depreciable
* Run additional, advanced searches.
*
* @param Illuminate\Database\Eloquent\Builder $query
* @param string $term The search term
* @param array $terms The search terms
* @return Illuminate\Database\Eloquent\Builder
*/
public function advancedTextSearch(Builder $query, string $term) {
public function advancedTextSearch(Builder $query, array $terms) {
/**
* Assigned user
*/
@@ -632,31 +634,44 @@ class Asset extends Depreciable
$leftJoin->on("assets_users.id", "=", "assets.assigned_to")
->where("assets.assigned_type", "=", User::class);
});
$query = $query
->orWhere('assets_users.first_name', 'LIKE', '%'.$term.'%')
->orWhere('assets_users.last_name', 'LIKE', '%'.$term.'%')
->orWhere('assets_users.username', 'LIKE', '%'.$term.'%')
->orWhereRaw('CONCAT('.DB::getTablePrefix().'assets_users.first_name," ",'.DB::getTablePrefix().'assets_users.last_name) LIKE ?', ["%$term%", "%$term%"]);
foreach($terms as $term) {
$query = $query
->orWhere('assets_users.first_name', 'LIKE', '%'.$term.'%')
->orWhere('assets_users.last_name', 'LIKE', '%'.$term.'%')
->orWhere('assets_users.username', 'LIKE', '%'.$term.'%')
->orWhereRaw('CONCAT('.DB::getTablePrefix().'assets_users.first_name," ",'.DB::getTablePrefix().'assets_users.last_name) LIKE ?', ["%$term%", "%$term%"]);
}
/**
* Assigned location
*/
*/
$query = $query->leftJoin('locations as assets_locations',function ($leftJoin) {
$leftJoin->on("assets_locations.id","=","assets.assigned_to")
->where("assets.assigned_type","=",Location::class);
});
$query = $query->orWhere('assets_locations.name', 'LIKE', '%'.$term.'%');
foreach($terms as $term) {
$query = $query->orWhere('assets_locations.name', 'LIKE', '%'.$term.'%');
}
/**
* Assigned assets
*/
*/
$query = $query->leftJoin('assets as assigned_assets',function ($leftJoin) {
$leftJoin->on('assigned_assets.id', '=', 'assets.assigned_to')
->where('assets.assigned_type', '=', Asset::class);
});
$query = $query->orWhere('assigned_assets.name', 'LIKE', '%'.$term.'%');
foreach($terms as $term) {
$query = $query->orWhere('assigned_assets.name', 'LIKE', '%'.$term.'%');
}
return $query;
}