Skip to content

Commit

Permalink
Fixed lightswitch query params when the default value was enabled
Browse files Browse the repository at this point in the history
Also Db::parseParam() no longer assumes that a null DB value means `false`

Fixes #5866
  • Loading branch information
brandonkelly committed Apr 1, 2020
1 parent 731d3dc commit 73e4816
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

### Changed
- Ajax requests sent with `Craft.sendActionRequest()` now have an `X-Requested-With: XMLHttpRequest` header. ([#5868](https://github.com/craftcms/cms/issues/5868))
- `craft\helpers\Db::parseParam()` no longer assumes that `null` values within boolean columns should equate to `false`.

### Fixed
- Fixed a bug where Lightswitch element query params were filtering out entries that hadn’t been saved since the Lightswitch field was added, if the field’s default value was enabled. ([#5866](https://github.com/craftcms/cms/issues/5866))

## 3.4.12 - 2020-03-31

Expand Down
10 changes: 9 additions & 1 deletion src/fields/Lightswitch.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,17 @@ public function modifyElementsQuery(ElementQueryInterface $query, $value)
return null;
}

$value = (bool)$value;
$column = 'content.' . Craft::$app->getContent()->fieldColumnPrefix . $this->handle;
$condition = Db::parseParam($column, $value, '=', false, Schema::TYPE_BOOLEAN);

// If the value matches the default value, allow null values as well
if ($value == $this->default) {
$condition = ['or', $condition, [$column => null]];
}

/** @var ElementQuery $query */
$query->subQuery->andWhere(Db::parseParam($column, $value, '=', false, Schema::TYPE_BOOLEAN));
$query->subQuery->andWhere($condition);
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/helpers/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ public static function parseParam(string $column, $value, string $defaultOperato
if ($operator === '!=') {
$val = !$val;
}
$condition[] = $val ? [$column => true] : ['or', ['not', [$column => true]], [$column => null]];
$condition[] = [$column => (bool)$val];
continue;
}

Expand Down
10 changes: 5 additions & 5 deletions tests/unit/helpers/dbhelper/DbHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,23 +361,23 @@ public function parseParamDataProvider(): array
'foo', 'not :empty:', '=', false, Schema::TYPE_BOOLEAN,
],
[
['or', ['not', ['foo' => true]], ['foo' => null]],
['foo' => false],
'foo', false, '=', false, Schema::TYPE_BOOLEAN,
],
[
['or', ['not', ['foo' => true]], ['foo' => null]],
['foo' => false],
'foo', 0, '=', false, Schema::TYPE_BOOLEAN,
],
[
['or', ['not', ['foo' => true]], ['foo' => null]],
['foo' => false],
'foo', '0', '=', false, Schema::TYPE_BOOLEAN,
],
[
['or', ['not', ['foo' => true]], ['foo' => null]],
['foo' => false],
'foo', 'not 1', '=', false, Schema::TYPE_BOOLEAN,
],
[
['or', ['not', ['foo' => true]], ['foo' => null]],
['foo' => false],
'foo', ':empty:', '=', false, Schema::TYPE_BOOLEAN,
],
];
Expand Down

0 comments on commit 73e4816

Please sign in to comment.