删除实体

Testing Is Documentation

tests/Database/Ddd/Delete/DeleteTest.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;
  6. use Tests\Database\Ddd\Entity\EntityWithoutPrimaryKey;
  7. use Tests\Database\Ddd\Entity\Relation\Post;
  8. use Tests\Database\Ddd\Entity\Relation\PostContent;
  9. use Tests\Database\Ddd\Entity\SoftDeleteNotFoundDeleteAtField;

delete 删除一个实体

完整例子

  1. $entity = new DemoEntity(['id' => 5]);
  2. $entity->delete()->flush();

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

完整模型

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

TIP

通过 delete 方法删除一个实体,并通过 flush 将实体持久化到数据库。

softDelete 软删除一个实体

完整模型

  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 testSoftDelete(): void
  2. {
  3. $connect = $this->createDatabaseConnect();
  4. $this->assertSame(
  5. 1,
  6. $connect
  7. ->table('post')
  8. ->insert([
  9. 'title' => 'hello world',
  10. 'user_id' => 1,
  11. 'summary' => 'post summary',
  12. 'delete_at' => 0,
  13. ])
  14. );
  15. $this->assertSame(
  16. 2,
  17. $connect
  18. ->table('post')
  19. ->insert([
  20. 'title' => 'hello world',
  21. 'user_id' => 1,
  22. 'summary' => 'post summary',
  23. 'delete_at' => 0,
  24. ])
  25. );
  26. $post = Post::select()->findEntity(1);
  27. $this->assertInstanceof(Post::class, $post);
  28. $this->assertSame(1, $post->userId);
  29. $this->assertSame('hello world', $post->title);
  30. $this->assertSame('post summary', $post->summary);
  31. $this->assertSame(0, $post->delete_at);
  32. $this->assertFalse($post->softDeleted());
  33. $post->softDelete()->flush();
  34. $this->assertTrue($post->softDeleted());
  35. $post1 = Post::withSoftDeleted()->findEntity(1);
  36. $this->assertInstanceof(Post::class, $post1);
  37. $this->assertSame(1, $post1->userId);
  38. $this->assertSame('hello world', $post1->title);
  39. $this->assertSame('post summary', $post1->summary);
  40. $this->assertSame(date('Y-m'), date('Y-m', $post1->delete_at));
  41. $post2 = Post::select()->findEntity(2);
  42. $this->assertInstanceof(Post::class, $post2);
  43. $this->assertSame(1, $post2->userId);
  44. $this->assertSame('hello world', $post2->title);
  45. $this->assertSame('post summary', $post2->summary);
  46. $this->assertSame(0, $post2->delete_at);
  47. $post1 = Post::select()->findEntity(1);
  48. $this->assertInstanceof(Post::class, $post1);
  49. $this->assertNull($post1->userId);
  50. $this->assertNull($post1->title);
  51. $this->assertNull($post1->summary);
  52. $this->assertNull($post1->delete_at);
  53. }

softDestroy 根据主键 ID 软删除实体

  1. public function testSoftDestroy(): void
  2. {
  3. $connect = $this->createDatabaseConnect();
  4. $this->assertSame(
  5. 1,
  6. $connect
  7. ->table('post')
  8. ->insert([
  9. 'title' => 'hello world',
  10. 'user_id' => 1,
  11. 'summary' => 'post summary',
  12. 'delete_at' => 0,
  13. ])
  14. );
  15. $this->assertSame(
  16. 2,
  17. $connect
  18. ->table('post')
  19. ->insert([
  20. 'title' => 'hello world',
  21. 'user_id' => 1,
  22. 'summary' => 'post summary',
  23. 'delete_at' => 0,
  24. ])
  25. );
  26. $post = Post::select()->findEntity(1);
  27. $this->assertInstanceof(Post::class, $post);
  28. $this->assertSame(1, $post->userId);
  29. $this->assertSame('hello world', $post->title);
  30. $this->assertSame('post summary', $post->summary);
  31. $this->assertSame(0, $post->delete_at);
  32. $this->assertFalse($post->softDeleted());
  33. $this->assertSame(1, Post::softDestroy([1]));
  34. $this->assertFalse($post->softDeleted());
  35. $post1 = Post::withSoftDeleted()->findEntity(1);
  36. $this->assertInstanceof(Post::class, $post1);
  37. $this->assertSame(1, $post1->userId);
  38. $this->assertSame('hello world', $post1->title);
  39. $this->assertSame('post summary', $post1->summary);
  40. $this->assertSame(date('Y-m'), date('Y-m', $post1->delete_at));
  41. $post2 = Post::select()->findEntity(2);
  42. $this->assertInstanceof(Post::class, $post2);
  43. $this->assertSame(1, $post2->userId);
  44. $this->assertSame('hello world', $post2->title);
  45. $this->assertSame('post summary', $post2->summary);
  46. $this->assertSame(0, $post2->delete_at);
  47. $post1 = Post::select()->findEntity(1);
  48. $this->assertInstanceof(Post::class, $post1);
  49. $this->assertNull($post1->userId);
  50. $this->assertNull($post1->title);
  51. $this->assertNull($post1->summary);
  52. $this->assertNull($post1->delete_at);
  53. }

destroy 根据主键 ID 删除实体

  1. public function testDestroy(): void
  2. {
  3. $connect = $this->createDatabaseConnect();
  4. $this->assertSame(
  5. 1,
  6. $connect
  7. ->table('post')
  8. ->insert([
  9. 'title' => 'hello world',
  10. 'user_id' => 1,
  11. 'summary' => 'post summary',
  12. 'delete_at' => 0,
  13. ])
  14. );
  15. $this->assertSame(
  16. 2,
  17. $connect
  18. ->table('post')
  19. ->insert([
  20. 'title' => 'hello world',
  21. 'user_id' => 1,
  22. 'summary' => 'post summary',
  23. 'delete_at' => 0,
  24. ])
  25. );
  26. $post = Post::select()->findEntity(1);
  27. $this->assertInstanceof(Post::class, $post);
  28. $this->assertSame(1, $post->userId);
  29. $this->assertSame('hello world', $post->title);
  30. $this->assertSame('post summary', $post->summary);
  31. $this->assertSame(0, $post->delete_at);
  32. Post::destroy([1]);
  33. $post1 = Post::withSoftDeleted()->findEntity(1);
  34. $this->assertInstanceof(Post::class, $post1);
  35. $this->assertSame(1, $post1->userId);
  36. $this->assertSame('hello world', $post1->title);
  37. $this->assertSame('post summary', $post1->summary);
  38. $this->assertSame(date('Y-m'), date('Y-m', $post1->delete_at));
  39. $post2 = Post::select()->findEntity(2);
  40. $this->assertInstanceof(Post::class, $post2);
  41. $this->assertSame(1, $post2->userId);
  42. $this->assertSame('hello world', $post2->title);
  43. $this->assertSame('post summary', $post2->summary);
  44. $this->assertSame(0, $post2->delete_at);
  45. $post1 = Post::select()->findEntity(1);
  46. $this->assertInstanceof(Post::class, $post1);
  47. $this->assertNull($post1->userId);
  48. $this->assertNull($post1->title);
  49. $this->assertNull($post1->summary);
  50. $this->assertNull($post1->delete_at);
  51. }

forceDestroy 根据主键 ID 强制删除实体

  1. public function testForceDestroy(): void
  2. {
  3. $connect = $this->createDatabaseConnect();
  4. $this->assertSame(
  5. 1,
  6. $connect
  7. ->table('post')
  8. ->insert([
  9. 'title' => 'hello world',
  10. 'user_id' => 1,
  11. 'summary' => 'post summary',
  12. 'delete_at' => 0,
  13. ])
  14. );
  15. $this->assertSame(
  16. 2,
  17. $connect
  18. ->table('post')
  19. ->insert([
  20. 'title' => 'hello world',
  21. 'user_id' => 1,
  22. 'summary' => 'post summary',
  23. 'delete_at' => 0,
  24. ])
  25. );
  26. $post = Post::select()->findEntity(1);
  27. $this->assertInstanceof(Post::class, $post);
  28. $this->assertSame(1, $post->userId);
  29. $this->assertSame('hello world', $post->title);
  30. $this->assertSame('post summary', $post->summary);
  31. $this->assertSame(0, $post->delete_at);
  32. Post::forceDestroy([1]);
  33. $post1 = Post::withSoftDeleted()->findEntity(1);
  34. $this->assertInstanceof(Post::class, $post1);
  35. $this->assertNull($post1->userId);
  36. $this->assertNull($post1->title);
  37. $this->assertNull($post1->summary);
  38. $this->assertNull($post1->delete_at);
  39. $post2 = Post::select()->findEntity(2);
  40. $this->assertInstanceof(Post::class, $post2);
  41. $this->assertSame(1, $post2->userId);
  42. $this->assertSame('hello world', $post2->title);
  43. $this->assertSame('post summary', $post2->summary);
  44. $this->assertSame(0, $post2->delete_at);
  45. $post1 = Post::select()->findEntity(1);
  46. $this->assertInstanceof(Post::class, $post1);
  47. $this->assertNull($post1->userId);
  48. $this->assertNull($post1->title);
  49. $this->assertNull($post1->summary);
  50. $this->assertNull($post1->delete_at);
  51. }

softRestore 恢复软删除的实体

  1. public function testSoftRestore(): void
  2. {
  3. $connect = $this->createDatabaseConnect();
  4. $this->assertSame(
  5. 1,
  6. $connect
  7. ->table('post')
  8. ->insert([
  9. 'title' => 'hello world',
  10. 'user_id' => 1,
  11. 'summary' => 'post summary',
  12. 'delete_at' => 0,
  13. ])
  14. );
  15. $this->assertSame(
  16. 2,
  17. $connect
  18. ->table('post')
  19. ->insert([
  20. 'title' => 'hello world',
  21. 'user_id' => 1,
  22. 'summary' => 'post summary',
  23. 'delete_at' => 0,
  24. ])
  25. );
  26. $post = Post::select()->findEntity(1);
  27. $this->assertInstanceof(Post::class, $post);
  28. $this->assertSame(1, $post->userId);
  29. $this->assertSame('hello world', $post->title);
  30. $this->assertSame('post summary', $post->summary);
  31. $this->assertSame(0, $post->delete_at);
  32. $this->assertFalse($post->softDeleted());
  33. $post->softDelete()->flush();
  34. $this->assertTrue($post->softDeleted());
  35. $post1 = Post::select()->findEntity(1);
  36. $this->assertInstanceof(Post::class, $post1);
  37. $this->assertNull($post1->userId);
  38. $this->assertNull($post1->title);
  39. $this->assertNull($post1->summary);
  40. $this->assertNull($post1->delete_at);
  41. $post1 = Post::withSoftDeleted()->findEntity(1);
  42. $this->assertInstanceof(Post::class, $post1);
  43. $this->assertSame(1, $post1->userId);
  44. $this->assertSame('hello world', $post1->title);
  45. $this->assertSame('post summary', $post1->summary);
  46. $this->assertSame(date('Y-m'), date('Y-m', $post1->delete_at));
  47. $post2 = Post::select()->findEntity(2);
  48. $this->assertInstanceof(Post::class, $post2);
  49. $this->assertSame(1, $post2->userId);
  50. $this->assertSame('hello world', $post2->title);
  51. $this->assertSame('post summary', $post2->summary);
  52. $this->assertSame(0, $post2->delete_at);
  53. $newPost = Post::withSoftDeleted()->findEntity(1);
  54. $this->assertTrue($newPost->softDeleted());
  55. $newPost->softRestore()->flush();
  56. $this->assertFalse($newPost->softDeleted());
  57. $restorePost1 = Post::select()->findEntity(1);
  58. $this->assertSame(0, $restorePost1->delete_at);
  59. $post1 = Post::withSoftDeleted()->findEntity(1);
  60. $this->assertInstanceof(Post::class, $post1);
  61. $this->assertSame(1, $post1->userId);
  62. $this->assertSame('hello world', $post1->title);
  63. $this->assertSame('post summary', $post1->summary);
  64. $this->assertSame(0, $post1->delete_at);
  65. }

delete 删除实体

  1. public function testDelete(): void
  2. {
  3. $connect = $this->createDatabaseConnect();
  4. $this->assertSame(
  5. 1,
  6. $connect
  7. ->table('post')
  8. ->insert([
  9. 'title' => 'hello world',
  10. 'user_id' => 1,
  11. 'summary' => 'post summary',
  12. 'delete_at' => 0,
  13. ])
  14. );
  15. $this->assertSame(
  16. 2,
  17. $connect
  18. ->table('post')
  19. ->insert([
  20. 'title' => 'hello world',
  21. 'user_id' => 1,
  22. 'summary' => 'post summary',
  23. 'delete_at' => 0,
  24. ])
  25. );
  26. $post = Post::select()->findEntity(1);
  27. $this->assertInstanceof(Post::class, $post);
  28. $this->assertSame(1, $post->userId);
  29. $this->assertSame('hello world', $post->title);
  30. $this->assertSame('post summary', $post->summary);
  31. $this->assertSame(0, $post->delete_at);
  32. $this->assertFalse($post->softDeleted());
  33. $post->delete()->flush();
  34. $sql = 'SQL: [104] UPDATE `post` SET `post`.`delete_at` = :pdonamedparameter_delete_at WHERE `post`.`id` = :post_id LIMIT 1 | Params: 2 | Key: Name: [28] :pdonamedparameter_delete_at | paramno=0 | name=[28] ":pdonamedparameter_delete_at" | is_param=1 | param_type=1 | Key: Name: [8] :post_id | paramno=1 | name=[8] ":post_id" | is_param=1 | param_type=1 (UPDATE `post` SET `post`.`delete_at` = %d WHERE `post`.`id` = 1 LIMIT 1)';
  35. $time = time();
  36. $this->assertTrue(in_array(Post::select()->getLastSql(), [sprintf($sql, $time), sprintf($sql, $time - 1), sprintf($sql, $time + 1)], true));
  37. $this->assertTrue($post->softDeleted());
  38. $post1 = Post::withSoftDeleted()->findEntity(1);
  39. $this->assertInstanceof(Post::class, $post1);
  40. $this->assertSame(1, $post1->userId);
  41. $this->assertSame('hello world', $post1->title);
  42. $this->assertSame('post summary', $post1->summary);
  43. $this->assertSame(date('Y-m'), date('Y-m', $post1->delete_at));
  44. $post2 = Post::select()->findEntity(2);
  45. $this->assertInstanceof(Post::class, $post2);
  46. $this->assertSame(1, $post2->userId);
  47. $this->assertSame('hello world', $post2->title);
  48. $this->assertSame('post summary', $post2->summary);
  49. $this->assertSame(0, $post2->delete_at);
  50. $post1 = Post::select()->findEntity(1);
  51. $this->assertInstanceof(Post::class, $post1);
  52. $this->assertNull($post1->userId);
  53. $this->assertNull($post1->title);
  54. $this->assertNull($post1->summary);
  55. $this->assertNull($post1->delete_at);
  56. }

delete.condition 删除实体配合设置扩展查询条件

  1. public function testDeleteWithCondition(): void
  2. {
  3. $connect = $this->createDatabaseConnect();
  4. $this->assertSame(
  5. 1,
  6. $connect
  7. ->table('post')
  8. ->insert([
  9. 'title' => 'hello world',
  10. 'user_id' => 1,
  11. 'summary' => 'post summary',
  12. 'delete_at' => 0,
  13. ])
  14. );
  15. $this->assertSame(
  16. 2,
  17. $connect
  18. ->table('post')
  19. ->insert([
  20. 'title' => 'hello world',
  21. 'user_id' => 1,
  22. 'summary' => 'post summary',
  23. 'delete_at' => 0,
  24. ])
  25. );
  26. $post = Post::select()->findEntity(1);
  27. $this->assertInstanceof(Post::class, $post);
  28. $this->assertSame(1, $post->userId);
  29. $this->assertSame('hello world', $post->title);
  30. $this->assertSame('post summary', $post->summary);
  31. $this->assertSame(0, $post->delete_at);
  32. $this->assertFalse($post->softDeleted());
  33. $post->condition(['user_id' => 99999])->delete()->flush();
  34. $sql = 'SQL: [141] UPDATE `post` SET `post`.`delete_at` = :pdonamedparameter_delete_at WHERE `post`.`user_id` = :post_user_id AND `post`.`id` = :post_id LIMIT 1 | Params: 3 | Key: Name: [28] :pdonamedparameter_delete_at | paramno=0 | name=[28] ":pdonamedparameter_delete_at" | is_param=1 | param_type=1 | Key: Name: [13] :post_user_id | paramno=1 | name=[13] ":post_user_id" | is_param=1 | param_type=1 | Key: Name: [8] :post_id | paramno=2 | name=[8] ":post_id" | is_param=1 | param_type=1 (UPDATE `post` SET `post`.`delete_at` = %d WHERE `post`.`user_id` = 99999 AND `post`.`id` = 1 LIMIT 1)';
  35. $time = time();
  36. $this->assertTrue(in_array(Post::select()->getLastSql(), [sprintf($sql, $time), sprintf($sql, $time - 1), sprintf($sql, $time + 1)], true));
  37. $this->assertTrue($post->softDeleted());
  38. $post1 = Post::withSoftDeleted()->findEntity(1);
  39. $this->assertInstanceof(Post::class, $post1);
  40. $this->assertSame(1, $post1->userId);
  41. $this->assertSame('hello world', $post1->title);
  42. $this->assertSame('post summary', $post1->summary);
  43. $this->assertSame(0, $post1->delete_at);
  44. $post2 = Post::select()->findEntity(2);
  45. $this->assertInstanceof(Post::class, $post2);
  46. $this->assertSame(1, $post2->userId);
  47. $this->assertSame('hello world', $post2->title);
  48. $this->assertSame('post summary', $post2->summary);
  49. $this->assertSame(0, $post2->delete_at);
  50. $post1 = Post::select()->findEntity(1);
  51. $this->assertInstanceof(Post::class, $post1);
  52. $this->assertSame(1, $post1->userId);
  53. $this->assertSame('hello world', $post1->title);
  54. $this->assertSame('post summary', $post1->summary);
  55. $this->assertSame(0, $post1->delete_at);
  56. }

delete 复合主键删除实体

  1. public function testDeleteForCompositeId(): void
  2. {
  3. $connect = $this->createDatabaseConnect();
  4. $this->assertSame(
  5. 1,
  6. $connect
  7. ->table('composite_id')
  8. ->insert([
  9. 'id1' => 1,
  10. 'id2' => 2,
  11. 'name' => 'hello liu',
  12. ])
  13. );
  14. $entity = CompositeId::select()->where(['id1' => 1, 'id2' => 2])->findOne();
  15. $this->assertInstanceof(CompositeId::class, $entity);
  16. $this->assertSame(1, $entity->id1);
  17. $this->assertSame(2, $entity->id2);
  18. $this->assertSame('hello liu', $entity->name);
  19. $entity->delete()->flush();
  20. $entity = CompositeId::select()->where(['id1' => 1, 'id2' => 2])->findOne();
  21. $this->assertInstanceof(CompositeId::class, $entity);
  22. $this->assertNull($entity->id1);
  23. $this->assertNull($entity->id2);
  24. $this->assertNull($entity->name);
  25. }

forceDelete 强制删除实体

  1. public function testForceDelete(): void
  2. {
  3. $connect = $this->createDatabaseConnect();
  4. $this->assertSame(
  5. 1,
  6. $connect
  7. ->table('post')
  8. ->insert([
  9. 'title' => 'hello world',
  10. 'user_id' => 1,
  11. 'summary' => 'post summary',
  12. 'delete_at' => 0,
  13. ])
  14. );
  15. $this->assertSame(
  16. 2,
  17. $connect
  18. ->table('post')
  19. ->insert([
  20. 'title' => 'hello world',
  21. 'user_id' => 1,
  22. 'summary' => 'post summary',
  23. 'delete_at' => 0,
  24. ])
  25. );
  26. $post = Post::select()->findEntity(1);
  27. $this->assertInstanceof(Post::class, $post);
  28. $this->assertSame(1, $post->userId);
  29. $this->assertSame('hello world', $post->title);
  30. $this->assertSame('post summary', $post->summary);
  31. $this->assertSame(0, $post->delete_at);
  32. $this->assertFalse($post->softDeleted());
  33. $post->forceDelete()->flush();
  34. $this->assertSame('SQL: [55] DELETE FROM `post` WHERE `post`.`id` = :post_id LIMIT 1 | Params: 1 | Key: Name: [8] :post_id | paramno=0 | name=[8] ":post_id" | is_param=1 | param_type=1 (DELETE FROM `post` WHERE `post`.`id` = 1 LIMIT 1)', Post::select()->getLastSql());
  35. $this->assertFalse($post->softDeleted());
  36. $post1 = Post::withSoftDeleted()->findEntity(1);
  37. $this->assertInstanceof(Post::class, $post1);
  38. $this->assertNull($post1->userId);
  39. $this->assertNull($post1->title);
  40. $this->assertNull($post1->summary);
  41. $this->assertNull($post1->delete_at);
  42. $post2 = Post::select()->findEntity(2);
  43. $this->assertInstanceof(Post::class, $post2);
  44. $this->assertSame(1, $post2->userId);
  45. $this->assertSame('hello world', $post2->title);
  46. $this->assertSame('post summary', $post2->summary);
  47. $this->assertSame(0, $post2->delete_at);
  48. $post1 = Post::select()->findEntity(1);
  49. $this->assertInstanceof(Post::class, $post1);
  50. $this->assertNull($post1->userId);
  51. $this->assertNull($post1->title);
  52. $this->assertNull($post1->summary);
  53. $this->assertNull($post1->delete_at);
  54. }

forceDelete.condition 强制删除实体配合设置扩展查询条件

  1. public function testForceDeleteWithCondition(): void
  2. {
  3. $connect = $this->createDatabaseConnect();
  4. $this->assertSame(
  5. 1,
  6. $connect
  7. ->table('post')
  8. ->insert([
  9. 'title' => 'hello world',
  10. 'user_id' => 1,
  11. 'summary' => 'post summary',
  12. 'delete_at' => 0,
  13. ])
  14. );
  15. $this->assertSame(
  16. 2,
  17. $connect
  18. ->table('post')
  19. ->insert([
  20. 'title' => 'hello world',
  21. 'user_id' => 1,
  22. 'summary' => 'post summary',
  23. 'delete_at' => 0,
  24. ])
  25. );
  26. $post = Post::select()->findEntity(1);
  27. $this->assertInstanceof(Post::class, $post);
  28. $this->assertSame(1, $post->userId);
  29. $this->assertSame('hello world', $post->title);
  30. $this->assertSame('post summary', $post->summary);
  31. $this->assertSame(0, $post->delete_at);
  32. $this->assertFalse($post->softDeleted());
  33. $post->condition(['user_id' => 99999])->forceDelete()->flush();
  34. $this->assertSame('SQL: [92] DELETE FROM `post` WHERE `post`.`user_id` = :post_user_id AND `post`.`id` = :post_id LIMIT 1 | Params: 2 | Key: Name: [13] :post_user_id | paramno=0 | name=[13] ":post_user_id" | is_param=1 | param_type=1 | Key: Name: [8] :post_id | paramno=1 | name=[8] ":post_id" | is_param=1 | param_type=1 (DELETE FROM `post` WHERE `post`.`user_id` = 99999 AND `post`.`id` = 1 LIMIT 1)', Post::select()->getLastSql());
  35. $this->assertFalse($post->softDeleted());
  36. $post1 = Post::withSoftDeleted()->findEntity(1);
  37. $this->assertInstanceof(Post::class, $post1);
  38. $this->assertSame(1, $post1->userId);
  39. $this->assertSame('hello world', $post1->title);
  40. $this->assertSame('post summary', $post1->summary);
  41. $this->assertSame(0, $post1->delete_at);
  42. $post2 = Post::select()->findEntity(2);
  43. $this->assertInstanceof(Post::class, $post2);
  44. $this->assertSame(1, $post2->userId);
  45. $this->assertSame('hello world', $post2->title);
  46. $this->assertSame('post summary', $post2->summary);
  47. $this->assertSame(0, $post2->delete_at);
  48. $post1 = Post::select()->findEntity(1);
  49. $this->assertInstanceof(Post::class, $post1);
  50. $this->assertSame(1, $post1->userId);
  51. $this->assertSame('hello world', $post1->title);
  52. $this->assertSame('post summary', $post1->summary);
  53. $this->assertSame(0, $post1->delete_at);
  54. }