保存实体

Testing Is Documentation

tests/Database/Ddd/Create/CreateTest.php保存实体 - 图1

将实体持久化到数据库。

Uses

  1. <?php
  2. use Leevel\Database\Ddd\Entity;
  3. use Tests\Database\DatabaseTestCase as TestCase;
  4. use Tests\Database\Ddd\Entity\CompositeId;
  5. use Tests\Database\Ddd\Entity\DemoConstructPropBlackEntity;
  6. use Tests\Database\Ddd\Entity\DemoConstructPropWhiteEntity;
  7. use Tests\Database\Ddd\Entity\DemoCreateAutoFillEntity;
  8. use Tests\Database\Ddd\Entity\DemoCreatePropWhiteEntity;
  9. use Tests\Database\Ddd\Entity\DemoDatabaseEntity;
  10. use Tests\Database\Ddd\Entity\DemoEntity;

save 创建一个实体

没有主键数据,则可以通过 save 方法创建一个实体。

完整例子

  1. $entity = new DemoEntity();
  2. $entity->name = 'foo';
  3. $entity->save()->flush();

调用 save 方法并没有立刻真正持久化到数据库,这一个步骤计算好了待保存的数据。

完整模型

  1. namespace Tests\Database\Ddd\Entity;
  2. use Leevel\Database\Ddd\Entity;
  3. use Leevel\Database\Ddd\GetterSetter;
  4. class DemoEntity extends Entity
  5. {
  6. use GetterSetter;
  7. const TABLE = 'test';
  8. const ID = 'id';
  9. const AUTO = 'id';
  10. const STRUCT = [
  11. 'id' => [
  12. self::READONLY => true,
  13. ],
  14. 'name' => [],
  15. ];
  16. }
  1. public function testBaseUse(): void
  2. {
  3. $entity = new DemoEntity();
  4. $entity->name = 'foo';
  5. $this->assertInstanceof(Entity::class, $entity);
  6. $this->assertSame('foo', $entity->name);
  7. $this->assertSame(['name'], $entity->changed());
  8. $this->assertNull($entity->flushData());
  9. $entity->save();
  10. $data = <<<'eot'
  11. [
  12. {
  13. "name": "foo"
  14. }
  15. ]
  16. eot;
  17. $this->assertSame(
  18. $data,
  19. $this->varJson(
  20. $entity->flushData()
  21. )
  22. );
  23. }

TIP

通过 save 方法保存一个实体,并通过 flush 将实体持久化到数据库。

create 创建一个实体

  1. public function testCreateBaseUse(): void
  2. {
  3. $entity = new DemoEntity();
  4. $entity->name = 'foo';
  5. $this->assertInstanceof(Entity::class, $entity);
  6. $this->assertSame('foo', $entity->name);
  7. $this->assertSame(['name'], $entity->changed());
  8. $this->assertNull($entity->flushData());
  9. $entity->create();
  10. $data = <<<'eot'
  11. [
  12. {
  13. "name": "foo"
  14. }
  15. ]
  16. eot;
  17. $this->assertSame(
  18. $data,
  19. $this->varJson(
  20. $entity->flushData()
  21. )
  22. );
  23. }

TIP

通过 create 方法保存一个实体,并通过 flush 将实体持久化到数据库。

创建一个实体支持构造器白名单

完整模型

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

调用 \Leevel\Database\Ddd\Entity::CONSTRUCT_PROP_WHITE => true 来设置字段白名单,一旦设置了构造器白名单只有通过了白名单的字段才能够更新模型属性。

  1. public function testConsturctPropWhite(): void
  2. {
  3. $entity = new DemoConstructPropWhiteEntity([
  4. 'id' => 5,
  5. 'name' => 'foo',
  6. ]);
  7. $this->assertSame(5, $entity->getId());
  8. $this->assertNull($entity->getName());
  9. }

创建一个实体支持构造器黑名单

完整模型

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

调用 \Leevel\Database\Ddd\Entity::CONSTRUCT_PROP_BLACK => true 来设置字段黑名单,一旦设置了构造器黑名单处于黑名单的字段无法更新模型属性。

  1. public function testConsturctPropBlack(): void
  2. {
  3. $entity = new DemoConstructPropBlackEntity([
  4. 'id' => 5,
  5. 'name' => 'foo',
  6. ]);
  7. $this->assertNull($entity->getId());
  8. $this->assertSame('foo', $entity->getName());
  9. }

创建一个实体支持创建属性白名单

完整模型

  1. namespace Tests\Database\Ddd\Entity;
  2. use Leevel\Database\Ddd\Entity;
  3. use Leevel\Database\Ddd\GetterSetter;
  4. class DemoCreatePropWhiteEntity extends Entity
  5. {
  6. use GetterSetter;
  7. const TABLE = 'test';
  8. const ID = 'id';
  9. const AUTO = 'id';
  10. const STRUCT = [
  11. 'id' => [
  12. self::READONLY => true,
  13. ],
  14. 'name' => [
  15. self::CREATE_PROP_WHITE => true,
  16. ],
  17. 'description' => [],
  18. ];
  19. }

调用 \Leevel\Database\Ddd\Entity::CREATE_PROP_WHITE => true 来设置字段白名单,一旦设置了创建属性白名单只有通过了白名单的字段才能够更新模型属性。

  1. public function testSavePropBlackAndWhite(): void
  2. {
  3. $entity = new DemoCreatePropWhiteEntity([
  4. 'name' => 'foo',
  5. 'description' => 'hello description',
  6. ]);
  7. $entity->save();
  8. $data = <<<'eot'
  9. [
  10. {
  11. "name": "foo"
  12. }
  13. ]
  14. eot;
  15. $this->assertSame(
  16. $data,
  17. $this->varJson(
  18. $entity->flushData()
  19. )
  20. );
  21. }

fill 设置允许自动填充字段

完整模型

  1. namespace Tests\Database\Ddd\Entity;
  2. use Leevel\Database\Ddd\Entity;
  3. use Leevel\Database\Ddd\GetterSetter;
  4. class DemoCreateAutoFillEntity extends Entity
  5. {
  6. use GetterSetter;
  7. const TABLE = 'test';
  8. const ID = 'id';
  9. const AUTO = 'id';
  10. const STRUCT = [
  11. 'id' => [
  12. self::READONLY => true,
  13. ],
  14. 'name' => [
  15. self::CREATE_FILL => 'name for '.self::CREATE_FILL,
  16. ],
  17. 'description' => [
  18. self::CREATE_FILL => null,
  19. ],
  20. 'address' => [
  21. self::CREATE_FILL => null,
  22. ],
  23. 'foo_bar' => [
  24. self::CREATE_FILL => null,
  25. ],
  26. 'hello' => [
  27. self::CREATE_FILL => null,
  28. ],
  29. ];
  30. protected function fillDescription($old): string
  31. {
  32. return 'set description.';
  33. }
  34. protected function fillAddress($old): string
  35. {
  36. return 'address is set now.';
  37. }
  38. protected function fillFooBar($old): string
  39. {
  40. return 'foo bar.';
  41. }
  42. protected function fillHello($old): string
  43. {
  44. return 'hello field.';
  45. }
  46. }
  1. public function testCreateAutoFill(): void
  2. {
  3. $entity = new DemoCreateAutoFillEntity();
  4. $entity
  5. ->fill()
  6. ->create();
  7. $data = <<<'eot'
  8. [
  9. []
  10. ]
  11. eot;
  12. $this->assertSame(
  13. $data,
  14. $this->varJson(
  15. $entity->flushData()
  16. )
  17. );
  18. }

TIP

默认情况下,不会自动填充,除非指定允许填充字段。

fillAll 设置允许自动填充字段为所有字段

  1. public function testAutoFillWithAll(): void
  2. {
  3. $entity = new DemoCreateAutoFillEntity();
  4. $entity
  5. ->fillAll()
  6. ->save();
  7. $data = <<<'eot'
  8. [
  9. {
  10. "name": "name for create_fill",
  11. "description": "set description.",
  12. "address": "address is set now.",
  13. "foo_bar": "foo bar.",
  14. "hello": "hello field."
  15. }
  16. ]
  17. eot;
  18. $this->assertSame(
  19. $data,
  20. $this->varJson(
  21. $entity->flushData()
  22. )
  23. );
  24. }

fill 设置允许自动填充字段指定字段例子

  1. public function testAutoFillWithCustomField(): void
  2. {
  3. $entity = new DemoCreateAutoFillEntity();
  4. $entity
  5. ->fill(['address'])
  6. ->save();
  7. $data = <<<'eot'
  8. [
  9. {
  10. "address": "address is set now."
  11. }
  12. ]
  13. eot;
  14. $this->assertSame(
  15. $data,
  16. $this->varJson(
  17. $entity->flushData()
  18. )
  19. );
  20. }

save 自动判断操作快捷方式支持添加数据

完整模型

  1. namespace Tests\Database\Ddd\Entity;
  2. use Leevel\Database\Ddd\Entity;
  3. use Leevel\Database\Ddd\GetterSetter;
  4. class DemoDatabaseEntity extends Entity
  5. {
  6. use GetterSetter;
  7. const TABLE = 'test';
  8. const ID = 'id';
  9. const AUTO = 'id';
  10. const STRUCT = [
  11. 'id' => [
  12. self::READONLY => true,
  13. ],
  14. 'name' => [],
  15. 'create_at' => [],
  16. ];
  17. }
  1. public function testSaveWithProp(): void
  2. {
  3. $entity = new DemoDatabaseEntity();
  4. $entity->save(['name' => 'hello']);
  5. $data = <<<'eot'
  6. [
  7. {
  8. "name": "hello"
  9. }
  10. ]
  11. eot;
  12. $this->assertSame(
  13. $data,
  14. $this->varJson(
  15. $entity->flushData()
  16. )
  17. );
  18. }

create 新增快捷方式支持添加数据

  1. public function testCreateWithProp(): void
  2. {
  3. $entity = new DemoDatabaseEntity();
  4. $entity->create(['name' => 'hello']);
  5. $data = <<<'eot'
  6. [
  7. {
  8. "name": "hello"
  9. }
  10. ]
  11. eot;
  12. $this->assertSame(
  13. $data,
  14. $this->varJson(
  15. $entity->flushData()
  16. )
  17. );
  18. }