更新实体

Testing Is Documentation

tests/Database/Ddd/Update/UpdateTest.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\DemoDatabaseEntity;
  6. use Tests\Database\Ddd\Entity\DemoEntity;
  7. use Tests\Database\Ddd\Entity\DemoReadonlyUpdateEntity;
  8. use Tests\Database\Ddd\Entity\DemoUpdateAutoFillEntity;
  9. use Tests\Database\Ddd\Entity\DemoUpdatePropWhiteEntity;

save 更新一个实体

存在主键数据,则可以通过 save 方法更新一个实体。

完整例子

  1. $entity = new DemoEntity(['id' => 1], true);
  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(['id' => 1], true);
  4. $entity->name = 'foo';
  5. $this->assertInstanceof(Entity::class, $entity);
  6. $this->assertSame(1, $entity->id);
  7. $this->assertSame('foo', $entity->name);
  8. $this->assertSame(['name'], $entity->changed());
  9. $this->assertNull($entity->flushData());
  10. $entity->save();
  11. $data = <<<'eot'
  12. [
  13. {
  14. "id": 1
  15. },
  16. {
  17. "name": "foo"
  18. }
  19. ]
  20. eot;
  21. $this->assertSame(
  22. $data,
  23. $this->varJson(
  24. $entity->flushData()
  25. )
  26. );
  27. }

TIP

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

update 更新一个实体

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

TIP

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

更新一个实体支持更新属性白名单

完整模型

  1. namespace Tests\Database\Ddd\Entity;
  2. use Leevel\Database\Ddd\Entity;
  3. use Leevel\Database\Ddd\GetterSetter;
  4. class DemoUpdatePropWhiteEntity 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::UPDATE_PROP_WHITE => true,
  13. self::READONLY => true,
  14. ],
  15. 'name' => [
  16. self::UPDATE_PROP_WHITE => true,
  17. ],
  18. 'description' => [],
  19. ];
  20. }

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

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

fill 设置允许自动填充字段

完整模型

  1. namespace Tests\Database\Ddd\Entity;
  2. use Leevel\Database\Ddd\Entity;
  3. use Leevel\Database\Ddd\GetterSetter;
  4. class DemoUpdateAutoFillEntity 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::UPDATE_FILL => 'name for '.self::UPDATE_FILL,
  16. ],
  17. 'description' => [
  18. self::UPDATE_FILL => null,
  19. ],
  20. 'address' => [
  21. self::UPDATE_FILL => null,
  22. ],
  23. 'foo_bar' => [
  24. self::UPDATE_FILL => null,
  25. ],
  26. 'hello' => [
  27. self::UPDATE_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 testUpdateAutoFillWithCustomField(): void
  2. {
  3. $entity = new DemoUpdateAutoFillEntity(['id' => 5], true);
  4. $entity
  5. ->fill(['address', 'hello'])
  6. ->update();
  7. $data = <<<'eot'
  8. [
  9. {
  10. "id": 5
  11. },
  12. {
  13. "address": "address is set now.",
  14. "hello": "hello field."
  15. }
  16. ]
  17. eot;
  18. $this->assertSame(
  19. $data,
  20. $this->varJson(
  21. $entity->flushData()
  22. )
  23. );
  24. }

TIP

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

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

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

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(['id' => 1], true);
  4. $entity->save(['name' => 'hello']);
  5. $data = <<<'eot'
  6. [
  7. {
  8. "id": 1
  9. },
  10. {
  11. "name": "hello"
  12. }
  13. ]
  14. eot;
  15. $this->assertSame(
  16. $data,
  17. $this->varJson(
  18. $entity->flushData()
  19. )
  20. );
  21. }

update 更新快捷方式支持添加数据

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

update 更新快捷方式存在更新数据才能够保存

  1. public function testUpdateWithNoDataAndDoNothing(): void
  2. {
  3. $entity = new DemoDatabaseEntity(['id' => 1]);
  4. $this->assertInstanceof(DemoDatabaseEntity::class, $entity->update());
  5. $this->assertNull($entity->flushData());
  6. }

update 更新快捷方式存在主键数据才能够保存

  1. public function testUpdateWithPrimaryKeyData(): void
  2. {
  3. $this->expectException(\InvalidArgumentException::class);
  4. $this->expectExceptionMessage('Entity Tests\\Database\\Ddd\\Entity\\DemoDatabaseEntity has no primary key data.');
  5. $entity = new DemoDatabaseEntity();
  6. $entity->update();
  7. }

save 自动判断操作快捷方式复合主键例子

完整模型

  1. namespace Tests\Database\Ddd\Entity;
  2. use Leevel\Database\Ddd\Entity;
  3. use Leevel\Database\Ddd\GetterSetter;
  4. class CompositeId extends Entity
  5. {
  6. use GetterSetter;
  7. const TABLE = 'composite_id';
  8. const ID = ['id1', 'id2'];
  9. const AUTO = null;
  10. const STRUCT = [
  11. 'id1' => [],
  12. 'id2' => [],
  13. 'name' => [],
  14. ];
  15. }
  1. public function testSaveWithCompositeId(): void
  2. {
  3. $connect = $this->createDatabaseConnect();
  4. $this->assertSame(
  5. 1,
  6. $connect
  7. ->table('composite_id')
  8. ->insert([
  9. 'id1' => 2,
  10. 'id2' => 3,
  11. ])
  12. );
  13. $entity = new CompositeId();
  14. $entity->save(['id1' => 2, 'id2' => 3, 'name' => 'hello']);
  15. $data = <<<'eot'
  16. [
  17. {
  18. "id1": 2,
  19. "id2": 3,
  20. "name": "hello"
  21. }
  22. ]
  23. eot;
  24. $this->assertSame(
  25. $data,
  26. $this->varJson(
  27. $entity->flushData()
  28. )
  29. );
  30. $entity->flush();
  31. $sql = 'SQL: [173] UPDATE `composite_id` SET `composite_id`.`name` = :pdonamedparameter_name WHERE `composite_id`.`id1` = :composite_id_id1 AND `composite_id`.`id2` = :composite_id_id2 LIMIT 1 | Params: 3 | Key: Name: [23] :pdonamedparameter_name | paramno=0 | name=[23] ":pdonamedparameter_name" | is_param=1 | param_type=2 | Key: Name: [17] :composite_id_id1 | paramno=1 | name=[17] ":composite_id_id1" | is_param=1 | param_type=1 | Key: Name: [17] :composite_id_id2 | paramno=2 | name=[17] ":composite_id_id2" | is_param=1 | param_type=1 (UPDATE `composite_id` SET `composite_id`.`name` = \'hello\' WHERE `composite_id`.`id1` = 2 AND `composite_id`.`id2` = 3 LIMIT 1)';
  32. $this->assertSame($sql, $entity->select()->getLastSql());
  33. }