保存实体

将实体持久化到数据库。

引入相关类

  • use Leevel\Database\Ddd\Entity;
  • use Tests\Database\DatabaseTestCase as TestCase;
  • use Tests\Database\Ddd\Entity\TestConstructPropBlackEntity;
  • use Tests\Database\Ddd\Entity\TestConstructPropWhiteEntity;
  • use Tests\Database\Ddd\Entity\TestCreateAutoFillEntity;
  • use Tests\Database\Ddd\Entity\TestCreatePropWhiteEntity;
  • use Tests\Database\Ddd\Entity\TestEntity;

    创建一个实体

完整例子

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

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

  1. public function testBaseUse()
  2. {
  3. $entity = new TestEntity();
  4. $this->assertInstanceof(Entity::class, $entity);
  5. $entity->name = 'foo';
  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 将实体持久化到数据库。
:::

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

完整模型

  1. namespace Tests\Database\Ddd\Entity;
  2. use Leevel\Database\Ddd\Entity;
  3. class TestConstructPropWhiteEntity extends Entity
  4. {
  5. const TABLE = 'test';
  6. const ID = 'id';
  7. const AUTO = 'id';
  8. const STRUCT = [
  9. 'id' => [
  10. 'readonly' => true,
  11. 'construct_prop_white' => true,
  12. ],
  13. 'name' => [],
  14. ];
  15. private $id;
  16. private $name;
  17. public function setter(string $prop, $value): Entity
  18. {
  19. $this->{$this->prop($prop)} = $value;
  20. return $this;
  21. }
  22. public function getter(string $prop)
  23. {
  24. return $this->{$this->prop($prop)};
  25. }
  26. }

调用 construct_prop_white => true 来设置字段白名单,一旦设置了白名单只有通过了白名单的数据才能够通过构造器更新模型属性。

  1. public function testConsturctPropWhite()
  2. {
  3. $entity = new TestConstructPropWhiteEntity([
  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. class TestConstructPropBlackEntity extends Entity
  4. {
  5. const TABLE = 'test';
  6. const ID = 'id';
  7. const AUTO = 'id';
  8. const STRUCT = [
  9. 'id' => [
  10. 'readonly' => true,
  11. 'construct_prop_black' => true,
  12. ],
  13. 'name' => [],
  14. ];
  15. private $id;
  16. private $name;
  17. public function setter(string $prop, $value): Entity
  18. {
  19. $this->{$this->prop($prop)} = $value;
  20. return $this;
  21. }
  22. public function getter(string $prop)
  23. {
  24. return $this->{$this->prop($prop)};
  25. }
  26. }

调用 construct_prop_black => true 来设置字段黑名单,一旦设置了黑名单处于黑名单的数据无法通过构造器更新模型属性。

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