diff --git a/src/BaseActiveRecordTrait.php b/src/BaseActiveRecordTrait.php index c5061b1ac..50c5ac2d4 100644 --- a/src/BaseActiveRecordTrait.php +++ b/src/BaseActiveRecordTrait.php @@ -250,11 +250,11 @@ public function offsetGet(mixed $offset): mixed * It is implicitly called when you use something like `$model[$offset] = $item;`. * * @param mixed $offset the offset to set element. - * @param mixed $item the element value. + * @param mixed $value the element value. */ - public function offsetSet(mixed $offset, mixed $item): void + public function offsetSet(mixed $offset, mixed $value): void { - $this->$offset = $item; + $this->$offset = $value; } /** diff --git a/tests/ActiveRecordTest.php b/tests/ActiveRecordTest.php index 6fb1bfc7a..de8d993bc 100644 --- a/tests/ActiveRecordTest.php +++ b/tests/ActiveRecordTest.php @@ -771,7 +771,16 @@ public function testToArrayForArrayable(): void $this->checkFixture($this->db, 'customer', true); $customerQuery = new ActiveQuery(CustomerForArrayable::class, $this->db); + + /** @var CustomerForArrayable $customer */ $customer = $customerQuery->findOne(1); + /** @var CustomerForArrayable $customer2 */ + $customer2 = $customerQuery->findOne(2); + /** @var CustomerForArrayable $customer3 */ + $customer3 = $customerQuery->findOne(3); + + $customer->setItem($customer2); + $customer->setItems($customer3); $this->assertSame( [ @@ -780,9 +789,34 @@ public function testToArrayForArrayable(): void 'name' => 'user1', 'address' => 'address1', 'status' => 'active', - 'profile_id' => 1, + 'item' => [ + 'id' => 2, + 'name' => 'user2', + 'email' => 'user2@example.com', + 'status' => 'active', + ], + 'items' => [ + [ + 'id' => 3, + 'email' => 'user3@example.com', + 'name' => 'user3', + 'status' => 'inactive', + ], + ] ], - ArrayHelper::toArray($customer), + $customer->toArray([ + 'id', + 'name', + 'email', + 'address', + 'status', + 'item.id', + 'item.name', + 'item.email', + 'items.0.id', + 'items.0.name', + 'items.0.email', + ]), ); } } diff --git a/tests/Driver/Oracle/ActiveRecordTest.php b/tests/Driver/Oracle/ActiveRecordTest.php index af0094387..e48379226 100644 --- a/tests/Driver/Oracle/ActiveRecordTest.php +++ b/tests/Driver/Oracle/ActiveRecordTest.php @@ -174,7 +174,16 @@ public function testToArrayForArrayable(): void $this->checkFixture($this->db, 'customer', true); $customerQuery = new ActiveQuery(CustomerForArrayable::class, $this->db); + + /** @var CustomerForArrayable $customer */ $customer = $customerQuery->findOne(1); + /** @var CustomerForArrayable $customer2 */ + $customer2 = $customerQuery->findOne(2); + /** @var CustomerForArrayable $customer3 */ + $customer3 = $customerQuery->findOne(3); + + $customer->setItem($customer2); + $customer->setItems($customer3); $this->assertSame( [ @@ -183,10 +192,34 @@ public function testToArrayForArrayable(): void 'name' => 'user1', 'address' => 'address1', 'status' => 'active', - 'bool_status' => '1', - 'profile_id' => 1, + 'item' => [ + 'id' => 2, + 'name' => 'user2', + 'email' => 'user2@example.com', + 'status' => 'active', + ], + 'items' => [ + [ + 'id' => 3, + 'email' => 'user3@example.com', + 'name' => 'user3', + 'status' => 'inactive', + ], + ] ], - ArrayHelper::toArray($customer), + $customer->toArray([ + 'id', + 'name', + 'email', + 'address', + 'status', + 'item.id', + 'item.name', + 'item.email', + 'items.0.id', + 'items.0.name', + 'items.0.email', + ]), ); } } diff --git a/tests/Driver/Pgsql/ActiveRecordTest.php b/tests/Driver/Pgsql/ActiveRecordTest.php index e357fcf8c..620d71d36 100644 --- a/tests/Driver/Pgsql/ActiveRecordTest.php +++ b/tests/Driver/Pgsql/ActiveRecordTest.php @@ -389,7 +389,16 @@ public function testToArrayForArrayable(): void $this->checkFixture($this->db, 'customer', true); $customerQuery = new ActiveQuery(CustomerForArrayable::class, $this->db); + + /** @var CustomerForArrayable $customer */ $customer = $customerQuery->findOne(1); + /** @var CustomerForArrayable $customer2 */ + $customer2 = $customerQuery->findOne(2); + /** @var CustomerForArrayable $customer3 */ + $customer3 = $customerQuery->findOne(3); + + $customer->setItem($customer2); + $customer->setItems($customer3); $this->assertSame( [ @@ -398,10 +407,34 @@ public function testToArrayForArrayable(): void 'name' => 'user1', 'address' => 'address1', 'status' => 'active', - 'bool_status' => true, - 'profile_id' => 1, + 'item' => [ + 'id' => 2, + 'name' => 'user2', + 'email' => 'user2@example.com', + 'status' => 'active', + ], + 'items' => [ + [ + 'id' => 3, + 'email' => 'user3@example.com', + 'name' => 'user3', + 'status' => 'inactive', + ], + ] ], - ArrayHelper::toArray($customer), + $customer->toArray([ + 'id', + 'name', + 'email', + 'address', + 'status', + 'item.id', + 'item.name', + 'item.email', + 'items.0.id', + 'items.0.name', + 'items.0.email', + ]), ); } } diff --git a/tests/Stubs/ActiveRecord/CustomerForArrayable.php b/tests/Stubs/ActiveRecord/CustomerForArrayable.php index 70bd1662b..7a4bff910 100644 --- a/tests/Stubs/ActiveRecord/CustomerForArrayable.php +++ b/tests/Stubs/ActiveRecord/CustomerForArrayable.php @@ -1,5 +1,7 @@ item = $item; + } + + public function setItems(CustomerForArrayable ...$items) + { + $this->items = $items; + } + public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array { $data = parent::toArray($fields, $expand, $recursive); @@ -28,4 +53,4 @@ public function toArray(array $fields = [], array $expand = [], bool $recursive return $data; } -} \ No newline at end of file +}