Skip to content
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
20 changes: 20 additions & 0 deletions docs/collection-filtering.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/migrate_6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/Collection/Functions/CompareGreaterThanEqualsFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/Collection/Functions/CompareGreaterThanFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/Collection/Functions/CompareSmallerThanEqualsFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/Collection/Functions/CompareSmallerThanFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
36 changes: 36 additions & 0 deletions tests/cases/unit/Collection/ArrayCollectionTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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<array{string, mixed, mixed, mixed}>
*/
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([
Expand Down
Loading