Skip to content
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

Make multivalue filter with content fields #1733

Merged
merged 2 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions public/theme/skeleton/custom/setcontent_1.twig
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@
</ul>
<section>

{# @todo: This doesn't work yet
<section id="eight">
<h1>Eight</h1>
{% setcontent entries = "entries,blocks,showcases" where {'title': '%voluptat% || %porro%' } printquery %}
Expand All @@ -125,8 +124,7 @@
{% set last = entry.id %}
{% endfor %}
</ul>
<section>
#}
</section>

<section id="nine">
<h1>Nine</h1>
Expand Down
29 changes: 19 additions & 10 deletions src/Storage/SelectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,7 @@ public function getWhereExpression(): ?Base
$this->fieldJoins = [];

foreach ($this->filters as $filter) {
if ($filter->getExpressionObject() instanceof \Doctrine\ORM\Query\Expr\Orx) {
// todo: `|||` and `bolt_field` integration.
$expr = $expr->add($filter->getExpression());
} elseif (in_array($filter->getKey(), $this->coreFields, true)) {
if (in_array($filter->getKey(), $this->coreFields, true)) {
// For fields like `id`, `createdAt` and `status`, which are in the main `bolt_content` table
$expr = $expr->add($filter->getExpression());
} elseif (in_array($filter->getKey(), $this->referenceFields, true)) {
Expand Down Expand Up @@ -375,7 +372,14 @@ public function doFieldJoins(): void
$newLeftExpression = JsonHelper::wrapJsonFunction('LOWER(' . $translationsAlias . '.value)', null, $em->getConnection());

$where = $filter->getExpression();
$where = str_replace($originalLeftExpression, $newLeftExpression, $where);
$exactWhere = str_replace($originalLeftExpression, $newLeftExpression, $where);

// add containsWhere to allow searching of fields with Muiltiple JSON values (eg. Selectfield with mutiple entries).
preg_match_all('/\:([a-z]*_[0-9]+)/', $where, $matches);
$clauses = array_map(function ($m) use ($translationsAlias) {
return 'LOWER(' . $translationsAlias . '.value) LIKE :' . $m . '_JSON';
}, $matches[1]);
$containsWhere = implode(' OR ', $clauses);

// Create the subselect to filter on the value of fields
$innerQuery = $em
Expand All @@ -384,9 +388,11 @@ public function doFieldJoins(): void
->from(\Bolt\Entity\Content::class, $contentAlias)
->innerJoin($contentAlias . '.fields', $fieldsAlias)
->innerJoin($fieldsAlias . '.translations', $translationsAlias)
->andWhere($where)
// add orWhere to allow searching of fields with Muiltiple JSON values (eg. Selectfield with mutiple entries).
->orWhere($this->qb->expr()->like('LOWER(' . $translationsAlias . '.value)', ':' . $key . '_1_JSON'));
->andWhere($exactWhere);

if (! empty($containsWhere)) {
$innerQuery->OrWhere($containsWhere);
}

// Unless the field to which the 'where' applies is `anyColumn`, we
// Make certain it's narrowed down to that fieldname
Expand All @@ -409,8 +415,11 @@ public function doFieldJoins(): void
foreach ($filter->getParameters() as $key => $value) {
$value = JsonHelper::wrapJsonFunction(null, $value, $em->getConnection());
$this->qb->setParameter($key, $value);
//remove % if present. Reformat JSON to work with both json enabled platforms and non json platforms.
$this->qb->setParameter($key . '_JSON', '%"' . str_replace(['["', '"]', '%'], '', $value) . '"%');

if (! empty($containsWhere)) {
//remove % if present. Reformat JSON to work with both json enabled platforms and non json platforms.
$this->qb->setParameter($key . '_JSON', '%"' . str_replace(['["', '"]', '%'], '', $value) . '"%');
}
}
}
}
Expand Down