实体导出数组

Testing Is Documentation

tests/Database/Ddd/EntityToArrayTest.php实体导出数组 - 图1

我们可以将实体导出为数组来方便处理数据。

Uses

  1. <?php
  2. use Leevel\Database\Ddd\Entity;
  3. use Tests\Database\DatabaseTestCase as TestCase;
  4. use Tests\Database\Ddd\Entity\DemoToArrayBlackEntity;
  5. use Tests\Database\Ddd\Entity\DemoToArrayEntity;
  6. use Tests\Database\Ddd\Entity\DemoToArrayShowPropNullEntity;
  7. use Tests\Database\Ddd\Entity\DemoToArrayShowPropNullRelationEntity;
  8. use Tests\Database\Ddd\Entity\DemoToArrayShowPropNullRelationTargetEntity;
  9. use Tests\Database\Ddd\Entity\DemoToArrayWhiteEntity;
  10. use Tests\Database\Ddd\Entity\Relation\Post;
  11. use Tests\Database\Ddd\Entity\Relation\User;

toArray 基本使用方法

fixture 定义

  1. # Tests\Database\Ddd\EntityToArrayTest::makeEntity
  2. protected function makeEntity(): DemoToArrayEntity
  3. {
  4. $entity = new DemoToArrayEntity();
  5. $this->assertInstanceof(Entity::class, $entity);
  6. $entity->name = '实体名字';
  7. $entity->description = 'goods name';
  8. $entity->address = '四川成都';
  9. $entity->foo_bar = 'foo';
  10. $entity->hello = 'hello world';
  11. return $entity;
  12. }

Tests\Database\Ddd\Entity\DemoToArrayEntity

  1. namespace Tests\Database\Ddd\Entity;
  2. use Leevel\Database\Ddd\Entity;
  3. use Leevel\Database\Ddd\GetterSetter;
  4. class DemoToArrayEntity extends Entity
  5. {
  6. use GetterSetter;
  7. const TABLE = 'test';
  8. const ID = 'id';
  9. const AUTO = 'id';
  10. const STRUCT = [
  11. 'id' => [],
  12. 'name' => [],
  13. 'description' => [],
  14. 'address' => [],
  15. 'foo_bar' => [],
  16. 'hello' => [],
  17. ];
  18. }
  1. public function testBaseUse(): void
  2. {
  3. $entity = $this->makeEntity();
  4. $data = <<<'eot'
  5. {
  6. "name": "实体名字",
  7. "description": "goods name",
  8. "address": "四川成都",
  9. "foo_bar": "foo",
  10. "hello": "hello world"
  11. }
  12. eot;
  13. $this->assertSame(
  14. $data,
  15. $this->varJson(
  16. $entity->toArray()
  17. )
  18. );
  19. }

toArray 实体对象转数组支持白名单

toArray 第一个参数为白名单,设置了白名单,只有白名单的字段才能够转换为数组数据。

  1. public function testWithWhite(): void
  2. {
  3. $entity = $this->makeEntity();
  4. $data = <<<'eot'
  5. {
  6. "name": "实体名字",
  7. "description": "goods name",
  8. "address": "四川成都",
  9. "foo_bar": "foo",
  10. "hello": "hello world"
  11. }
  12. eot;
  13. $this->assertSame(
  14. $data,
  15. $this->varJson(
  16. $entity->toArray()
  17. )
  18. );
  19. $data = <<<'eot'
  20. {
  21. "name": "实体名字"
  22. }
  23. eot;
  24. $this->assertSame(
  25. $data,
  26. $this->varJson(
  27. $entity->toArray(['name']),
  28. 1
  29. )
  30. );
  31. $data = <<<'eot'
  32. {
  33. "name": "实体名字",
  34. "description": "goods name"
  35. }
  36. eot;
  37. $this->assertSame(
  38. $data,
  39. $this->varJson(
  40. $entity->toArray(['name', 'description']),
  41. 2
  42. )
  43. );
  44. $data = <<<'eot'
  45. {
  46. "name": "实体名字",
  47. "description": "goods name",
  48. "hello": "hello world"
  49. }
  50. eot;
  51. $this->assertSame(
  52. $data,
  53. $this->varJson(
  54. $entity->toArray(['name', 'description', 'hello']),
  55. 3
  56. )
  57. );
  58. }

toArray 实体对象转数组支持黑名单

toArray 第二个参数为白名单,设置了黑名单但是没有设置白名单,只有不属于黑名单的字段才能够转换为数组数据。

  1. public function testWithBlack(): void
  2. {
  3. $entity = $this->makeEntity();
  4. $data = <<<'eot'
  5. {
  6. "name": "实体名字",
  7. "description": "goods name",
  8. "address": "四川成都",
  9. "foo_bar": "foo",
  10. "hello": "hello world"
  11. }
  12. eot;
  13. $this->assertSame(
  14. $data,
  15. $this->varJson(
  16. $entity->toArray()
  17. )
  18. );
  19. $data = <<<'eot'
  20. {
  21. "description": "goods name",
  22. "address": "四川成都",
  23. "foo_bar": "foo",
  24. "hello": "hello world"
  25. }
  26. eot;
  27. $this->assertSame(
  28. $data,
  29. $this->varJson(
  30. $entity->toArray([], ['name']),
  31. 1
  32. )
  33. );
  34. $data = <<<'eot'
  35. {
  36. "address": "四川成都",
  37. "foo_bar": "foo",
  38. "hello": "hello world"
  39. }
  40. eot;
  41. $this->assertSame(
  42. $data,
  43. $this->varJson(
  44. $entity->toArray([], ['name', 'description']),
  45. 2
  46. )
  47. );
  48. $data = <<<'eot'
  49. {
  50. "description": "goods name",
  51. "hello": "hello world"
  52. }
  53. eot;
  54. $this->assertSame(
  55. $data,
  56. $this->varJson(
  57. $entity->toArray([], ['foo_bar', 'name', 'address']),
  58. 3
  59. )
  60. );
  61. }

toArray 实体对象转数组支持字段设置为白名单

可以通过 STRUCT 中的定义 \Leevel\Database\Ddd\Entity::SHOW_PROP_WHITE 来设置字段白名单。

值得注意的是, toArray 的第一个参数白名单优先级更高。

如果设置了白名单,只有白名单的字段才能够转换为数组数据。

fixture 定义

  1. # Tests\Database\Ddd\EntityToArrayTest::makeWhiteEntity
  2. protected function makeWhiteEntity(): DemoToArrayWhiteEntity
  3. {
  4. $entity = new DemoToArrayWhiteEntity();
  5. $this->assertInstanceof(Entity::class, $entity);
  6. $entity->name = '实体名字';
  7. $entity->description = 'goods name';
  8. $entity->address = '四川成都';
  9. $entity->foo_bar = 'foo';
  10. $entity->hello = 'hello world';
  11. return $entity;
  12. }

Tests\Database\Ddd\Entity\DemoToArrayWhiteEntity

  1. namespace Tests\Database\Ddd\Entity;
  2. use Leevel\Database\Ddd\Entity;
  3. use Leevel\Database\Ddd\GetterSetter;
  4. class DemoToArrayWhiteEntity extends Entity
  5. {
  6. use GetterSetter;
  7. const TABLE = 'test';
  8. const ID = 'id';
  9. const AUTO = 'id';
  10. const STRUCT = [
  11. 'id' => [],
  12. 'name' => [],
  13. 'description' => [
  14. self::SHOW_PROP_WHITE => true,
  15. ],
  16. 'address' => [],
  17. 'foo_bar' => [
  18. self::SHOW_PROP_WHITE => true,
  19. ],
  20. 'hello' => [],
  21. ];
  22. }
  1. public function testWithWhiteEntity(): void
  2. {
  3. $entity = $this->makeWhiteEntity();
  4. $data = <<<'eot'
  5. {
  6. "description": "goods name",
  7. "foo_bar": "foo"
  8. }
  9. eot;
  10. $this->assertSame(
  11. $data,
  12. $this->varJson(
  13. $entity->toArray()
  14. )
  15. );
  16. }

toArray 实体对象转数组支持字段设置为黑名单

可以通过 STRUCT 中的定义 \Leevel\Database\Ddd\Entity::SHOW_PROP_BLACK 来设置字段黑名单。

值得注意的是, toArray 的第二个参数黑名单优先级更高。

如果设置了黑名单,设置了黑名单但是没有设置白名单,只有不属于黑名单的字段才能够转换为数组数据。

fixture 定义

  1. # Tests\Database\Ddd\EntityToArrayTest::makeBlackEntity
  2. protected function makeBlackEntity(): DemoToArrayBlackEntity
  3. {
  4. $entity = new DemoToArrayBlackEntity();
  5. $this->assertInstanceof(Entity::class, $entity);
  6. $entity->name = '实体名字';
  7. $entity->description = 'goods name';
  8. $entity->address = '四川成都';
  9. $entity->foo_bar = 'foo';
  10. $entity->hello = 'hello world';
  11. return $entity;
  12. }

Tests\Database\Ddd\Entity\DemoToArrayBlackEntity

  1. namespace Tests\Database\Ddd\Entity;
  2. use Leevel\Database\Ddd\Entity;
  3. use Leevel\Database\Ddd\GetterSetter;
  4. class DemoToArrayBlackEntity extends Entity
  5. {
  6. use GetterSetter;
  7. const TABLE = 'test';
  8. const ID = 'id';
  9. const AUTO = 'id';
  10. const STRUCT = [
  11. 'id' => [],
  12. 'name' => [],
  13. 'description' => [
  14. self::SHOW_PROP_BLACK => true,
  15. ],
  16. 'address' => [],
  17. 'foo_bar' => [
  18. self::SHOW_PROP_BLACK => true,
  19. ],
  20. 'hello' => [],
  21. ];
  22. }
  1. public function testWithBlackEntity(): void
  2. {
  3. $entity = $this->makeBlackEntity();
  4. $data = <<<'eot'
  5. {
  6. "name": "实体名字",
  7. "address": "四川成都",
  8. "hello": "hello world"
  9. }
  10. eot;
  11. $this->assertSame(
  12. $data,
  13. $this->varJson(
  14. $entity->toArray()
  15. )
  16. );
  17. }

toArray 实体对象转数组支持转换关联实体数据

fixture 定义

  1. # Tests\Database\Ddd\EntityToArrayTest::makeRelationEntity
  2. protected function makeRelationEntity(): Post
  3. {
  4. $user = new User(['id' => 7]);
  5. $user->name = 'xiaoniuge';
  6. $entity = new Post(['id' => 5]);
  7. $this->assertInstanceof(Post::class, $entity);
  8. $entity->title = 'I am title';
  9. $entity->summary = 'I am summary';
  10. $entity->userId = 7;
  11. $entity->withRelationProp('user', $user);
  12. return $entity;
  13. }

Tests\Database\Ddd\Entity\Relation\User

  1. namespace Tests\Database\Ddd\Entity\Relation;
  2. use Leevel\Database\Ddd\Entity;
  3. use Leevel\Database\Ddd\GetterSetter;
  4. use Leevel\Database\Ddd\Relation\ManyMany;
  5. class User extends Entity
  6. {
  7. use GetterSetter;
  8. const TABLE = 'user';
  9. const ID = 'id';
  10. const AUTO = 'id';
  11. const STRUCT = [
  12. 'id' => [],
  13. 'name' => [],
  14. 'create_at' => [],
  15. 'role' => [
  16. self::MANY_MANY => Role::class,
  17. self::MIDDLE_ENTITY => UserRole::class,
  18. self::SOURCE_KEY => 'id',
  19. self::TARGET_KEY => 'id',
  20. self::MIDDLE_SOURCE_KEY => 'user_id',
  21. self::MIDDLE_TARGET_KEY => 'role_id',
  22. ],
  23. 'role_soft_deleted' => [
  24. self::MANY_MANY => RoleSoftDeleted::class,
  25. self::MIDDLE_ENTITY => UserRoleSoftDeleted::class,
  26. self::SOURCE_KEY => 'id',
  27. self::TARGET_KEY => 'id',
  28. self::MIDDLE_SOURCE_KEY => 'user_id',
  29. self::MIDDLE_TARGET_KEY => 'role_id',
  30. ],
  31. 'role_middle_with_soft_deleted' => [
  32. self::MANY_MANY => RoleSoftDeleted::class,
  33. self::MIDDLE_ENTITY => UserRoleSoftDeleted::class,
  34. self::SOURCE_KEY => 'id',
  35. self::TARGET_KEY => 'id',
  36. self::MIDDLE_SOURCE_KEY => 'user_id',
  37. self::MIDDLE_TARGET_KEY => 'role_id',
  38. self::RELATION_SCOPE => 'withSoftDeleted',
  39. ],
  40. 'role_middle_only_soft_deleted' => [
  41. self::MANY_MANY => RoleSoftDeleted::class,
  42. self::MIDDLE_ENTITY => UserRoleSoftDeleted::class,
  43. self::SOURCE_KEY => 'id',
  44. self::TARGET_KEY => 'id',
  45. self::MIDDLE_SOURCE_KEY => 'user_id',
  46. self::MIDDLE_TARGET_KEY => 'role_id',
  47. self::RELATION_SCOPE => 'onlySoftDeleted',
  48. ],
  49. 'role_relation_scope_not_found' => [
  50. self::MANY_MANY => RoleSoftDeleted::class,
  51. self::MIDDLE_ENTITY => UserRoleSoftDeleted::class,
  52. self::SOURCE_KEY => 'id',
  53. self::TARGET_KEY => 'id',
  54. self::MIDDLE_SOURCE_KEY => 'user_id',
  55. self::MIDDLE_TARGET_KEY => 'role_id',
  56. self::RELATION_SCOPE => 'notFound',
  57. ],
  58. 'role_relation_scope_found_but_private' => [
  59. self::MANY_MANY => RoleSoftDeleted::class,
  60. self::MIDDLE_ENTITY => UserRoleSoftDeleted::class,
  61. self::SOURCE_KEY => 'id',
  62. self::TARGET_KEY => 'id',
  63. self::MIDDLE_SOURCE_KEY => 'user_id',
  64. self::MIDDLE_TARGET_KEY => 'role_id',
  65. self::RELATION_SCOPE => 'foundButPrivate',
  66. ],
  67. 'role_not_defined_middle_entity' => [
  68. self::MANY_MANY => Role::class,
  69. self::SOURCE_KEY => 'id',
  70. self::TARGET_KEY => 'id',
  71. self::MIDDLE_SOURCE_KEY => 'user_id',
  72. self::MIDDLE_TARGET_KEY => 'role_id',
  73. ],
  74. 'role_not_defined_source_key' => [
  75. self::MANY_MANY => Role::class,
  76. self::MIDDLE_ENTITY => UserRole::class,
  77. self::TARGET_KEY => 'id',
  78. self::MIDDLE_SOURCE_KEY => 'user_id',
  79. self::MIDDLE_TARGET_KEY => 'role_id',
  80. ],
  81. 'role_not_defined_target_key' => [
  82. self::MANY_MANY => Role::class,
  83. self::MIDDLE_ENTITY => UserRole::class,
  84. self::SOURCE_KEY => 'id',
  85. self::MIDDLE_SOURCE_KEY => 'user_id',
  86. self::MIDDLE_TARGET_KEY => 'role_id',
  87. ],
  88. 'role_not_defined_middle_source_key' => [
  89. self::MANY_MANY => Role::class,
  90. self::MIDDLE_ENTITY => UserRole::class,
  91. self::SOURCE_KEY => 'id',
  92. self::TARGET_KEY => 'id',
  93. self::MIDDLE_TARGET_KEY => 'role_id',
  94. ],
  95. 'role_not_defined_middle_target_key' => [
  96. self::MANY_MANY => Role::class,
  97. self::MIDDLE_ENTITY => UserRole::class,
  98. self::SOURCE_KEY => 'id',
  99. self::TARGET_KEY => 'id',
  100. self::MIDDLE_SOURCE_KEY => 'user_id',
  101. ],
  102. 'role_middle_field' => [
  103. self::MANY_MANY => Role::class,
  104. self::MIDDLE_ENTITY => UserRole::class,
  105. self::SOURCE_KEY => 'id',
  106. self::TARGET_KEY => 'id',
  107. self::MIDDLE_SOURCE_KEY => 'user_id',
  108. self::MIDDLE_TARGET_KEY => 'role_id',
  109. self::RELATION_SCOPE => 'middleField',
  110. ],
  111. 'role_middle_only_soft_deleted_and_middle_field_and_other_table_condition' => [
  112. self::MANY_MANY => RoleSoftDeleted::class,
  113. self::MIDDLE_ENTITY => UserRoleSoftDeleted::class,
  114. self::SOURCE_KEY => 'id',
  115. self::TARGET_KEY => 'id',
  116. self::MIDDLE_SOURCE_KEY => 'user_id',
  117. self::MIDDLE_TARGET_KEY => 'role_id',
  118. self::RELATION_SCOPE => 'middleOnlySoftDeletedAndMiddleFieldAndOtherTableCondition',
  119. ],
  120. ];
  121. protected function relationScopeWithSoftDeleted(ManyMany $relation): void
  122. {
  123. $relation->middleWithSoftDeleted();
  124. }
  125. protected function relationScopeOnlySoftDeleted(ManyMany $relation): void
  126. {
  127. $relation->middleOnlySoftDeleted();
  128. }
  129. protected function relationScopeMiddleField(ManyMany $relation): void
  130. {
  131. $relation->middleField(['create_at', 'middle_id' => 'id']);
  132. }
  133. protected function relationScopeMiddleOnlySoftDeletedAndMiddleFieldAndOtherTableCondition(ManyMany $relation): void
  134. {
  135. $relation
  136. ->middleOnlySoftDeleted()
  137. ->middleField(['create_at', 'middle_id' => 'id'])
  138. ->setColumns('id,name')
  139. ->where('id', '>', 3);
  140. }
  141. private function relationScopeFoundButPrivate(ManyMany $relation): void
  142. {
  143. }
  144. }

Tests\Database\Ddd\Entity\Relation\Post

  1. namespace Tests\Database\Ddd\Entity\Relation;
  2. use Leevel\Database\Ddd\Entity;
  3. use Leevel\Database\Ddd\GetterSetter;
  4. use Leevel\Database\Ddd\Relation\Relation;
  5. class Post extends Entity
  6. {
  7. use GetterSetter;
  8. const TABLE = 'post';
  9. const ID = 'id';
  10. const AUTO = 'id';
  11. const STRUCT = [
  12. 'id' => [
  13. self::READONLY => true,
  14. ],
  15. 'title' => [],
  16. 'user_id' => [],
  17. 'summary' => [],
  18. 'create_at' => [],
  19. 'delete_at' => [
  20. self::CREATE_FILL => 0,
  21. ],
  22. 'user' => [
  23. self::BELONGS_TO => User::class,
  24. self::SOURCE_KEY => 'user_id',
  25. self::TARGET_KEY => 'id',
  26. ],
  27. 'comment' => [
  28. self::HAS_MANY => Comment::class,
  29. self::SOURCE_KEY => 'id',
  30. self::TARGET_KEY => 'post_id',
  31. self::RELATION_SCOPE => 'comment',
  32. ],
  33. 'post_content' => [
  34. self::HAS_ONE => PostContent::class,
  35. self::SOURCE_KEY => 'id',
  36. self::TARGET_KEY => 'post_id',
  37. ],
  38. 'user_not_defined_source_key' => [
  39. self::BELONGS_TO => User::class,
  40. self::TARGET_KEY => 'id',
  41. ],
  42. 'user_not_defined_target_key' => [
  43. self::BELONGS_TO => User::class,
  44. self::SOURCE_KEY => 'id',
  45. ],
  46. 'comment_not_defined_source_key' => [
  47. self::HAS_MANY => Comment::class,
  48. self::TARGET_KEY => 'post_id',
  49. self::RELATION_SCOPE => 'comment',
  50. ],
  51. 'comment_not_defined_target_key' => [
  52. self::HAS_MANY => Comment::class,
  53. self::SOURCE_KEY => 'id',
  54. self::RELATION_SCOPE => 'comment',
  55. ],
  56. 'post_content_not_defined_source_key' => [
  57. self::HAS_ONE => PostContent::class,
  58. self::TARGET_KEY => 'post_id',
  59. ],
  60. 'post_content_not_defined_target_key' => [
  61. self::HAS_ONE => PostContent::class,
  62. self::SOURCE_KEY => 'id',
  63. ],
  64. ];
  65. const DELETE_AT = 'delete_at';
  66. protected function relationScopeComment(Relation $relation): void
  67. {
  68. $relation->where('id', '>', 4);
  69. }
  70. }
  1. public function testWithRelation(): void
  2. {
  3. $entity = $this->makeRelationEntity();
  4. $data = <<<'eot'
  5. {
  6. "id": 5,
  7. "title": "I am title",
  8. "user_id": 7,
  9. "summary": "I am summary",
  10. "user": {
  11. "id": 7,
  12. "name": "xiaoniuge"
  13. }
  14. }
  15. eot;
  16. $this->assertSame(
  17. $data,
  18. $this->varJson(
  19. $entity->toArray()
  20. )
  21. );
  22. }

toArray 实体对象转数组支持转换关联实体数据(黑白名单)

toArray 第三个参数为关联实体的黑白名单。

  1. public function testWithRelationWhiteAndBlack(): void
  2. {
  3. $entity = $this->makeRelationEntity();
  4. $data = <<<'eot'
  5. {
  6. "id": 5,
  7. "title": "I am title",
  8. "user_id": 7,
  9. "summary": "I am summary",
  10. "user": {
  11. "name": "xiaoniuge"
  12. }
  13. }
  14. eot;
  15. $this->assertSame(
  16. $data,
  17. $this->varJson(
  18. $entity->toArray([], [], ['user' => [['name']]])
  19. )
  20. );
  21. }

toArray 实体对象转数组支持 NULL 值字段默认指定数据

fixture 定义

  1. # Tests\Database\Ddd\EntityToArrayTest::makeShowPropNullEntity
  2. protected function makeShowPropNullEntity(): DemoToArrayShowPropNullEntity
  3. {
  4. $entity = new DemoToArrayShowPropNullEntity();
  5. $this->assertInstanceof(Entity::class, $entity);
  6. $entity->name = '实体名字';
  7. $entity->description = 'goods name';
  8. return $entity;
  9. }

Tests\Database\Ddd\Entity\DemoToArrayShowPropNullEntity

  1. namespace Tests\Database\Ddd\Entity;
  2. use Leevel\Database\Ddd\Entity;
  3. use Leevel\Database\Ddd\GetterSetter;
  4. class DemoToArrayShowPropNullEntity extends Entity
  5. {
  6. use GetterSetter;
  7. const TABLE = 'test';
  8. const ID = 'id';
  9. const AUTO = 'id';
  10. const STRUCT = [
  11. 'id' => [],
  12. 'name' => [],
  13. 'description' => [],
  14. 'address' => [
  15. self::SHOW_PROP_NULL => '',
  16. ],
  17. 'foo_bar' => [
  18. self::SHOW_PROP_NULL => null,
  19. ],
  20. 'hello' => [
  21. self::SHOW_PROP_NULL => 'default_value',
  22. ],
  23. ];
  24. }
  1. public function testWithShowPropNull(): void
  2. {
  3. $entity = $this->makeShowPropNullEntity();
  4. $data = <<<'eot'
  5. {
  6. "name": "实体名字",
  7. "description": "goods name",
  8. "address": "",
  9. "foo_bar": null,
  10. "hello": "default_value"
  11. }
  12. eot;
  13. $this->assertSame(
  14. $data,
  15. $this->varJson(
  16. $entity->toArray()
  17. )
  18. );
  19. }

toArray 实体对象转数组支持 NULL 值字段默认指定数据(关联模型)

fixture 定义

  1. # Tests\Database\Ddd\EntityToArrayTest::makeRelationShowPropNullEntity
  2. protected function makeRelationShowPropNullEntity(): DemoToArrayShowPropNullRelationEntity
  3. {
  4. $target = new DemoToArrayShowPropNullRelationTargetEntity(['id' => 5]);
  5. $target->name = 'xiaoniuge';
  6. $entity = new DemoToArrayShowPropNullRelationEntity(['id' => 5]);
  7. $this->assertInstanceof(DemoToArrayShowPropNullRelationEntity::class, $entity);
  8. $entity->name = 'I am name';
  9. $entity->description = 'I am description';
  10. $entity->withRelationProp('target', $target);
  11. return $entity;
  12. }

Tests\Database\Ddd\Entity\DemoToArrayShowPropNullRelationTargetEntity

  1. namespace Tests\Database\Ddd\Entity;
  2. use Leevel\Database\Ddd\Entity;
  3. use Leevel\Database\Ddd\GetterSetter;
  4. class DemoToArrayShowPropNullRelationTargetEntity extends Entity
  5. {
  6. use GetterSetter;
  7. const TABLE = 'test';
  8. const ID = 'id';
  9. const AUTO = 'id';
  10. const STRUCT = [
  11. 'id' => [],
  12. 'name' => [],
  13. ];
  14. }

Tests\Database\Ddd\Entity\DemoToArrayShowPropNullRelationEntity

  1. namespace Tests\Database\Ddd\Entity;
  2. use Leevel\Database\Ddd\Entity;
  3. use Leevel\Database\Ddd\GetterSetter;
  4. class DemoToArrayShowPropNullRelationEntity extends Entity
  5. {
  6. use GetterSetter;
  7. const TABLE = 'test';
  8. const ID = 'id';
  9. const AUTO = 'id';
  10. const STRUCT = [
  11. 'id' => [],
  12. 'name' => [],
  13. 'description' => [],
  14. 'address' => [
  15. self::SHOW_PROP_NULL => '',
  16. ],
  17. 'foo_bar' => [
  18. self::SHOW_PROP_NULL => null,
  19. ],
  20. 'hello' => [
  21. self::SHOW_PROP_NULL => 'default_value',
  22. ],
  23. 'target' => [
  24. self::HAS_ONE => DemoToArrayShowPropNullRelationTargetEntity::class,
  25. self::SOURCE_KEY => 'id',
  26. self::TARGET_KEY => 'id',
  27. ],
  28. ];
  29. }
  1. public function testWithShowPropNullForRelation(): void
  2. {
  3. $entity = $this->makeRelationShowPropNullEntity();
  4. $data = <<<'eot'
  5. {
  6. "id": 5,
  7. "name": "I am name",
  8. "description": "I am description",
  9. "address": "",
  10. "foo_bar": null,
  11. "hello": "default_value",
  12. "target": {
  13. "id": 5,
  14. "name": "xiaoniuge"
  15. }
  16. }
  17. eot;
  18. $this->assertSame(
  19. $data,
  20. $this->varJson(
  21. $entity->toArray()
  22. )
  23. );
  24. }