Skip to content

Commit 8413fe4

Browse files
committed
Scopes have a toArray() method to get a true array, useful for applying them to array_map() or similar
1 parent ec58f62 commit 8413fe4

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

orm.class.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,10 @@ function last() {
880880
return $this[$count-1];
881881
}
882882

883+
function toArray() {
884+
return iterator_to_array($this);
885+
}
886+
883887
#[\ReturnTypeWillChange]
884888
function count() {
885889
$this->run_if_missing();

simpletest/BasicOrmTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,11 @@ function test_11_scope() {
429429
$FirstTeenager = Person::scope('first_teenager');
430430
$this->assert(count($FirstTeenager) === 1);
431431
$this->assert($FirstTeenager[0]->name === 'Ellie');
432+
433+
$array = $FirstTeenager->toArray();
434+
$this->assert(!is_array($FirstTeenager));
435+
$this->assert(is_array($array));
436+
$this->assert($array[0]->name === 'Ellie');
432437
}
433438

434439
// * has_many: works with scoping

0 commit comments

Comments
 (0)