Skip to content

Commit

Permalink
:empty: & :notempty: Matrix field query params must account for soft-…
Browse files Browse the repository at this point in the history
…deleted blocks

Resolves #4161
  • Loading branch information
brandonkelly authored and angrybrad committed May 6, 2019
1 parent fd93556 commit dc12ae8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Fixed an error where re-saving a site would reset its sorting order. ([#4147](https://github.com/craftcms/cms/issues/4147))
- Fixed a SQL error that could occur when updating to Craft 3.1. ([#3663](https://github.com/craftcms/cms/issues/3663))
- Fixed an error that occurred when an SVG with `/` characters in its `id` attributes was passed to the `svg()` Twig function. ([#4155](https://github.com/craftcms/cms/issues/4155))
- Fixed a bug where passing `:empty:` or `:notempty:` to a Matrix field param on an element query could return incorrect results for fields that had soft-deleted blocks. ([#4161](https://github.com/craftcms/cms/issues/4161))
- Fixed a bug where Craft wasn’t returning a `1` exit code for console requests if the server was running under PHP 7. ([#4153](https://github.com/craftcms/cms/issues/4153))
- Fixed a bug where `craft\services\Elements::duplicateElements()` would only ignore non-safe attributes passed to the `$newAttributes` argument.
- Fixed a bug where `craft\elements\db\ElementQuery::exists()` and `offsetExists()` were ignoring cached query results.
Expand Down
21 changes: 15 additions & 6 deletions src/fields/Matrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,13 +437,22 @@ public function modifyElementsQuery(ElementQueryInterface $query, $value)
}

if ($value === ':notempty:' || $value === ':empty:') {
$alias = 'matrixblocks_' . $this->handle;
$operator = ($value === ':notempty:' ? '!=' : '=');
$ns = $this->handle . '_' . StringHelper::randomString(5);
$condition = ['exists', (new Query())
->from(TableName::MATRIXBLOCKS . " matrixblocks_$ns")
->innerJoin(TableName::ELEMENTS . " elements_$ns", "[[elements_$ns.id]] = [[matrixblocks_$ns.id]]")
->where("[[matrixblocks_$ns.ownerId]] = [[elements.id]]")
->andWhere([
"matrixblocks_$ns.fieldId" => $this->id,
"elements_$ns.dateDeleted" => null,
])
];

$query->subQuery->andWhere(
"(select count([[{$alias}.id]]) from {{%matrixblocks}} {{{$alias}}} where [[{$alias}.ownerId]] = [[elements.id]] and [[{$alias}.fieldId]] = :fieldId) {$operator} 0",
[':fieldId' => $this->id]
);
if ($value === ':notempty:') {
$query->subQuery->andWhere($condition);
} else {
$query->subQuery->andWhere(['not', $condition]);
}
} else if ($value !== null) {
return false;
}
Expand Down

0 comments on commit dc12ae8

Please sign in to comment.