From 12c33003f48472bf65fdc0da8ede4d7fe34e1630 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sat, 9 Dec 2023 16:50:31 +0800 Subject: [PATCH 01/50] implement the interface ArrayableInterface for BaseActiveRecord --- composer.json | 1 + src/ActiveRecordInterface.php | 12 ++++++- src/BaseActiveRecord.php | 33 +++++++++++++------ tests/ActiveRecordTest.php | 22 +++++++++++++ tests/Driver/Oracle/ActiveRecordTest.php | 22 +++++++++++++ tests/Driver/Pgsql/ActiveRecordTest.php | 22 +++++++++++++ .../ActiveRecord/CustomerForArrayable.php | 31 +++++++++++++++++ 7 files changed, 132 insertions(+), 11 deletions(-) create mode 100644 tests/Stubs/ActiveRecord/CustomerForArrayable.php diff --git a/composer.json b/composer.json index 1d26f0fdf..9f43232ff 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ "require": { "php": "^8.0", "ext-json": "*", + "yiisoft/arrays": "^3.0", "yiisoft/db": "^1.1", "yiisoft/factory": "^1.0" }, diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index 2177dd362..0be43e215 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -502,6 +502,16 @@ public function populateRecord(array|object $row): void; /** * Serializes the active record into its array implementation with attribute name as a key, and it values as value. + * + * @param array $fields the fields that the output array should contain. Fields not specified + * in {@see fields()} will be ignored. If this parameter is empty, all fields as specified + * in {@see fields()} will be returned. + * @param array $expand the additional fields that the output array should contain. + * Fields not specified in {@see extraFields()} will be ignored. If this parameter is empty, no extra fields + * will be returned. + * @param bool $recursive Whether to recursively return array representation of embedded objects. + * + * @return array The array representation of the object. */ - public function toArray(): array; + public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array; } diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index bdbdd4d1d..fc40a9c76 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -9,6 +9,9 @@ use IteratorAggregate; use ReflectionException; use Throwable; +use Yiisoft\Arrays\ArrayableInterface; +use Yiisoft\Arrays\ArrayableTrait; +use Yiisoft\Arrays\ArrayHelper; use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; @@ -40,8 +43,9 @@ * @template-implements ArrayAccess * @template-implements IteratorAggregate */ -abstract class BaseActiveRecord implements ActiveRecordInterface, IteratorAggregate, ArrayAccess +abstract class BaseActiveRecord implements ActiveRecordInterface, IteratorAggregate, ArrayAccess, ArrayableInterface { + use ArrayableTrait; use BaseActiveRecordTrait; private array $attributes = []; @@ -1266,19 +1270,28 @@ public function getTableName(): string return $this->tableName; } - public function toArray(): array + /** + * @inheritDoc + */ + public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array { $data = []; - foreach ($this->fields() as $key => $value) { - if ($value instanceof Closure) { - /** @var mixed */ - $data[$key] = $value($this); - } else { - /** @var mixed */ - $data[$value] = $this[$value]; + foreach ($this->resolveFields($fields, $expand) as $field => $definition) { + $attribute = $definition instanceof Closure ? $definition($this, $field) : $definition; + + if ($recursive) { + $nestedFields = $this->extractFieldsFor($fields, $field); + $nestedExpand = $this->extractFieldsFor($expand, $field); + if ($attribute instanceof ArrayableInterface) { + $attribute = $attribute->toArray($nestedFields, $nestedExpand); + } elseif (is_array($attribute) && ($nestedExpand || $nestedFields)) { + $attribute = $this->filterAndExpand($attribute, $nestedFields, $nestedExpand); + } } + $data[$field] = $attribute; } - return $data; + + return $recursive ? ArrayHelper::toArray($data) : $data; } } diff --git a/tests/ActiveRecordTest.php b/tests/ActiveRecordTest.php index 4d054d29e..6fb1bfc7a 100644 --- a/tests/ActiveRecordTest.php +++ b/tests/ActiveRecordTest.php @@ -10,6 +10,7 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Cat; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerClosureField; +use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerForArrayable; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerWithAlias; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Dog; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Item; @@ -20,6 +21,7 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\OrderItemWithNullFK; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Type; use Yiisoft\ActiveRecord\Tests\Support\Assert; +use Yiisoft\Arrays\ArrayHelper; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidCallException; @@ -763,4 +765,24 @@ public function testToArrayWithClosure(): void $customer->toArray(), ); } + + public function testToArrayForArrayable(): void + { + $this->checkFixture($this->db, 'customer', true); + + $customerQuery = new ActiveQuery(CustomerForArrayable::class, $this->db); + $customer = $customerQuery->findOne(1); + + $this->assertSame( + [ + 'id' => 1, + 'email' => 'user1@example.com', + 'name' => 'user1', + 'address' => 'address1', + 'status' => 'active', + 'profile_id' => 1, + ], + ArrayHelper::toArray($customer), + ); + } } diff --git a/tests/Driver/Oracle/ActiveRecordTest.php b/tests/Driver/Oracle/ActiveRecordTest.php index dbad51704..14f6f24d4 100644 --- a/tests/Driver/Oracle/ActiveRecordTest.php +++ b/tests/Driver/Oracle/ActiveRecordTest.php @@ -7,8 +7,10 @@ use Yiisoft\ActiveRecord\ActiveQuery; use Yiisoft\ActiveRecord\Tests\Driver\Oracle\Stubs\Customer; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerClosureField; +use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerForArrayable; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Type; use Yiisoft\ActiveRecord\Tests\Support\OracleHelper; +use Yiisoft\Arrays\ArrayHelper; final class ActiveRecordTest extends \Yiisoft\ActiveRecord\Tests\ActiveRecordTest { @@ -166,4 +168,24 @@ public function testToArrayWithClosure(): void $customer->toArray(), ); } + + public function testToArrayForArrayable(): void + { + $this->checkFixture($this->db, 'customer', true); + + $customerQuery = new ActiveQuery(CustomerForArrayable::class, $this->db); + $customer = $customerQuery->findOne(1); + + $this->assertSame( + [ + 'id' => 1, + 'email' => 'user1@example.com', + 'name' => 'user1', + 'address' => 'address1', + 'status' => 'active', + 'profile_id' => 1, + ], + ArrayHelper::toArray($customer), + ); + } } diff --git a/tests/Driver/Pgsql/ActiveRecordTest.php b/tests/Driver/Pgsql/ActiveRecordTest.php index 3a91cbd76..f6357ad84 100644 --- a/tests/Driver/Pgsql/ActiveRecordTest.php +++ b/tests/Driver/Pgsql/ActiveRecordTest.php @@ -12,9 +12,11 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\BoolAR; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerClosureField; +use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerForArrayable; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\DefaultPk; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\UserAR; use Yiisoft\ActiveRecord\Tests\Support\PgsqlHelper; +use Yiisoft\Arrays\ArrayHelper; use Yiisoft\Db\Expression\ArrayExpression; use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Expression\JsonExpression; @@ -381,4 +383,24 @@ public function testToArrayWithClosure(): void $customer->toArray(), ); } + + public function testToArrayForArrayable(): void + { + $this->checkFixture($this->db, 'customer', true); + + $customerQuery = new ActiveQuery(CustomerForArrayable::class, $this->db); + $customer = $customerQuery->findOne(1); + + $this->assertSame( + [ + 'id' => 1, + 'email' => 'user1@example.com', + 'name' => 'user1', + 'address' => 'address1', + 'status' => 'active', + 'profile_id' => 1, + ], + ArrayHelper::toArray($customer), + ); + } } diff --git a/tests/Stubs/ActiveRecord/CustomerForArrayable.php b/tests/Stubs/ActiveRecord/CustomerForArrayable.php new file mode 100644 index 000000000..70bd1662b --- /dev/null +++ b/tests/Stubs/ActiveRecord/CustomerForArrayable.php @@ -0,0 +1,31 @@ +status == 1 ? 'active' : 'inactive'; + + return $data; + } +} \ No newline at end of file From be499a5238f5a30d3d417f13286e241540ee49fe Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 00:34:21 +0800 Subject: [PATCH 02/50] implement the interface ArrayableInterface for BaseActiveRecord --- src/BaseActiveRecord.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index fc40a9c76..21a0308c4 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -1278,7 +1278,7 @@ public function toArray(array $fields = [], array $expand = [], bool $recursive $data = []; foreach ($this->resolveFields($fields, $expand) as $field => $definition) { - $attribute = $definition instanceof Closure ? $definition($this, $field) : $definition; + $attribute = $definition instanceof Closure ? $definition($this, $field) : $this[$definition]; if ($recursive) { $nestedFields = $this->extractFieldsFor($fields, $field); From 3988dcc18a66291431af0ee6646b382dc5023ef7 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 00:41:26 +0800 Subject: [PATCH 03/50] implement the interface ArrayableInterface for BaseActiveRecord --- tests/Driver/Oracle/ActiveRecordTest.php | 1 + tests/Driver/Pgsql/ActiveRecordTest.php | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/Driver/Oracle/ActiveRecordTest.php b/tests/Driver/Oracle/ActiveRecordTest.php index 14f6f24d4..af0094387 100644 --- a/tests/Driver/Oracle/ActiveRecordTest.php +++ b/tests/Driver/Oracle/ActiveRecordTest.php @@ -183,6 +183,7 @@ public function testToArrayForArrayable(): void 'name' => 'user1', 'address' => 'address1', 'status' => 'active', + 'bool_status' => '1', 'profile_id' => 1, ], ArrayHelper::toArray($customer), diff --git a/tests/Driver/Pgsql/ActiveRecordTest.php b/tests/Driver/Pgsql/ActiveRecordTest.php index f6357ad84..e357fcf8c 100644 --- a/tests/Driver/Pgsql/ActiveRecordTest.php +++ b/tests/Driver/Pgsql/ActiveRecordTest.php @@ -398,6 +398,7 @@ public function testToArrayForArrayable(): void 'name' => 'user1', 'address' => 'address1', 'status' => 'active', + 'bool_status' => true, 'profile_id' => 1, ], ArrayHelper::toArray($customer), From 424f84162b98254e3c31ec09fa5ec90f45513ee6 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 02:36:32 +0800 Subject: [PATCH 04/50] implement the interface ArrayableInterface for BaseActiveRecord --- src/BaseActiveRecordTrait.php | 6 +-- tests/ActiveRecordTest.php | 38 +++++++++++++++++- tests/Driver/Oracle/ActiveRecordTest.php | 39 +++++++++++++++++-- tests/Driver/Pgsql/ActiveRecordTest.php | 39 +++++++++++++++++-- .../ActiveRecord/CustomerForArrayable.php | 27 ++++++++++++- 5 files changed, 137 insertions(+), 12 deletions(-) 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 +} From b64dde562669a73c0d649ef2050c05ba4d8f4eaa Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 02:42:11 +0800 Subject: [PATCH 05/50] implement the interface ArrayableInterface for BaseActiveRecord --- tests/Driver/Pgsql/ActiveRecordTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Driver/Pgsql/ActiveRecordTest.php b/tests/Driver/Pgsql/ActiveRecordTest.php index 620d71d36..6f6efd6cd 100644 --- a/tests/Driver/Pgsql/ActiveRecordTest.php +++ b/tests/Driver/Pgsql/ActiveRecordTest.php @@ -403,8 +403,8 @@ public function testToArrayForArrayable(): void $this->assertSame( [ 'id' => 1, - 'email' => 'user1@example.com', 'name' => 'user1', + 'email' => 'user1@example.com', 'address' => 'address1', 'status' => 'active', 'item' => [ @@ -416,8 +416,8 @@ public function testToArrayForArrayable(): void 'items' => [ [ 'id' => 3, - 'email' => 'user3@example.com', 'name' => 'user3', + 'email' => 'user3@example.com', 'status' => 'inactive', ], ] @@ -429,11 +429,11 @@ public function testToArrayForArrayable(): void 'address', 'status', 'item.id', - 'item.name', 'item.email', + 'item.name', 'items.0.id', - 'items.0.name', 'items.0.email', + 'items.0.name', ]), ); } From 0f680fe018e9a48d8a667f3ad7f67999a7efb7f8 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 02:44:00 +0800 Subject: [PATCH 06/50] implement the interface ArrayableInterface for BaseActiveRecord --- tests/ActiveRecordTest.php | 4 ++-- tests/Driver/Oracle/ActiveRecordTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/ActiveRecordTest.php b/tests/ActiveRecordTest.php index de8d993bc..d9c4283b0 100644 --- a/tests/ActiveRecordTest.php +++ b/tests/ActiveRecordTest.php @@ -785,8 +785,8 @@ public function testToArrayForArrayable(): void $this->assertSame( [ 'id' => 1, - 'email' => 'user1@example.com', 'name' => 'user1', + 'email' => 'user1@example.com', 'address' => 'address1', 'status' => 'active', 'item' => [ @@ -798,8 +798,8 @@ public function testToArrayForArrayable(): void 'items' => [ [ 'id' => 3, - 'email' => 'user3@example.com', 'name' => 'user3', + 'email' => 'user3@example.com', 'status' => 'inactive', ], ] diff --git a/tests/Driver/Oracle/ActiveRecordTest.php b/tests/Driver/Oracle/ActiveRecordTest.php index e48379226..078b3cf1e 100644 --- a/tests/Driver/Oracle/ActiveRecordTest.php +++ b/tests/Driver/Oracle/ActiveRecordTest.php @@ -188,8 +188,8 @@ public function testToArrayForArrayable(): void $this->assertSame( [ 'id' => 1, - 'email' => 'user1@example.com', 'name' => 'user1', + 'email' => 'user1@example.com', 'address' => 'address1', 'status' => 'active', 'item' => [ @@ -201,8 +201,8 @@ public function testToArrayForArrayable(): void 'items' => [ [ 'id' => 3, - 'email' => 'user3@example.com', 'name' => 'user3', + 'email' => 'user3@example.com', 'status' => 'inactive', ], ] From 594a2225af0d16a830549513f11e6f4b88c53fd9 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 02:45:52 +0800 Subject: [PATCH 07/50] implement the interface ArrayableInterface for BaseActiveRecord --- tests/ActiveRecordTest.php | 3 +-- tests/Driver/Pgsql/ActiveRecordTest.php | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/ActiveRecordTest.php b/tests/ActiveRecordTest.php index d9c4283b0..779935782 100644 --- a/tests/ActiveRecordTest.php +++ b/tests/ActiveRecordTest.php @@ -21,7 +21,6 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\OrderItemWithNullFK; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Type; use Yiisoft\ActiveRecord\Tests\Support\Assert; -use Yiisoft\Arrays\ArrayHelper; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidCallException; @@ -802,7 +801,7 @@ public function testToArrayForArrayable(): void 'email' => 'user3@example.com', 'status' => 'inactive', ], - ] + ], ], $customer->toArray([ 'id', diff --git a/tests/Driver/Pgsql/ActiveRecordTest.php b/tests/Driver/Pgsql/ActiveRecordTest.php index 6f6efd6cd..44a7999e6 100644 --- a/tests/Driver/Pgsql/ActiveRecordTest.php +++ b/tests/Driver/Pgsql/ActiveRecordTest.php @@ -16,7 +16,6 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\DefaultPk; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\UserAR; use Yiisoft\ActiveRecord\Tests\Support\PgsqlHelper; -use Yiisoft\Arrays\ArrayHelper; use Yiisoft\Db\Expression\ArrayExpression; use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Expression\JsonExpression; @@ -420,7 +419,7 @@ public function testToArrayForArrayable(): void 'email' => 'user3@example.com', 'status' => 'inactive', ], - ] + ], ], $customer->toArray([ 'id', From 08711a6f9a094f9d95f367fc0132aa2128bfbaae Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 02:47:11 +0800 Subject: [PATCH 08/50] implement the interface ArrayableInterface for BaseActiveRecord --- tests/Driver/Oracle/ActiveRecordTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Driver/Oracle/ActiveRecordTest.php b/tests/Driver/Oracle/ActiveRecordTest.php index 078b3cf1e..250212575 100644 --- a/tests/Driver/Oracle/ActiveRecordTest.php +++ b/tests/Driver/Oracle/ActiveRecordTest.php @@ -10,7 +10,6 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerForArrayable; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Type; use Yiisoft\ActiveRecord\Tests\Support\OracleHelper; -use Yiisoft\Arrays\ArrayHelper; final class ActiveRecordTest extends \Yiisoft\ActiveRecord\Tests\ActiveRecordTest { @@ -205,7 +204,7 @@ public function testToArrayForArrayable(): void 'email' => 'user3@example.com', 'status' => 'inactive', ], - ] + ], ], $customer->toArray([ 'id', From cb9db0e0f30b13c3f2a1ad2e29d0fe25c1d722fc Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 02:48:12 +0800 Subject: [PATCH 09/50] implement the interface ArrayableInterface for BaseActiveRecord --- tests/Stubs/ActiveRecord/CustomerForArrayable.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Stubs/ActiveRecord/CustomerForArrayable.php b/tests/Stubs/ActiveRecord/CustomerForArrayable.php index 7a4bff910..e54d1ce85 100644 --- a/tests/Stubs/ActiveRecord/CustomerForArrayable.php +++ b/tests/Stubs/ActiveRecord/CustomerForArrayable.php @@ -36,11 +36,11 @@ public function fields(): array return $fields; } - public function setItem(CustomerForArrayable $item) { + public function setItem(self $item) { $this->item = $item; } - public function setItems(CustomerForArrayable ...$items) + public function setItems(self ...$items) { $this->items = $items; } From 044755d0bf8a1d325f77a696ce94e6cabcb025aa Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 02:49:07 +0800 Subject: [PATCH 10/50] implement the interface ArrayableInterface for BaseActiveRecord --- tests/Stubs/ActiveRecord/CustomerForArrayable.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Stubs/ActiveRecord/CustomerForArrayable.php b/tests/Stubs/ActiveRecord/CustomerForArrayable.php index e54d1ce85..591cd79cf 100644 --- a/tests/Stubs/ActiveRecord/CustomerForArrayable.php +++ b/tests/Stubs/ActiveRecord/CustomerForArrayable.php @@ -36,7 +36,8 @@ public function fields(): array return $fields; } - public function setItem(self $item) { + public function setItem(self $item) + { $this->item = $item; } From fa7c4cb1144c5d2190ddeb37d4c773bc4921312b Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 02:52:21 +0800 Subject: [PATCH 11/50] implement the interface ArrayableInterface for BaseActiveRecord --- tests/ActiveRecordTest.php | 6 +++--- tests/Driver/Oracle/ActiveRecordTest.php | 6 +++--- tests/Driver/Pgsql/ActiveRecordTest.php | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/ActiveRecordTest.php b/tests/ActiveRecordTest.php index 779935782..7d232d6cf 100644 --- a/tests/ActiveRecordTest.php +++ b/tests/ActiveRecordTest.php @@ -784,21 +784,21 @@ public function testToArrayForArrayable(): void $this->assertSame( [ 'id' => 1, - 'name' => 'user1', 'email' => 'user1@example.com', + 'name' => 'user1', 'address' => 'address1', 'status' => 'active', 'item' => [ 'id' => 2, - 'name' => 'user2', 'email' => 'user2@example.com', + 'name' => 'user2', 'status' => 'active', ], 'items' => [ [ 'id' => 3, - 'name' => 'user3', 'email' => 'user3@example.com', + 'name' => 'user3', 'status' => 'inactive', ], ], diff --git a/tests/Driver/Oracle/ActiveRecordTest.php b/tests/Driver/Oracle/ActiveRecordTest.php index 250212575..d91b25b02 100644 --- a/tests/Driver/Oracle/ActiveRecordTest.php +++ b/tests/Driver/Oracle/ActiveRecordTest.php @@ -187,21 +187,21 @@ public function testToArrayForArrayable(): void $this->assertSame( [ 'id' => 1, - 'name' => 'user1', 'email' => 'user1@example.com', + 'name' => 'user1', 'address' => 'address1', 'status' => 'active', 'item' => [ 'id' => 2, - 'name' => 'user2', 'email' => 'user2@example.com', + 'name' => 'user2', 'status' => 'active', ], 'items' => [ [ 'id' => 3, - 'name' => 'user3', 'email' => 'user3@example.com', + 'name' => 'user3', 'status' => 'inactive', ], ], diff --git a/tests/Driver/Pgsql/ActiveRecordTest.php b/tests/Driver/Pgsql/ActiveRecordTest.php index 44a7999e6..51a25c23e 100644 --- a/tests/Driver/Pgsql/ActiveRecordTest.php +++ b/tests/Driver/Pgsql/ActiveRecordTest.php @@ -402,21 +402,21 @@ public function testToArrayForArrayable(): void $this->assertSame( [ 'id' => 1, - 'name' => 'user1', 'email' => 'user1@example.com', + 'name' => 'user1', 'address' => 'address1', 'status' => 'active', 'item' => [ 'id' => 2, - 'name' => 'user2', 'email' => 'user2@example.com', + 'name' => 'user2', 'status' => 'active', ], 'items' => [ [ 'id' => 3, - 'name' => 'user3', 'email' => 'user3@example.com', + 'name' => 'user3', 'status' => 'inactive', ], ], From 0af5333ffee098f07489b5cb0ed87c13f38314a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E9=A3=8E?= Date: Sun, 10 Dec 2023 12:44:30 +0800 Subject: [PATCH 12/50] Update src/BaseActiveRecord.php Co-authored-by: Sergei Tigrov --- src/BaseActiveRecord.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index 21a0308c4..7ec6c7f6f 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -1273,7 +1273,6 @@ public function getTableName(): string /** * @inheritDoc */ - public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array { $data = []; From baf1889c004dd6a681530044eaaa55ea5d47496a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E9=A3=8E?= Date: Sun, 10 Dec 2023 12:44:45 +0800 Subject: [PATCH 13/50] Update src/ActiveRecordInterface.php Co-authored-by: Sergei Tigrov --- src/ActiveRecordInterface.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index 0be43e215..6ec025b45 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -513,5 +513,4 @@ public function populateRecord(array|object $row): void; * * @return array The array representation of the object. */ - public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array; } From b479a120a6c9e881ebcc99cf2785e2a8b1422a58 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 15:33:59 +0800 Subject: [PATCH 14/50] Revert "Update src/ActiveRecordInterface.php" This reverts commit baf1889c004dd6a681530044eaaa55ea5d47496a. --- src/ActiveRecordInterface.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index 6ec025b45..0be43e215 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -513,4 +513,5 @@ public function populateRecord(array|object $row): void; * * @return array The array representation of the object. */ + public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array; } From 0af32f14a4d16a65e0ce17278314cbf3d89055ef Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 15:34:09 +0800 Subject: [PATCH 15/50] Revert "Update src/BaseActiveRecord.php" This reverts commit 0af5333ffee098f07489b5cb0ed87c13f38314a5. --- src/BaseActiveRecord.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index 7ec6c7f6f..21a0308c4 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -1273,6 +1273,7 @@ public function getTableName(): string /** * @inheritDoc */ + public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array { $data = []; From c84a97d391b12f5841cd55e0c82953dec9c5d237 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 15:39:55 +0800 Subject: [PATCH 16/50] implement the interface ArrayableInterface for BaseActiveRecord --- src/ActiveRecordInterface.php | 26 ++------------------------ src/BaseActiveRecord.php | 27 +-------------------------- 2 files changed, 3 insertions(+), 50 deletions(-) diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index 0be43e215..76f96cdaa 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -6,13 +6,14 @@ use Closure; use Throwable; +use Yiisoft\Arrays\ArrayableInterface; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Exception\StaleObjectException; -interface ActiveRecordInterface +interface ActiveRecordInterface extends ArrayableInterface { /** * Returns the list of all attribute names of the model. @@ -86,14 +87,6 @@ public function deleteAll(array $condition = []): int; */ public function equals(self $record): bool; - /** - * @return array The default implementation returns the names of the columns whose values have been populated into - * this record. - * - * @psalm-return array - */ - public function fields(): array; - /** * Filters array condition before it's assigned to a Query filter. * @@ -499,19 +492,4 @@ public function getOldAttributes(): array; * @throws InvalidConfigException */ public function populateRecord(array|object $row): void; - - /** - * Serializes the active record into its array implementation with attribute name as a key, and it values as value. - * - * @param array $fields the fields that the output array should contain. Fields not specified - * in {@see fields()} will be ignored. If this parameter is empty, all fields as specified - * in {@see fields()} will be returned. - * @param array $expand the additional fields that the output array should contain. - * Fields not specified in {@see extraFields()} will be ignored. If this parameter is empty, no extra fields - * will be returned. - * @param bool $recursive Whether to recursively return array representation of embedded objects. - * - * @return array The array representation of the object. - */ - public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array; } diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index 21a0308c4..de49ac475 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -43,7 +43,7 @@ * @template-implements ArrayAccess * @template-implements IteratorAggregate */ -abstract class BaseActiveRecord implements ActiveRecordInterface, IteratorAggregate, ArrayAccess, ArrayableInterface +abstract class BaseActiveRecord implements ActiveRecordInterface, IteratorAggregate, ArrayAccess { use ArrayableTrait; use BaseActiveRecordTrait; @@ -1269,29 +1269,4 @@ public function getTableName(): string return $this->tableName; } - - /** - * @inheritDoc - */ - public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array - { - $data = []; - - foreach ($this->resolveFields($fields, $expand) as $field => $definition) { - $attribute = $definition instanceof Closure ? $definition($this, $field) : $this[$definition]; - - if ($recursive) { - $nestedFields = $this->extractFieldsFor($fields, $field); - $nestedExpand = $this->extractFieldsFor($expand, $field); - if ($attribute instanceof ArrayableInterface) { - $attribute = $attribute->toArray($nestedFields, $nestedExpand); - } elseif (is_array($attribute) && ($nestedExpand || $nestedFields)) { - $attribute = $this->filterAndExpand($attribute, $nestedFields, $nestedExpand); - } - } - $data[$field] = $attribute; - } - - return $recursive ? ArrayHelper::toArray($data) : $data; - } } From 2b82b2bc31b247fbb2400f12180b20a33bb84e59 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sun, 10 Dec 2023 15:41:34 +0800 Subject: [PATCH 17/50] implement the interface ArrayableInterface for BaseActiveRecord --- src/ActiveRecordInterface.php | 1 - src/BaseActiveRecord.php | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index 76f96cdaa..6752b1992 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -4,7 +4,6 @@ namespace Yiisoft\ActiveRecord; -use Closure; use Throwable; use Yiisoft\Arrays\ArrayableInterface; use Yiisoft\Db\Exception\Exception; diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index de49ac475..448e32706 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -9,9 +9,7 @@ use IteratorAggregate; use ReflectionException; use Throwable; -use Yiisoft\Arrays\ArrayableInterface; use Yiisoft\Arrays\ArrayableTrait; -use Yiisoft\Arrays\ArrayHelper; use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; From 32cc7b4f9056f1df44b57f41b0ddae903d52d348 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E9=A3=8E?= Date: Mon, 11 Dec 2023 11:48:04 +0800 Subject: [PATCH 18/50] Update tests/Driver/Oracle/ActiveRecordTest.php Co-authored-by: Sergei Tigrov --- tests/Driver/Oracle/ActiveRecordTest.php | 54 ------------------------ 1 file changed, 54 deletions(-) diff --git a/tests/Driver/Oracle/ActiveRecordTest.php b/tests/Driver/Oracle/ActiveRecordTest.php index d91b25b02..ac4c9f407 100644 --- a/tests/Driver/Oracle/ActiveRecordTest.php +++ b/tests/Driver/Oracle/ActiveRecordTest.php @@ -167,58 +167,4 @@ public function testToArrayWithClosure(): void $customer->toArray(), ); } - - 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( - [ - 'id' => 1, - 'email' => 'user1@example.com', - 'name' => 'user1', - 'address' => 'address1', - 'status' => 'active', - 'item' => [ - 'id' => 2, - 'email' => 'user2@example.com', - 'name' => 'user2', - 'status' => 'active', - ], - 'items' => [ - [ - 'id' => 3, - 'email' => 'user3@example.com', - 'name' => 'user3', - 'status' => 'inactive', - ], - ], - ], - $customer->toArray([ - 'id', - 'name', - 'email', - 'address', - 'status', - 'item.id', - 'item.name', - 'item.email', - 'items.0.id', - 'items.0.name', - 'items.0.email', - ]), - ); - } } From 85832694b8fb474253e140d0c4e88404e2195e11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E9=A3=8E?= Date: Mon, 11 Dec 2023 11:48:12 +0800 Subject: [PATCH 19/50] Update tests/Driver/Pgsql/ActiveRecordTest.php Co-authored-by: Sergei Tigrov --- tests/Driver/Pgsql/ActiveRecordTest.php | 54 ------------------------- 1 file changed, 54 deletions(-) diff --git a/tests/Driver/Pgsql/ActiveRecordTest.php b/tests/Driver/Pgsql/ActiveRecordTest.php index 51a25c23e..de5c2dc92 100644 --- a/tests/Driver/Pgsql/ActiveRecordTest.php +++ b/tests/Driver/Pgsql/ActiveRecordTest.php @@ -382,58 +382,4 @@ public function testToArrayWithClosure(): void $customer->toArray(), ); } - - 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( - [ - 'id' => 1, - 'email' => 'user1@example.com', - 'name' => 'user1', - 'address' => 'address1', - 'status' => 'active', - 'item' => [ - 'id' => 2, - 'email' => 'user2@example.com', - 'name' => 'user2', - 'status' => 'active', - ], - 'items' => [ - [ - 'id' => 3, - 'email' => 'user3@example.com', - 'name' => 'user3', - 'status' => 'inactive', - ], - ], - ], - $customer->toArray([ - 'id', - 'name', - 'email', - 'address', - 'status', - 'item.id', - 'item.email', - 'item.name', - 'items.0.id', - 'items.0.email', - 'items.0.name', - ]), - ); - } } From e299dfa467f34139f2f9afe7099bea251c672de2 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Mon, 11 Dec 2023 11:50:48 +0800 Subject: [PATCH 20/50] implement the interface ArrayableInterface for BaseActiveRecord --- tests/Driver/Oracle/ActiveRecordTest.php | 1 - tests/Driver/Pgsql/ActiveRecordTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/Driver/Oracle/ActiveRecordTest.php b/tests/Driver/Oracle/ActiveRecordTest.php index ac4c9f407..dbad51704 100644 --- a/tests/Driver/Oracle/ActiveRecordTest.php +++ b/tests/Driver/Oracle/ActiveRecordTest.php @@ -7,7 +7,6 @@ use Yiisoft\ActiveRecord\ActiveQuery; use Yiisoft\ActiveRecord\Tests\Driver\Oracle\Stubs\Customer; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerClosureField; -use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerForArrayable; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Type; use Yiisoft\ActiveRecord\Tests\Support\OracleHelper; diff --git a/tests/Driver/Pgsql/ActiveRecordTest.php b/tests/Driver/Pgsql/ActiveRecordTest.php index de5c2dc92..3a91cbd76 100644 --- a/tests/Driver/Pgsql/ActiveRecordTest.php +++ b/tests/Driver/Pgsql/ActiveRecordTest.php @@ -12,7 +12,6 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\BoolAR; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerClosureField; -use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\CustomerForArrayable; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\DefaultPk; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\UserAR; use Yiisoft\ActiveRecord\Tests\Support\PgsqlHelper; From 1f8610452a1169deab650668b4dbfc24845f2246 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Mon, 11 Dec 2023 17:20:16 +0800 Subject: [PATCH 21/50] implement the interface ArrayableInterface for BaseActiveRecord --- src/ActiveRecordInterface.php | 26 ++++++++++++++++++++++++-- src/BaseActiveRecord.php | 3 ++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index 6752b1992..1049a6c5c 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -5,14 +5,13 @@ namespace Yiisoft\ActiveRecord; use Throwable; -use Yiisoft\Arrays\ArrayableInterface; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Exception\StaleObjectException; -interface ActiveRecordInterface extends ArrayableInterface +interface ActiveRecordInterface { /** * Returns the list of all attribute names of the model. @@ -86,6 +85,14 @@ public function deleteAll(array $condition = []): int; */ public function equals(self $record): bool; + /** + * @return array The default implementation returns the names of the columns whose values have been populated into + * this record. + * + * @psalm-return array + */ + public function fields(): array; + /** * Filters array condition before it's assigned to a Query filter. * @@ -491,4 +498,19 @@ public function getOldAttributes(): array; * @throws InvalidConfigException */ public function populateRecord(array|object $row): void; + + /** + * Serializes the active record into its array implementation with attribute name as a key, and it values as value. + * + * @param array $fields the fields that the output array should contain. Fields not specified + * in {@see fields()} will be ignored. If this parameter is empty, all fields as specified + * in {@see fields()} will be returned. + * @param array $expand the additional fields that the output array should contain. + * Fields not specified in {@see extraFields()} will be ignored. If this parameter is empty, no extra fields + * will be returned. + * @param bool $recursive Whether to recursively return array representation of embedded objects. + * + * @return array The array representation of the object. + */ + public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array; } diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index 448e32706..8180e271a 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -9,6 +9,7 @@ use IteratorAggregate; use ReflectionException; use Throwable; +use Yiisoft\Arrays\ArrayableInterface; use Yiisoft\Arrays\ArrayableTrait; use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Exception\Exception; @@ -41,7 +42,7 @@ * @template-implements ArrayAccess * @template-implements IteratorAggregate */ -abstract class BaseActiveRecord implements ActiveRecordInterface, IteratorAggregate, ArrayAccess +abstract class BaseActiveRecord implements ActiveRecordInterface, IteratorAggregate, ArrayAccess, ArrayableInterface { use ArrayableTrait; use BaseActiveRecordTrait; From 43f615b5a7c4d754007a99edd60d6bbe7213a7d0 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Mon, 11 Dec 2023 17:24:11 +0800 Subject: [PATCH 22/50] implement the interface ArrayableInterface for BaseActiveRecord --- src/ActiveRecordInterface.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index 1049a6c5c..0be43e215 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -4,6 +4,7 @@ namespace Yiisoft\ActiveRecord; +use Closure; use Throwable; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; From 418be7de6c995828a20b85069bc874cfeb90c316 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Mon, 11 Dec 2023 17:26:50 +0800 Subject: [PATCH 23/50] implement the interface ArrayableInterface for BaseActiveRecord --- src/BaseActiveRecord.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index 8180e271a..d6cb497e3 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -118,7 +118,7 @@ public function extraFields(): array } /** - * @psalm-suppress MixedReturnTypeCoercion + * @psalm-return array */ public function fields(): array { From 564e0191e5f9d34969f0b18325636c2f211a0499 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Mon, 11 Dec 2023 23:09:06 +0800 Subject: [PATCH 24/50] implement the interface ArrayableInterface for BaseActiveRecord --- src/ActiveRecordInterface.php | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index 0be43e215..f8a9952b8 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -4,7 +4,6 @@ namespace Yiisoft\ActiveRecord; -use Closure; use Throwable; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; @@ -86,14 +85,6 @@ public function deleteAll(array $condition = []): int; */ public function equals(self $record): bool; - /** - * @return array The default implementation returns the names of the columns whose values have been populated into - * this record. - * - * @psalm-return array - */ - public function fields(): array; - /** * Filters array condition before it's assigned to a Query filter. * @@ -499,19 +490,4 @@ public function getOldAttributes(): array; * @throws InvalidConfigException */ public function populateRecord(array|object $row): void; - - /** - * Serializes the active record into its array implementation with attribute name as a key, and it values as value. - * - * @param array $fields the fields that the output array should contain. Fields not specified - * in {@see fields()} will be ignored. If this parameter is empty, all fields as specified - * in {@see fields()} will be returned. - * @param array $expand the additional fields that the output array should contain. - * Fields not specified in {@see extraFields()} will be ignored. If this parameter is empty, no extra fields - * will be returned. - * @param bool $recursive Whether to recursively return array representation of embedded objects. - * - * @return array The array representation of the object. - */ - public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array; } From a5ab13363218c507eca4d85884fac48e52dcdc43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E9=A3=8E?= Date: Tue, 19 Dec 2023 21:32:44 +0800 Subject: [PATCH 25/50] Update the comments of the method createQueryTo in ActiveRecordFactory so that the IDE can automatically identify the specific class of ActiveRecord --- src/ActiveRecordFactory.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ActiveRecordFactory.php b/src/ActiveRecordFactory.php index 3940fa741..2e2ded433 100644 --- a/src/ActiveRecordFactory.php +++ b/src/ActiveRecordFactory.php @@ -20,15 +20,17 @@ public function __construct(private Factory $factory) /** * Allows you to create an active record instance through the factory. * - * @param string $arClass active record class. + * @param class-string $arClass active record class. * @param string $tableName The name of the table associated with this ActiveRecord class, if its empty string the * name will be generated automatically by calling {@see getTableName()} in the active record class. * @param ConnectionInterface|null $db the database connection used for creating active record instances. + * @return ActiveRecordInterface|T * * @throws CircularReferenceException * @throws InvalidConfigException * @throws NotFoundException * @throws NotInstantiableException + * @template T */ public function createAR( string $arClass, From ae5773323033de9847da74bc77e6eb0ff5cb1891 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E9=A3=8E?= Date: Tue, 19 Dec 2023 21:37:22 +0800 Subject: [PATCH 26/50] Update the comments of the method createQueryTo in ActiveRecordFactory so that the IDE can automatically identify the specific class of ActiveRecord --- src/ActiveRecordFactory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ActiveRecordFactory.php b/src/ActiveRecordFactory.php index 2e2ded433..887ef32c8 100644 --- a/src/ActiveRecordFactory.php +++ b/src/ActiveRecordFactory.php @@ -24,12 +24,12 @@ public function __construct(private Factory $factory) * @param string $tableName The name of the table associated with this ActiveRecord class, if its empty string the * name will be generated automatically by calling {@see getTableName()} in the active record class. * @param ConnectionInterface|null $db the database connection used for creating active record instances. - * @return ActiveRecordInterface|T - * * @throws CircularReferenceException * @throws InvalidConfigException * @throws NotFoundException * @throws NotInstantiableException + * @return ActiveRecordInterface|T + * * @template T */ public function createAR( From 480c0e1158b614b17b498016514c2c389437a02d Mon Sep 17 00:00:00 2001 From: niqingyang Date: Tue, 19 Dec 2023 21:45:51 +0800 Subject: [PATCH 27/50] Update the comments of the method createQueryTo in ActiveRecordFactory so that the IDE can automatically identify the specific class of ActiveRecord --- src/ActiveRecordFactory.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/ActiveRecordFactory.php b/src/ActiveRecordFactory.php index 887ef32c8..83635a038 100644 --- a/src/ActiveRecordFactory.php +++ b/src/ActiveRecordFactory.php @@ -20,17 +20,19 @@ public function __construct(private Factory $factory) /** * Allows you to create an active record instance through the factory. * - * @param class-string $arClass active record class. + * @param string $arClass active record class. * @param string $tableName The name of the table associated with this ActiveRecord class, if its empty string the * name will be generated automatically by calling {@see getTableName()} in the active record class. * @param ConnectionInterface|null $db the database connection used for creating active record instances. + * * @throws CircularReferenceException * @throws InvalidConfigException * @throws NotFoundException * @throws NotInstantiableException - * @return ActiveRecordInterface|T * - * @template T + * @psalm-template T + * @psalm-param class-string $arClass + * @psalm-return T */ public function createAR( string $arClass, From 5044c91a6013ceef80ffec3488ac997ed866834d Mon Sep 17 00:00:00 2001 From: niqingyang Date: Tue, 19 Dec 2023 21:48:46 +0800 Subject: [PATCH 28/50] Update the comments of the method createQueryTo in ActiveRecordFactory so that the IDE can automatically identify the specific class of ActiveRecord --- src/ActiveRecordFactory.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ActiveRecordFactory.php b/src/ActiveRecordFactory.php index 83635a038..8426fa7e4 100644 --- a/src/ActiveRecordFactory.php +++ b/src/ActiveRecordFactory.php @@ -33,6 +33,7 @@ public function __construct(private Factory $factory) * @psalm-template T * @psalm-param class-string $arClass * @psalm-return T + * @psalm-suppress T */ public function createAR( string $arClass, From 8f360aa796b41cf8b2798285df0384cece12ad6b Mon Sep 17 00:00:00 2001 From: niqingyang Date: Tue, 19 Dec 2023 21:58:50 +0800 Subject: [PATCH 29/50] Update the comments of the method createQueryTo in ActiveRecordFactory so that the IDE can automatically identify the specific class of ActiveRecord --- src/ActiveRecordFactory.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ActiveRecordFactory.php b/src/ActiveRecordFactory.php index 8426fa7e4..2d9443131 100644 --- a/src/ActiveRecordFactory.php +++ b/src/ActiveRecordFactory.php @@ -29,11 +29,12 @@ public function __construct(private Factory $factory) * @throws InvalidConfigException * @throws NotFoundException * @throws NotInstantiableException + * @return ActiveRecordInterface * * @psalm-template T * @psalm-param class-string $arClass * @psalm-return T - * @psalm-suppress T + * @psalm-suppress MoreSpecificReturnType */ public function createAR( string $arClass, From 0d4ccc0831b1e9f135e7068a849006e264f667f5 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Tue, 19 Dec 2023 22:14:06 +0800 Subject: [PATCH 30/50] Update the comments of the method createQueryTo in ActiveRecordFactory so that the IDE can automatically identify the specific class of ActiveRecord --- src/ActiveRecordFactory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ActiveRecordFactory.php b/src/ActiveRecordFactory.php index 2d9443131..aa3fd917b 100644 --- a/src/ActiveRecordFactory.php +++ b/src/ActiveRecordFactory.php @@ -20,7 +20,7 @@ public function __construct(private Factory $factory) /** * Allows you to create an active record instance through the factory. * - * @param string $arClass active record class. + * @param class-string $arClass active record class. * @param string $tableName The name of the table associated with this ActiveRecord class, if its empty string the * name will be generated automatically by calling {@see getTableName()} in the active record class. * @param ConnectionInterface|null $db the database connection used for creating active record instances. @@ -34,7 +34,7 @@ public function __construct(private Factory $factory) * @psalm-template T * @psalm-param class-string $arClass * @psalm-return T - * @psalm-suppress MoreSpecificReturnType + * @psalm-suppress LessSpecificReturnStatement */ public function createAR( string $arClass, From 64b5b310a048ae8f20aba011686afae19f0272f8 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Tue, 19 Dec 2023 22:16:24 +0800 Subject: [PATCH 31/50] Update the comments of the method createQueryTo in ActiveRecordFactory so that the IDE can automatically identify the specific class of ActiveRecord --- src/ActiveRecordFactory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ActiveRecordFactory.php b/src/ActiveRecordFactory.php index aa3fd917b..d4ff33b40 100644 --- a/src/ActiveRecordFactory.php +++ b/src/ActiveRecordFactory.php @@ -20,7 +20,7 @@ public function __construct(private Factory $factory) /** * Allows you to create an active record instance through the factory. * - * @param class-string $arClass active record class. + * @param string $arClass active record class. * @param string $tableName The name of the table associated with this ActiveRecord class, if its empty string the * name will be generated automatically by calling {@see getTableName()} in the active record class. * @param ConnectionInterface|null $db the database connection used for creating active record instances. @@ -34,7 +34,7 @@ public function __construct(private Factory $factory) * @psalm-template T * @psalm-param class-string $arClass * @psalm-return T - * @psalm-suppress LessSpecificReturnStatement + * @psalm-suppress ActiveRecordInterface */ public function createAR( string $arClass, From d531911603c6719e3924e7eab14feb4a964fe568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E9=A3=8E?= Date: Thu, 28 Dec 2023 01:07:54 +0800 Subject: [PATCH 32/50] Update src/ActiveRecordFactory.php Co-authored-by: Sergei Tigrov --- src/ActiveRecordFactory.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ActiveRecordFactory.php b/src/ActiveRecordFactory.php index d4ff33b40..876487e7a 100644 --- a/src/ActiveRecordFactory.php +++ b/src/ActiveRecordFactory.php @@ -31,10 +31,9 @@ public function __construct(private Factory $factory) * @throws NotInstantiableException * @return ActiveRecordInterface * - * @psalm-template T + * @psalm-template T of ActiveRecordInterface * @psalm-param class-string $arClass * @psalm-return T - * @psalm-suppress ActiveRecordInterface */ public function createAR( string $arClass, From c62cd191d49ca4f90c45b5ea62118612e232d2d4 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sat, 30 Dec 2023 00:41:46 +0800 Subject: [PATCH 33/50] Update the comments of the method createQueryTo in ActiveRecordFactory so that the IDE can automatically identify the specific class of ActiveRecord --- src/ActiveRecordFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ActiveRecordFactory.php b/src/ActiveRecordFactory.php index 876487e7a..09f67f957 100644 --- a/src/ActiveRecordFactory.php +++ b/src/ActiveRecordFactory.php @@ -20,7 +20,7 @@ public function __construct(private Factory $factory) /** * Allows you to create an active record instance through the factory. * - * @param string $arClass active record class. + * @param class-string $arClass active record class. * @param string $tableName The name of the table associated with this ActiveRecord class, if its empty string the * name will be generated automatically by calling {@see getTableName()} in the active record class. * @param ConnectionInterface|null $db the database connection used for creating active record instances. From f488291aa6367ca624d8bb579f34057ac14d3b18 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Sat, 30 Dec 2023 00:47:57 +0800 Subject: [PATCH 34/50] Update the comments of the method createQueryTo in ActiveRecordFactory so that the IDE can automatically identify the specific class of ActiveRecord --- src/ActiveRecordFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ActiveRecordFactory.php b/src/ActiveRecordFactory.php index 09f67f957..17d525f91 100644 --- a/src/ActiveRecordFactory.php +++ b/src/ActiveRecordFactory.php @@ -20,7 +20,7 @@ public function __construct(private Factory $factory) /** * Allows you to create an active record instance through the factory. * - * @param class-string $arClass active record class. + * @param class-string $arClass active record class. * @param string $tableName The name of the table associated with this ActiveRecord class, if its empty string the * name will be generated automatically by calling {@see getTableName()} in the active record class. * @param ConnectionInterface|null $db the database connection used for creating active record instances. From 8430f4a224c25d6470525f20e61ae12460e40dd7 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Tue, 2 Jan 2024 16:48:26 +0800 Subject: [PATCH 35/50] Update the comments of the method createQueryTo in ActiveRecordFactory so that the IDE can automatically identify the specific class of ActiveRecord --- src/ActiveRecordFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ActiveRecordFactory.php b/src/ActiveRecordFactory.php index 17d525f91..876487e7a 100644 --- a/src/ActiveRecordFactory.php +++ b/src/ActiveRecordFactory.php @@ -20,7 +20,7 @@ public function __construct(private Factory $factory) /** * Allows you to create an active record instance through the factory. * - * @param class-string $arClass active record class. + * @param string $arClass active record class. * @param string $tableName The name of the table associated with this ActiveRecord class, if its empty string the * name will be generated automatically by calling {@see getTableName()} in the active record class. * @param ConnectionInterface|null $db the database connection used for creating active record instances. From 491e8f2d63d9b936b094adda93737ff7fdec4902 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Tue, 2 Jan 2024 17:08:54 +0800 Subject: [PATCH 36/50] Update the comments of the method createQueryTo in ActiveRecordFactory so that the IDE can automatically identify the specific class of ActiveRecord --- src/ActiveRecordFactory.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ActiveRecordFactory.php b/src/ActiveRecordFactory.php index 876487e7a..bfb110982 100644 --- a/src/ActiveRecordFactory.php +++ b/src/ActiveRecordFactory.php @@ -20,7 +20,7 @@ public function __construct(private Factory $factory) /** * Allows you to create an active record instance through the factory. * - * @param string $arClass active record class. + * @param class-string $arClass active record class. * @param string $tableName The name of the table associated with this ActiveRecord class, if its empty string the * name will be generated automatically by calling {@see getTableName()} in the active record class. * @param ConnectionInterface|null $db the database connection used for creating active record instances. @@ -31,8 +31,11 @@ public function __construct(private Factory $factory) * @throws NotInstantiableException * @return ActiveRecordInterface * + * @template T of ActiveRecordInterface + * @template-typeof T $arClass * @psalm-template T of ActiveRecordInterface * @psalm-param class-string $arClass + * @psalm-assert class-string $arClass * @psalm-return T */ public function createAR( From 6ce2ce3cc3d6918bb487525e658182959879dc40 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Tue, 2 Jan 2024 17:17:22 +0800 Subject: [PATCH 37/50] Update the comments of the method createQueryTo in ActiveRecordFactory so that the IDE can automatically identify the specific class of ActiveRecord --- src/ActiveQuery.php | 9 +++++++++ src/ActiveRecordFactory.php | 3 --- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/ActiveQuery.php b/src/ActiveQuery.php index 4cb8160d7..651ae80aa 100644 --- a/src/ActiveQuery.php +++ b/src/ActiveQuery.php @@ -107,6 +107,15 @@ class ActiveQuery extends Query implements ActiveQueryInterface private array $joinWith = []; private ActiveRecordInterface|null $arInstance = null; + /** + * @param string $arClass + * @param ConnectionInterface $db + * @param ActiveRecordFactory|null $arFactory + * @param string $tableName + * + * @psalm-template T of ActiveRecordInterface + * @psalm-param class-string $arClass + */ final public function __construct( protected string $arClass, protected ConnectionInterface $db, diff --git a/src/ActiveRecordFactory.php b/src/ActiveRecordFactory.php index bfb110982..09f67f957 100644 --- a/src/ActiveRecordFactory.php +++ b/src/ActiveRecordFactory.php @@ -31,11 +31,8 @@ public function __construct(private Factory $factory) * @throws NotInstantiableException * @return ActiveRecordInterface * - * @template T of ActiveRecordInterface - * @template-typeof T $arClass * @psalm-template T of ActiveRecordInterface * @psalm-param class-string $arClass - * @psalm-assert class-string $arClass * @psalm-return T */ public function createAR( From f3158ebad2a3dbe6794ef5a514d22f567eb3b443 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Tue, 2 Jan 2024 17:18:43 +0800 Subject: [PATCH 38/50] Update the comments of the method createQueryTo in ActiveRecordFactory so that the IDE can automatically identify the specific class of ActiveRecord --- src/ActiveQuery.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ActiveQuery.php b/src/ActiveQuery.php index 651ae80aa..be08d20b7 100644 --- a/src/ActiveQuery.php +++ b/src/ActiveQuery.php @@ -108,7 +108,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface private ActiveRecordInterface|null $arInstance = null; /** - * @param string $arClass + * @param class-string $arClass * @param ConnectionInterface $db * @param ActiveRecordFactory|null $arFactory * @param string $tableName From 7950258b1cb1c5eb939e5fe5b00723cecf2a1ae0 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Tue, 2 Jan 2024 17:22:54 +0800 Subject: [PATCH 39/50] Update the comments of the method createQueryTo in ActiveRecordFactory so that the IDE can automatically identify the specific class of ActiveRecord --- src/BaseActiveRecord.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index d6cb497e3..10621a408 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -320,12 +320,15 @@ public function hasAttribute($name): bool * * Call methods declared in {@see ActiveQuery} to further customize the relation. * - * @param string $class The class name of the related record + * @param class-string $class The class name of the related record * @param array $link The primary-foreign key constraint. The keys of the array refer to the attributes of the * record associated with the `$class` model, while the values of the array refer to the corresponding attributes in * **this** AR class. * * @return ActiveQueryInterface The relational query object. + * + * @psalm-template T of ActiveRecordInterface + * @psalm-param class-string $arClass */ public function hasMany(string $class, array $link): ActiveQueryInterface { @@ -356,18 +359,28 @@ public function hasMany(string $class, array $link): ActiveQueryInterface * * Call methods declared in {@see ActiveQuery} to further customize the relation. * - * @param string $class The class name of the related record. + * @param class-string $class The class name of the related record. * @param array $link The primary-foreign key constraint. The keys of the array refer to the attributes of the * record associated with the `$class` model, while the values of the array refer to the corresponding attributes in * **this** AR class. * * @return ActiveQueryInterface The relational query object. + * + * @psalm-template T of ActiveRecordInterface + * @psalm-param class-string $arClass */ public function hasOne(string $class, array $link): ActiveQueryInterface { return $this->createRelationQuery($class, $link, false); } + /** + * @param class-string $arClass + * @return ActiveQueryInterface + * + * @psalm-template T of ActiveRecordInterface + * @psalm-param class-string $arClass + */ public function instantiateQuery(string $arClass): ActiveQueryInterface { return new ActiveQuery($arClass, $this->db, $this->arFactory); @@ -1114,11 +1127,14 @@ private function setRelationDependencies( /** * Creates a query instance for `has-one` or `has-many` relation. * - * @param string $arClass The class name of the related record. + * @param class-string $arClass The class name of the related record. * @param array $link The primary-foreign key constraint. * @param bool $multiple Whether this query represents a relation to more than one record. * * @return ActiveQueryInterface The relational query object. + * + * @psalm-template T of ActiveRecordInterface + * @psalm-param class-string $arClass * {@see hasOne()} * {@see hasMany()} From 919ccb19486d8602b6dbf5bb46ed4edd69364f19 Mon Sep 17 00:00:00 2001 From: niqingyang Date: Tue, 2 Jan 2024 17:25:13 +0800 Subject: [PATCH 40/50] Update the comments of the method createQueryTo in ActiveRecordFactory so that the IDE can automatically identify the specific class of ActiveRecord --- src/ActiveRecordFactory.php | 2 +- src/BaseActiveRecord.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ActiveRecordFactory.php b/src/ActiveRecordFactory.php index 09f67f957..17d525f91 100644 --- a/src/ActiveRecordFactory.php +++ b/src/ActiveRecordFactory.php @@ -20,7 +20,7 @@ public function __construct(private Factory $factory) /** * Allows you to create an active record instance through the factory. * - * @param class-string $arClass active record class. + * @param class-string $arClass active record class. * @param string $tableName The name of the table associated with this ActiveRecord class, if its empty string the * name will be generated automatically by calling {@see getTableName()} in the active record class. * @param ConnectionInterface|null $db the database connection used for creating active record instances. diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index 10621a408..edd62910c 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -320,7 +320,7 @@ public function hasAttribute($name): bool * * Call methods declared in {@see ActiveQuery} to further customize the relation. * - * @param class-string $class The class name of the related record + * @param class-string $class The class name of the related record * @param array $link The primary-foreign key constraint. The keys of the array refer to the attributes of the * record associated with the `$class` model, while the values of the array refer to the corresponding attributes in * **this** AR class. @@ -359,7 +359,7 @@ public function hasMany(string $class, array $link): ActiveQueryInterface * * Call methods declared in {@see ActiveQuery} to further customize the relation. * - * @param class-string $class The class name of the related record. + * @param class-string $class The class name of the related record. * @param array $link The primary-foreign key constraint. The keys of the array refer to the attributes of the * record associated with the `$class` model, while the values of the array refer to the corresponding attributes in * **this** AR class. @@ -375,7 +375,7 @@ public function hasOne(string $class, array $link): ActiveQueryInterface } /** - * @param class-string $arClass + * @param class-string $arClass * @return ActiveQueryInterface * * @psalm-template T of ActiveRecordInterface @@ -1127,7 +1127,7 @@ private function setRelationDependencies( /** * Creates a query instance for `has-one` or `has-many` relation. * - * @param class-string $arClass The class name of the related record. + * @param class-string $arClass The class name of the related record. * @param array $link The primary-foreign key constraint. * @param bool $multiple Whether this query represents a relation to more than one record. * From 2991192f7d243535fb76e7ae89497618b4b14c5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E9=A3=8E?= Date: Wed, 3 Jan 2024 10:12:29 +0800 Subject: [PATCH 41/50] Update src/ActiveQuery.php Co-authored-by: Sergei Tigrov --- src/ActiveQuery.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ActiveQuery.php b/src/ActiveQuery.php index be08d20b7..651ae80aa 100644 --- a/src/ActiveQuery.php +++ b/src/ActiveQuery.php @@ -108,7 +108,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface private ActiveRecordInterface|null $arInstance = null; /** - * @param class-string $arClass + * @param string $arClass * @param ConnectionInterface $db * @param ActiveRecordFactory|null $arFactory * @param string $tableName From ec3a4665788a099178a026e2e3f2494e9f0abd9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E9=A3=8E?= Date: Wed, 3 Jan 2024 10:12:37 +0800 Subject: [PATCH 42/50] Update src/ActiveRecordFactory.php Co-authored-by: Sergei Tigrov --- src/ActiveRecordFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ActiveRecordFactory.php b/src/ActiveRecordFactory.php index 17d525f91..876487e7a 100644 --- a/src/ActiveRecordFactory.php +++ b/src/ActiveRecordFactory.php @@ -20,7 +20,7 @@ public function __construct(private Factory $factory) /** * Allows you to create an active record instance through the factory. * - * @param class-string $arClass active record class. + * @param string $arClass active record class. * @param string $tableName The name of the table associated with this ActiveRecord class, if its empty string the * name will be generated automatically by calling {@see getTableName()} in the active record class. * @param ConnectionInterface|null $db the database connection used for creating active record instances. From 236c7af8db210f595f7f5628b73891b631ba0930 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E9=A3=8E?= Date: Wed, 3 Jan 2024 10:12:47 +0800 Subject: [PATCH 43/50] Update src/BaseActiveRecord.php Co-authored-by: Sergei Tigrov --- src/BaseActiveRecord.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index edd62910c..e0b445d8e 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -359,7 +359,7 @@ public function hasMany(string $class, array $link): ActiveQueryInterface * * Call methods declared in {@see ActiveQuery} to further customize the relation. * - * @param class-string $class The class name of the related record. + * @param string $class The class name of the related record. * @param array $link The primary-foreign key constraint. The keys of the array refer to the attributes of the * record associated with the `$class` model, while the values of the array refer to the corresponding attributes in * **this** AR class. From 15e94841fed4780444c22fcba9f7094e049555df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E9=A3=8E?= Date: Wed, 3 Jan 2024 10:12:53 +0800 Subject: [PATCH 44/50] Update src/BaseActiveRecord.php Co-authored-by: Sergei Tigrov --- src/BaseActiveRecord.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index e0b445d8e..d620fa414 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -1127,7 +1127,7 @@ private function setRelationDependencies( /** * Creates a query instance for `has-one` or `has-many` relation. * - * @param class-string $arClass The class name of the related record. + * @param string $arClass The class name of the related record. * @param array $link The primary-foreign key constraint. * @param bool $multiple Whether this query represents a relation to more than one record. * From 380e4dcc2d5cf947443c8deb9aacba27fe5a2a3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E9=A3=8E?= Date: Wed, 3 Jan 2024 10:13:05 +0800 Subject: [PATCH 45/50] Update src/BaseActiveRecord.php Co-authored-by: Sergei Tigrov --- src/BaseActiveRecord.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index d620fa414..e3b91afe8 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -1133,8 +1133,7 @@ private function setRelationDependencies( * * @return ActiveQueryInterface The relational query object. * - * @psalm-template T of ActiveRecordInterface - * @psalm-param class-string $arClass + * @psalm-param class-string $arClass * {@see hasOne()} * {@see hasMany()} From 80b1a83f521b1d9c05bb0837124f64aadbbced87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E9=A3=8E?= Date: Wed, 3 Jan 2024 10:13:12 +0800 Subject: [PATCH 46/50] Update src/BaseActiveRecord.php Co-authored-by: Sergei Tigrov --- src/BaseActiveRecord.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index e3b91afe8..8d19783cd 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -375,11 +375,7 @@ public function hasOne(string $class, array $link): ActiveQueryInterface } /** - * @param class-string $arClass - * @return ActiveQueryInterface - * - * @psalm-template T of ActiveRecordInterface - * @psalm-param class-string $arClass + * @psalm-param class-string $arClass */ public function instantiateQuery(string $arClass): ActiveQueryInterface { From 040a83abc9a9662add45faf74e9f0dabf687d2ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E9=A3=8E?= Date: Wed, 3 Jan 2024 10:13:19 +0800 Subject: [PATCH 47/50] Update src/BaseActiveRecord.php Co-authored-by: Sergei Tigrov --- src/BaseActiveRecord.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index 8d19783cd..b892a2896 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -366,8 +366,7 @@ public function hasMany(string $class, array $link): ActiveQueryInterface * * @return ActiveQueryInterface The relational query object. * - * @psalm-template T of ActiveRecordInterface - * @psalm-param class-string $arClass + * @psalm-param class-string $arClass */ public function hasOne(string $class, array $link): ActiveQueryInterface { From ff9f10109f0c89180a5f6fe25dcaee22bccf16b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E9=A3=8E?= Date: Wed, 3 Jan 2024 10:13:25 +0800 Subject: [PATCH 48/50] Update src/BaseActiveRecord.php Co-authored-by: Sergei Tigrov --- src/BaseActiveRecord.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index b892a2896..e0e0d243a 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -327,8 +327,7 @@ public function hasAttribute($name): bool * * @return ActiveQueryInterface The relational query object. * - * @psalm-template T of ActiveRecordInterface - * @psalm-param class-string $arClass + * @psalm-param class-string $arClass */ public function hasMany(string $class, array $link): ActiveQueryInterface { From 801b80a9ce3b83f449584f56571f6c087cce7f9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=8C=E9=A3=8E?= Date: Wed, 3 Jan 2024 10:13:34 +0800 Subject: [PATCH 49/50] Update src/BaseActiveRecord.php Co-authored-by: Sergei Tigrov --- src/BaseActiveRecord.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index e0e0d243a..e4ef42811 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -320,7 +320,7 @@ public function hasAttribute($name): bool * * Call methods declared in {@see ActiveQuery} to further customize the relation. * - * @param class-string $class The class name of the related record + * @param string $class The class name of the related record * @param array $link The primary-foreign key constraint. The keys of the array refer to the attributes of the * record associated with the `$class` model, while the values of the array refer to the corresponding attributes in * **this** AR class. From 71b35f24efefb2e90ccdf188a209a86e1b132520 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Wed, 3 Jan 2024 14:15:45 +0700 Subject: [PATCH 50/50] Apply suggestions from code review --- src/ActiveQuery.php | 8 +------- src/BaseActiveRecord.php | 4 ++-- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/ActiveQuery.php b/src/ActiveQuery.php index 651ae80aa..23ea9ea53 100644 --- a/src/ActiveQuery.php +++ b/src/ActiveQuery.php @@ -108,13 +108,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface private ActiveRecordInterface|null $arInstance = null; /** - * @param string $arClass - * @param ConnectionInterface $db - * @param ActiveRecordFactory|null $arFactory - * @param string $tableName - * - * @psalm-template T of ActiveRecordInterface - * @psalm-param class-string $arClass + * @psalm-param class-string $arClass */ final public function __construct( protected string $arClass, diff --git a/src/BaseActiveRecord.php b/src/BaseActiveRecord.php index e4ef42811..c3e2a751a 100644 --- a/src/BaseActiveRecord.php +++ b/src/BaseActiveRecord.php @@ -327,7 +327,7 @@ public function hasAttribute($name): bool * * @return ActiveQueryInterface The relational query object. * - * @psalm-param class-string $arClass + * @psalm-param class-string $class */ public function hasMany(string $class, array $link): ActiveQueryInterface { @@ -365,7 +365,7 @@ public function hasMany(string $class, array $link): ActiveQueryInterface * * @return ActiveQueryInterface The relational query object. * - * @psalm-param class-string $arClass + * @psalm-param class-string $class */ public function hasOne(string $class, array $link): ActiveQueryInterface {