Skip to content

Commit

Permalink
Scopes have a toArray() method to get a true array, useful for applyi…
Browse files Browse the repository at this point in the history
…ng them to array_map() or similar
  • Loading branch information
SergeyTsalkov committed Oct 27, 2024
1 parent ec58f62 commit 8413fe4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions orm.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,10 @@ function last() {
return $this[$count-1];
}

function toArray() {
return iterator_to_array($this);
}

#[\ReturnTypeWillChange]
function count() {
$this->run_if_missing();
Expand Down
5 changes: 5 additions & 0 deletions simpletest/BasicOrmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,11 @@ function test_11_scope() {
$FirstTeenager = Person::scope('first_teenager');
$this->assert(count($FirstTeenager) === 1);
$this->assert($FirstTeenager[0]->name === 'Ellie');

$array = $FirstTeenager->toArray();
$this->assert(!is_array($FirstTeenager));
$this->assert(is_array($array));
$this->assert($array[0]->name === 'Ellie');
}

// * has_many: works with scoping
Expand Down

0 comments on commit 8413fe4

Please sign in to comment.