替换实体

Testing Is Documentation

tests/Database/Ddd/Replace/ReplaceTest.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\DemoEntity;

replace 替换实体

完整例子

  1. $entity = new DemoEntity(['id' => 1]);
  2. $entity->name = 'foo';
  3. $entity->replace()->flush();

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

完整模型

  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]);
  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(['id', 'name'], $entity->changed());
  9. $this->assertNull($entity->flushData());
  10. $entity->replace();
  11. $data = <<<'eot'
  12. [
  13. {
  14. "id": 1,
  15. "name": "foo"
  16. }
  17. ]
  18. eot;
  19. $this->assertSame(
  20. $data,
  21. $this->varJson(
  22. $entity->flushData()
  23. )
  24. );
  25. $this->assertSame(1, $entity->flush());
  26. $sql = 'SQL: [101] INSERT INTO `test` (`test`.`id`,`test`.`name`) VALUES (:pdonamedparameter_id,:pdonamedparameter_name) | Params: 2 | Key: Name: [21] :pdonamedparameter_id | paramno=0 | name=[21] ":pdonamedparameter_id" | is_param=1 | param_type=1 | Key: Name: [23] :pdonamedparameter_name | paramno=1 | name=[23] ":pdonamedparameter_name" | is_param=1 | param_type=2 (INSERT INTO `test` (`test`.`id`,`test`.`name`) VALUES (1,\'foo\'))';
  27. $this->assertSame($sql, $entity->select()->getLastSql());
  28. $entity->refresh();
  29. $sql = 'SQL: [64] SELECT `test`.* FROM `test` WHERE `test`.`id` = :test_id LIMIT 1 | Params: 1 | Key: Name: [8] :test_id | paramno=0 | name=[8] ":test_id" | is_param=1 | param_type=1 (SELECT `test`.* FROM `test` WHERE `test`.`id` = 1 LIMIT 1)';
  30. $this->assertSame($sql, $entity->select()->getLastSql());
  31. $this->assertSame(1, $entity->id);
  32. $this->assertSame('foo', $entity->name);
  33. }

TIP

通过 replace 方法替换一个实体,并通过 flush 将实体持久化到数据库。

replace 替换实体新增例子

  1. public function testReplaceBaseUseCreate(): void
  2. {
  3. $entity = new DemoEntity(['id' => 1]);
  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(['id', 'name'], $entity->changed());
  9. $this->assertNull($entity->flushData());
  10. $entity->replace();
  11. $data = <<<'eot'
  12. [
  13. {
  14. "id": 1,
  15. "name": "foo"
  16. }
  17. ]
  18. eot;
  19. $this->assertSame(
  20. $data,
  21. $this->varJson(
  22. $entity->flushData()
  23. )
  24. );
  25. $this->assertSame(1, $entity->flush());
  26. $sql = 'SQL: [101] INSERT INTO `test` (`test`.`id`,`test`.`name`) VALUES (:pdonamedparameter_id,:pdonamedparameter_name) | Params: 2 | Key: Name: [21] :pdonamedparameter_id | paramno=0 | name=[21] ":pdonamedparameter_id" | is_param=1 | param_type=1 | Key: Name: [23] :pdonamedparameter_name | paramno=1 | name=[23] ":pdonamedparameter_name" | is_param=1 | param_type=2 (INSERT INTO `test` (`test`.`id`,`test`.`name`) VALUES (1,\'foo\'))';
  27. $this->assertSame($sql, $entity->select()->getLastSql());
  28. $entity->refresh();
  29. $sql = 'SQL: [64] SELECT `test`.* FROM `test` WHERE `test`.`id` = :test_id LIMIT 1 | Params: 1 | Key: Name: [8] :test_id | paramno=0 | name=[8] ":test_id" | is_param=1 | param_type=1 (SELECT `test`.* FROM `test` WHERE `test`.`id` = 1 LIMIT 1)';
  30. $this->assertSame($sql, $entity->select()->getLastSql());
  31. $this->assertSame(1, $entity->id);
  32. $this->assertSame('foo', $entity->name);
  33. }

replace.condition 替换实体配合设置扩展查询条件新增例子

replace 新增例子,设置扩展查询条件没有任何作用。

  1. public function testReplaceBaseUseCreateWithCondition(): void
  2. {
  3. $entity = new DemoEntity(['id' => 1]);
  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(['id', 'name'], $entity->changed());
  9. $this->assertNull($entity->flushData());
  10. $entity->condition(['name' => 'hello'])->replace();
  11. $data = <<<'eot'
  12. [
  13. {
  14. "id": 1,
  15. "name": "foo"
  16. }
  17. ]
  18. eot;
  19. $this->assertSame(
  20. $data,
  21. $this->varJson(
  22. $entity->flushData()
  23. )
  24. );
  25. $this->assertSame(1, $entity->flush());
  26. $sql = 'SQL: [101] INSERT INTO `test` (`test`.`id`,`test`.`name`) VALUES (:pdonamedparameter_id,:pdonamedparameter_name) | Params: 2 | Key: Name: [21] :pdonamedparameter_id | paramno=0 | name=[21] ":pdonamedparameter_id" | is_param=1 | param_type=1 | Key: Name: [23] :pdonamedparameter_name | paramno=1 | name=[23] ":pdonamedparameter_name" | is_param=1 | param_type=2 (INSERT INTO `test` (`test`.`id`,`test`.`name`) VALUES (1,\'foo\'))';
  27. $this->assertSame($sql, $entity->select()->getLastSql());
  28. $entity->refresh();
  29. $sql = 'SQL: [64] SELECT `test`.* FROM `test` WHERE `test`.`id` = :test_id LIMIT 1 | Params: 1 | Key: Name: [8] :test_id | paramno=0 | name=[8] ":test_id" | is_param=1 | param_type=1 (SELECT `test`.* FROM `test` WHERE `test`.`id` = 1 LIMIT 1)';
  30. $this->assertSame($sql, $entity->select()->getLastSql());
  31. $this->assertSame(1, $entity->id);
  32. $this->assertSame('foo', $entity->name);
  33. }

replace 替换实体更新例子

  1. public function testReplaceBaseUseUpdate(): void
  2. {
  3. $connect = $this->createDatabaseConnect();
  4. $this->assertSame(
  5. 1,
  6. $connect
  7. ->table('test')
  8. ->insert([
  9. 'id' => 1,
  10. 'name' => 'old',
  11. ])
  12. );
  13. $entity = new DemoEntity(['id' => 1]);
  14. $entity->name = 'foo';
  15. $this->assertInstanceof(Entity::class, $entity);
  16. $this->assertSame(1, $entity->id);
  17. $this->assertSame('foo', $entity->name);
  18. $this->assertSame(['id', 'name'], $entity->changed());
  19. $this->assertNull($entity->flushData());
  20. $entity->replace();
  21. $data = <<<'eot'
  22. [
  23. {
  24. "id": 1,
  25. "name": "foo"
  26. }
  27. ]
  28. eot;
  29. $this->assertSame(
  30. $data,
  31. $this->varJson(
  32. $entity->flushData()
  33. )
  34. );
  35. $this->assertSame(1, $entity->flush());
  36. $sql = 'SQL: [94] UPDATE `test` SET `test`.`name` = :pdonamedparameter_name WHERE `test`.`id` = :test_id LIMIT 1 | Params: 2 | Key: Name: [23] :pdonamedparameter_name | paramno=0 | name=[23] ":pdonamedparameter_name" | is_param=1 | param_type=2 | Key: Name: [8] :test_id | paramno=1 | name=[8] ":test_id" | is_param=1 | param_type=1 (UPDATE `test` SET `test`.`name` = \'foo\' WHERE `test`.`id` = 1 LIMIT 1)';
  37. $this->assertSame($sql, $entity->select()->getLastSql());
  38. $entity->refresh();
  39. $sql = 'SQL: [64] SELECT `test`.* FROM `test` WHERE `test`.`id` = :test_id LIMIT 1 | Params: 1 | Key: Name: [8] :test_id | paramno=0 | name=[8] ":test_id" | is_param=1 | param_type=1 (SELECT `test`.* FROM `test` WHERE `test`.`id` = 1 LIMIT 1)';
  40. $this->assertSame($sql, $entity->select()->getLastSql());
  41. $this->assertSame(1, $entity->id);
  42. $this->assertSame('foo', $entity->name);
  43. }

replace.condition 替换实体配合设置扩展查询条件更新例子

replace 更新例子,设置扩展查询条件影响更新查询条件。

  1. public function testReplaceBaseUseUpdateWithCondition(): void
  2. {
  3. $connect = $this->createDatabaseConnect();
  4. $this->assertSame(
  5. 1,
  6. $connect
  7. ->table('test')
  8. ->insert([
  9. 'id' => 1,
  10. 'name' => 'old',
  11. ])
  12. );
  13. $entity = new DemoEntity(['id' => 1]);
  14. $entity->name = 'foo';
  15. $this->assertInstanceof(Entity::class, $entity);
  16. $this->assertSame(1, $entity->id);
  17. $this->assertSame('foo', $entity->name);
  18. $this->assertSame(['id', 'name'], $entity->changed());
  19. $this->assertNull($entity->flushData());
  20. $entity->condition(['name' => 'hello'])->replace();
  21. $data = <<<'eot'
  22. [
  23. {
  24. "id": 1,
  25. "name": "foo"
  26. }
  27. ]
  28. eot;
  29. $this->assertSame(
  30. $data,
  31. $this->varJson(
  32. $entity->flushData()
  33. )
  34. );
  35. $this->assertSame(0, $entity->flush());
  36. $sql = 'SQL: [125] UPDATE `test` SET `test`.`name` = :pdonamedparameter_name WHERE `test`.`name` = :test_name AND `test`.`id` = :test_id LIMIT 1 | Params: 3 | Key: Name: [23] :pdonamedparameter_name | paramno=0 | name=[23] ":pdonamedparameter_name" | is_param=1 | param_type=2 | Key: Name: [10] :test_name | paramno=1 | name=[10] ":test_name" | is_param=1 | param_type=2 | Key: Name: [8] :test_id | paramno=2 | name=[8] ":test_id" | is_param=1 | param_type=1 (UPDATE `test` SET `test`.`name` = \'foo\' WHERE `test`.`name` = \'hello\' AND `test`.`id` = 1 LIMIT 1)';
  37. $this->assertSame($sql, $entity->select()->getLastSql());
  38. $entity->refresh();
  39. $sql = 'SQL: [64] SELECT `test`.* FROM `test` WHERE `test`.`id` = :test_id LIMIT 1 | Params: 1 | Key: Name: [8] :test_id | paramno=0 | name=[8] ":test_id" | is_param=1 | param_type=1 (SELECT `test`.* FROM `test` WHERE `test`.`id` = 1 LIMIT 1)';
  40. $this->assertSame($sql, $entity->select()->getLastSql());
  41. $this->assertSame(1, $entity->id);
  42. $this->assertSame('old', $entity->name);
  43. }

replace 替换快捷方式,记录存在但是不存在更新数据不作任何处理

这里和单纯的更新不一样,单纯的更新不存在更新数据,则会抛出异常。

  1. public function testReplaceWithCompositeIdButNoDataToBeUpdate(): 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->replace(['id1' => 2, 'id2' => 3]);
  15. $data = <<<'eot'
  16. [
  17. {
  18. "id1": 2,
  19. "id2": 3
  20. }
  21. ]
  22. eot;
  23. $this->assertSame(
  24. $data,
  25. $this->varJson(
  26. $entity->flushData()
  27. )
  28. );
  29. $entity->flush();
  30. $sql = '[FAILED] SQL: [125] INSERT INTO `composite_id` (`composite_id`.`id1`,`composite_id`.`id2`) VALUES (:pdonamedparameter_id1,:pdonamedparameter_id2) | Params: 2 | Key: Name: [22] :pdonamedparameter_id1 | paramno=0 | name=[22] ":pdonamedparameter_id1" | is_param=1 | param_type=1 | Key: Name: [22] :pdonamedparameter_id2 | paramno=1 | name=[22] ":pdonamedparameter_id2" | is_param=1 | param_type=1 (INSERT INTO `composite_id` (`composite_id`.`id1`,`composite_id`.`id2`) VALUES (2,3))';
  31. $this->assertSame($sql, $entity->select()->getLastSql());
  32. }