Skip to content

Commit

Permalink
Fix ArrayList canFilterBy to work with ArrayData (#10915)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Aug 14, 2023
1 parent cdbc50c commit c7cd262
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/ORM/ArrayList.php
Expand Up @@ -604,7 +604,15 @@ public function canFilterBy($by)

$firstRecord = $this->first();

return is_array($firstRecord) ? array_key_exists($by, $firstRecord) : property_exists($firstRecord, $by ?? '');
if (is_array($firstRecord)) {
return array_key_exists($by, $firstRecord);
}

if ($firstRecord instanceof ViewableData) {
return $firstRecord->hasField($by);
}

return property_exists($firstRecord, $by ?? '');
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/php/ORM/ArrayListTest.php
Expand Up @@ -7,6 +7,7 @@
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\Filterable;
use SilverStripe\View\ArrayData;
use stdClass;

class ArrayListTest extends SapphireTest
Expand Down Expand Up @@ -1174,6 +1175,20 @@ public function testCanFilterBy()
$this->assertFalse($list->canFilterBy('Age'));
}

public function testCanFilterByArrayData()
{
$list = new ArrayList(
[
new ArrayData(['Name' => 'Steve']),
new ArrayData(['Name' => 'Bob']),
new ArrayData(['Name' => 'John'])
]
);

$this->assertTrue($list->canFilterBy('Name'));
$this->assertFalse($list->canFilterBy('Age'));
}

public function testCanFilterByEmpty()
{
$list = new ArrayList();
Expand Down

0 comments on commit c7cd262

Please sign in to comment.