Skip to content

Commit

Permalink
Improve filtering.Dynamically construct where clause using QueryExpre…
Browse files Browse the repository at this point in the history
…ssions.
  • Loading branch information
Ioannis committed Mar 12, 2024
1 parent 313d230 commit c97e49b
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 151 deletions.
35 changes: 13 additions & 22 deletions app/src/Controller/StandardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

namespace App\Controller;

use Cake\Database\Expression\QueryExpression;
use InvalidArgumentException;
use \Cake\Http\Exception\BadRequestException;
use \App\Lib\Enum\ProvisioningContextEnum;
Expand Down Expand Up @@ -564,22 +565,22 @@ public function index() {
$table = $this->$modelsName;
// $tableName = models
$tableName = $table->getTable();

$query = null;

// PrimaryLinkTrait
$link = $this->getPrimaryLink(true);

// AutoViewVarsTrait
$this->populateAutoViewVars();

// Initialize the Query Object
$query = $table->find($this->paginate['finder'] ?? 'all');
// Get a pointer to my expressions list
$newexp = $query->newExpr();

if(!empty($link->attr)) {
// If a link attribute is defined but no value is provided, then query
// where the link attribute is NULL
// "all" is the default finder. But since we are utilizing the paginator here, we will check the configuration
// for any custom finder.
$query = $query->where([$table->getAlias().'.'.$link->attr => $link->value]);
$newexp->add([$table->getAlias().'.'.$link->attr => $link->value]);
}

// QueryModificationTrait
Expand All @@ -590,43 +591,33 @@ public function index() {

// SearchFilterTrait
if(method_exists($table, 'getSearchableAttributes')) {
$searchableAttributes = $table->getSearchableAttributes($this->name, $this->viewBuilder()->getVar('vv_tz'));
$searchableAttributes = $table->getSearchableAttributes($this->name,
$this->viewBuilder()->getVar('vv_tz'));

if(!empty($searchableAttributes)) {
// Here we iterate over the attributes, and we add a new where clause for each one
foreach($searchableAttributes as $attribute => $options) {
// Add the Join Clauses
$query = $table->addJoins($query, $attribute);
$query = $table->addJoins($query, $attribute, $this->request);

// Construct and apply the where Clause
if(!empty($this->request->getQuery($attribute))) {
$query = $table->whereFilter($query, $attribute, $this->request->getQuery($attribute));
$newexp = $table->expressionsConstructor($query, $newexp, $attribute, $this->request->getQuery($attribute));
} elseif (!empty($this->request->getQuery($attribute . '_starts_at'))
|| !empty($this->request->getQuery($attribute . '_ends_at'))) {
$search_date = [];
// We allow empty for dates since we might refer to infinity (from whenever or to always)
$search_date[] = $this->request->getQuery($attribute . '_starts_at') ?? '';
$search_date[] = $this->request->getQuery($attribute . '_ends_at') ?? '';
$query = $table->whereFilter($query, $attribute, $search_date);
$newexp = $table->expressionsConstructor($query, $newexp, $attribute, $search_date);
}
}

$this->set('vv_searchable_attributes', $searchableAttributes);
}
}

// Filter on requested filter, if requested
// QueryModificationTrait
if(method_exists($table, "getIndexFilter")) {
$filter = $table->getIndexFilter();

if(is_callable($filter)) {
$query->where($filter($this->request));
} else {
$query->where($table->getIndexFilter());
}
}


$query = $query->where($newexp);
$resultSet = $this->paginate($query);

$this->set($tableName, $resultSet);
Expand Down
11 changes: 0 additions & 11 deletions app/src/Lib/Traits/QueryModificationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,6 @@ public function getIndexContains() {
return $this->indexContains;
}

/**
* Obtain the index filter for this model.
*
* @since COmanage Registry v5.0.0
* @return array|Closure Array of index filters or closure that generates an array
*/

public function getIndexFilter(): array|\Closure|null {
return $this->indexFilter;
}

/**
* Obtain the set of associated models to save during a patch.
*
Expand Down
Loading

0 comments on commit c97e49b

Please sign in to comment.