diff --git a/docs/collection-filtering.md b/docs/collection-filtering.md index 52d310a0..2ddcf350 100644 --- a/docs/collection-filtering.md +++ b/docs/collection-filtering.md @@ -13,6 +13,26 @@ $books = $orm->books->findBy([ Allowed operators are `=`, `!=`, `<=`, `<`, `>=`, `>`, and `~` (like -- see later); append it directly after the property name (without any space or other whitespace character). +The `<`, `<=`, `>`, and `>=` operators follow the same filtering semantics in `ArrayCollection` and `DbalCollection`: a comparison does not match when either the property value or the comparison value is `null`. PHP evaluation returns `false` in these cases, consistently with the exclusion of such rows by SQL `WHERE` conditions. + +```php +// Finds books printed before 2026; books with printedAt = null are excluded. +$books = $orm->books->findBy([ + 'printedAt<' => new DateTimeImmutable('2026-01-01'), +]); +``` + +To select only books with a missing or present value, use `['printedAt' => null]` or `['printedAt!=' => null]`, respectively. To include books with `null` values alongside books matching the comparison, combine the conditions with `ICollection::OR`: + +```php +// Finds books printed before 2026 or with an unknown printing date. +$books = $orm->books->findBy([ + ICollection::OR, + 'printedAt<' => new DateTimeImmutable('2026-01-01'), + 'printedAt' => null, +]); +``` + You can filter the collection by traversing relationships; use a *traversing expression*: it consists of the path delimited by `->`, i.e., the same arrow you use in PHP. ```php diff --git a/docs/collection.md b/docs/collection.md index 11630914..31e52446 100644 --- a/docs/collection.md +++ b/docs/collection.md @@ -73,6 +73,8 @@ You can easily sort the collection by an `orderBy()` method; The `orderBy()` met To change the order, use `ICollection::ASC` or `ICollection::DESC` constants. If the sorting property (or property expression) may contain a null value, use more specific sorting constants: `ICollection::ASC_NULLS_LAST`, `ICollection::ASC_NULLS_FIRST`, `ICollection::DESC_NULLS_LAST`, or `ICollection::DESC_NULLS_FIRST`. +With plain `ASC` and `DESC`, null placement depends on the storage or collection implementation. Use the explicit null-placement constants for consistent ordering across implementations. + ```php $orm->books->findAll()->orderBy('title'); // ORDER BY title ASC $orm->books->findAll()->orderBy('title', ICollection::DESC); // ORDER BY title DESC diff --git a/docs/migrate_6.0.md b/docs/migrate_6.0.md index 4332d962..963bad25 100644 --- a/docs/migrate_6.0.md +++ b/docs/migrate_6.0.md @@ -2,6 +2,8 @@ ### BC Breaks +- **Relational comparisons with `null` in `ArrayCollection` follow database filtering semantics** - the `<`, `<=`, `>`, and `>=` operators now return `false` when either operand is `null`. Previously, PHP comparisons or property comparators could include entities whose corresponding SQL comparison would exclude them. This affects filtering over nullable properties and comparisons against `null`. + - **`IProperty::setRawValue()` must not validate the value** - the method now only stores the passed raw value; it must neither convert it to the runtime representation nor validate it. Both the conversion and any validation are deferred to read time (`getInjectedValue()` / `getRawValue()`). This fixes premature initialization of non-nullable wrapped properties — reading such a property via `IEntity::getProperty()`, or creating an entity through `EntityCreator`, no longer triggers validation before the actual value is set. diff --git a/src/Collection/Functions/CompareGreaterThanEqualsFunction.php b/src/Collection/Functions/CompareGreaterThanEqualsFunction.php index d8624160..7ead1dfe 100644 --- a/src/Collection/Functions/CompareGreaterThanEqualsFunction.php +++ b/src/Collection/Functions/CompareGreaterThanEqualsFunction.php @@ -13,6 +13,8 @@ class CompareGreaterThanEqualsFunction extends BaseCompareFunction { protected function evaluateInPhp(mixed $sourceValue, mixed $targetValue, PropertyComparator|null $comparator): bool { + if ($sourceValue === null || $targetValue === null) return false; + if ($comparator === null) { return $sourceValue >= $targetValue; } else { diff --git a/src/Collection/Functions/CompareGreaterThanFunction.php b/src/Collection/Functions/CompareGreaterThanFunction.php index 7528e78d..81112c84 100644 --- a/src/Collection/Functions/CompareGreaterThanFunction.php +++ b/src/Collection/Functions/CompareGreaterThanFunction.php @@ -13,6 +13,8 @@ class CompareGreaterThanFunction extends BaseCompareFunction { protected function evaluateInPhp(mixed $sourceValue, mixed $targetValue, PropertyComparator|null $comparator): bool { + if ($sourceValue === null || $targetValue === null) return false; + if ($comparator === null) { return $sourceValue > $targetValue; } else { diff --git a/src/Collection/Functions/CompareSmallerThanEqualsFunction.php b/src/Collection/Functions/CompareSmallerThanEqualsFunction.php index aa62155a..73ccb35d 100644 --- a/src/Collection/Functions/CompareSmallerThanEqualsFunction.php +++ b/src/Collection/Functions/CompareSmallerThanEqualsFunction.php @@ -13,6 +13,8 @@ class CompareSmallerThanEqualsFunction extends BaseCompareFunction { protected function evaluateInPhp(mixed $sourceValue, mixed $targetValue, PropertyComparator|null $comparator): bool { + if ($sourceValue === null || $targetValue === null) return false; + if ($comparator === null) { return $sourceValue <= $targetValue; } else { diff --git a/src/Collection/Functions/CompareSmallerThanFunction.php b/src/Collection/Functions/CompareSmallerThanFunction.php index 590d9b36..619482af 100644 --- a/src/Collection/Functions/CompareSmallerThanFunction.php +++ b/src/Collection/Functions/CompareSmallerThanFunction.php @@ -13,6 +13,8 @@ class CompareSmallerThanFunction extends BaseCompareFunction { protected function evaluateInPhp(mixed $sourceValue, mixed $targetValue, PropertyComparator|null $comparator): bool { + if ($sourceValue === null || $targetValue === null) return false; + if ($comparator === null) { return $sourceValue < $targetValue; } else { diff --git a/tests/cases/unit/Collection/ArrayCollectionTest.phpt b/tests/cases/unit/Collection/ArrayCollectionTest.phpt index fa4a2fb2..726cc760 100644 --- a/tests/cases/unit/Collection/ArrayCollectionTest.phpt +++ b/tests/cases/unit/Collection/ArrayCollectionTest.phpt @@ -206,6 +206,42 @@ class ArrayCollectionTest extends TestCase } + /** + * @dataProvider comparisonWithNullProvider + */ + public function testComparisonOperatorsWithNull(string $property, mixed $smaller, mixed $equal, mixed $greater): void + { + $books = new ArrayCollection([ + $this->e(Book::class, ['title' => 'null', $property => null]), + $this->e(Book::class, ['title' => 'smaller', $property => $smaller]), + $this->e(Book::class, ['title' => 'equal', $property => $equal]), + $this->e(Book::class, ['title' => 'greater', $property => $greater]), + ], $this->orm->books); + + foreach ([ + '<' => ['smaller'], + '<=' => ['smaller', 'equal'], + '>' => ['greater'], + '>=' => ['equal', 'greater'], + ] as $operator => $expected) { + Assert::same($expected, $books->findBy([$property . $operator => $equal])->fetchPairs(null, 'title')); + Assert::same([], $books->findBy([$property . $operator => null])->fetchPairs(null, 'title')); + } + } + + + /** + * @return array + */ + protected function comparisonWithNullProvider(): array + { + return [ + ['id', -1, 0, 1], + ['printedAt', new DateTime('2025-12-02'), new DateTime('2026-01-01'), new DateTime('2026-02-02')], + ]; + } + + public function testLogicalOperatorsWithNonBooleanOperands(): void { $books = new ArrayCollection([