Skip to content

Commit

Permalink
implement the interface ArrayableInterface for BaseActiveRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
niqingyang committed Dec 9, 2023
1 parent 3988dcc commit 424f841
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/BaseActiveRecordTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
38 changes: 36 additions & 2 deletions tests/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand All @@ -780,9 +789,34 @@ public function testToArrayForArrayable(): void
'name' => 'user1',
'address' => 'address1',
'status' => 'active',
'profile_id' => 1,
'item' => [
'id' => 2,
'name' => 'user2',
'email' => '[email protected]',
'status' => 'active',
],
'items' => [
[
'id' => 3,
'email' => '[email protected]',
'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',
]),
);
}
}
39 changes: 36 additions & 3 deletions tests/Driver/Oracle/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand All @@ -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' => '[email protected]',
'status' => 'active',
],
'items' => [
[
'id' => 3,
'email' => '[email protected]',
'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',
]),
);
}
}
39 changes: 36 additions & 3 deletions tests/Driver/Pgsql/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand All @@ -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' => '[email protected]',
'status' => 'active',
],
'items' => [
[
'id' => 3,
'email' => '[email protected]',
'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',
]),
);
}
}
27 changes: 26 additions & 1 deletion tests/Stubs/ActiveRecord/CustomerForArrayable.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord;

use Yiisoft\ActiveRecord\ActiveRecord;
Expand All @@ -15,11 +17,34 @@
*/
class CustomerForArrayable extends ActiveRecord
{
public array $items = [];

public ?CustomerForArrayable $item = null;

public function getTableName(): string
{
return 'customer';
}

public function fields(): array
{
$fields = parent::fields();

$fields['item'] = 'item';
$fields['items'] = 'items';

return $fields;
}

public function setItem(CustomerForArrayable $item) {
$this->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);
Expand All @@ -28,4 +53,4 @@ public function toArray(array $fields = [], array $expand = [], bool $recursive

return $data;
}
}
}

0 comments on commit 424f841

Please sign in to comment.