-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement the interface ArrayableInterface for BaseActiveRecord (#273)
Co-authored-by: Sergei Tigrov <[email protected]>
- Loading branch information
1 parent
41c6691
commit 3bed01c
Showing
6 changed files
with
121 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -763,4 +764,58 @@ 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' => '[email protected]', | ||
'name' => 'user1', | ||
'address' => 'address1', | ||
'status' => 'active', | ||
'item' => [ | ||
'id' => 2, | ||
'email' => '[email protected]', | ||
'name' => 'user2', | ||
'status' => 'active', | ||
], | ||
'items' => [ | ||
[ | ||
'id' => 3, | ||
'email' => '[email protected]', | ||
'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', | ||
]), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord; | ||
|
||
use Yiisoft\ActiveRecord\ActiveRecord; | ||
|
||
/** | ||
* Class CustomerClosureField. | ||
* | ||
* @property int $id | ||
* @property string $name | ||
* @property string $email | ||
* @property string $address | ||
* @property int $status | ||
*/ | ||
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(self $item) | ||
{ | ||
$this->item = $item; | ||
} | ||
|
||
public function setItems(self ...$items) | ||
{ | ||
$this->items = $items; | ||
} | ||
|
||
public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array | ||
{ | ||
$data = parent::toArray($fields, $expand, $recursive); | ||
|
||
$data['status'] = $this->status == 1 ? 'active' : 'inactive'; | ||
|
||
return $data; | ||
} | ||
} |