Skip to content
Open
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
40 changes: 28 additions & 12 deletions src/ORM/EagerLoadedList.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,28 +485,40 @@ public function find($key, $value): ?DataObject

public function filter(...$args): static
{
$filters = $this->normaliseFilterArgs($args, __FUNCTION__);
$list = clone $this;
$list->rows = $this->getMatches($filters);

if ($list->rows) {
$filters = $list->normaliseFilterArgs($args, __FUNCTION__);
$list->rows = $this->getMatches($filters);
}

return $list;
}

public function filterAny(...$args): static
{
$filters = $this->normaliseFilterArgs($args, __FUNCTION__);
$list = clone $this;
$list->rows = $this->getMatches($filters, true);

if ($list->rows) {
$filters = $list->normaliseFilterArgs($args, __FUNCTION__);
$list->rows = $this->getMatches($filters, true);
}

return $list;
}

public function exclude(...$args): static
{
$filters = $this->normaliseFilterArgs($args, __FUNCTION__);
$toRemove = $this->getMatches($filters);
$list = clone $this;
foreach ($toRemove as $id => $row) {
unset($list->rows[$id]);

if ($list->rows) {
$filters = $list->normaliseFilterArgs($args, __FUNCTION__);
$toRemove = $list->getMatches($filters);
foreach ($toRemove as $id => $row) {
unset($list->rows[$id]);
}
}

return $list;
}

Expand All @@ -517,12 +529,16 @@ public function exclude(...$args): static
*/
public function excludeAny(...$args): static
{
$filters = $this->normaliseFilterArgs($args, __FUNCTION__);
$toRemove = $this->getMatches($filters, true);
$list = clone $this;
foreach ($toRemove as $id => $row) {
unset($list->rows[$id]);

if ($list->rows) {
$filters = $list->normaliseFilterArgs($args, __FUNCTION__);
$toRemove = $list->getMatches($filters, true);
foreach ($toRemove as $id => $row) {
unset($list->rows[$id]);
}
}

return $list;
}

Expand Down
17 changes: 17 additions & 0 deletions tests/php/ORM/EagerLoadedListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2345,4 +2345,21 @@ public function testDebug()
);
$this->assertStringEndsWith('</ul>', $result);
}

public function testEmptyList()
{
$list = Category::get()->eagerLoad('Products');
$categoryA = $this->idFromFixture(Category::class, 'categorya');

/** @var Category $category */
foreach ($list as $category) {
$count = $category->Products()->filter('Title:StartsWith', 'Product')->count();

if ($category->ID == $categoryA) {
$this->assertEquals(2, $count);
} else {
$this->assertEquals(0, $count);
}
}
}
}