hasOne 一对一关联

Testing Is Documentation

tests/Database/Ddd/Relation/HasOneTest.phphasOne 一对一关联 - 图1

一对一的关联是一种常用的关联,比如一篇文章与文章内容属于一对一的关系。

一对一关联支持类型关联项

关联项说明例子
\Leevel\Database\Ddd\Entity::HAS_ONE一对一关联实体\Tests\Database\Ddd\Entity\Relation\PostContent::class
\Leevel\Database\Ddd\Entity::SOURCE_KEY关联查询源键字段id
\Leevel\Database\Ddd\Entity::TARGET_KEY关联目标键字段post_id
\Leevel\Database\Ddd\Entity::RELATION_SCOPE关联查询作用域foo

Uses

  1. <?php
  2. use Leevel\Collection\Collection;
  3. use Leevel\Database\Ddd\Relation\HasOne;
  4. use Leevel\Database\Ddd\Relation\Relation;
  5. use Leevel\Database\Ddd\Select;
  6. use Tests\Database\DatabaseTestCase as TestCase;
  7. use Tests\Database\Ddd\Entity\Relation\Post;
  8. use Tests\Database\Ddd\Entity\Relation\PostContent;

基本使用方法

fixture 定义

Tests\Database\Ddd\Entity\Relation\Post

  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. }

Tests\Database\Ddd\Entity\Relation\PostContent

  1. namespace Tests\Database\Ddd\Entity\Relation;
  2. use Leevel\Database\Ddd\Entity;
  3. use Leevel\Database\Ddd\GetterSetter;
  4. class PostContent extends Entity
  5. {
  6. use GetterSetter;
  7. const TABLE = 'post_content';
  8. const ID = null;
  9. const AUTO = null;
  10. const STRUCT = [
  11. 'post_id' => [
  12. self::READONLY => true,
  13. ],
  14. 'content' => [],
  15. ];
  16. }
  1. public function testBaseUse(): void
  2. {
  3. $post = Post::select()->where('id', 1)->findOne();
  4. $this->assertInstanceof(Post::class, $post);
  5. $this->assertNull($post->id);
  6. $connect = $this->createDatabaseConnect();
  7. $this->assertSame(
  8. 1,
  9. $connect
  10. ->table('post')
  11. ->insert([
  12. 'title' => 'hello world',
  13. 'user_id' => 1,
  14. 'summary' => 'Say hello to the world.',
  15. 'delete_at' => 0,
  16. ])
  17. );
  18. $this->assertSame(
  19. 1,
  20. $connect
  21. ->table('post_content')
  22. ->insert([
  23. 'post_id' => 1,
  24. 'content' => 'I am content with big data.',
  25. ])
  26. );
  27. $post = Post::select()->where('id', 1)->findOne();
  28. $this->assertSame(1, $post->id);
  29. $this->assertSame(1, $post['id']);
  30. $this->assertSame(1, $post->getId());
  31. $this->assertSame(1, $post->user_id);
  32. $this->assertSame(1, $post->userId);
  33. $this->assertSame(1, $post['user_id']);
  34. $this->assertSame(1, $post->getUserId());
  35. $this->assertSame('hello world', $post->title);
  36. $this->assertSame('hello world', $post['title']);
  37. $this->assertSame('hello world', $post->getTitle());
  38. $this->assertSame('Say hello to the world.', $post->summary);
  39. $this->assertSame('Say hello to the world.', $post['summary']);
  40. $this->assertSame('Say hello to the world.', $post->getSummary());
  41. $postContent = $post->postContent;
  42. $this->assertInstanceof(PostContent::class, $postContent);
  43. $this->assertSame(1, $postContent->post_id);
  44. $this->assertSame(1, $postContent->postId);
  45. $this->assertSame(1, $postContent['post_id']);
  46. $this->assertSame(1, $postContent['postId']);
  47. $this->assertSame(1, $postContent->getPostId());
  48. $this->assertSame('I am content with big data.', $postContent->content);
  49. $this->assertSame('I am content with big data.', $postContent['content']);
  50. $this->assertSame('I am content with big data.', $postContent->getContent());
  51. }

eager 预加载关联

  1. public function testEager(): void
  2. {
  3. $post = Post::select()->where('id', 1)->findOne();
  4. $this->assertInstanceof(Post::class, $post);
  5. $this->assertNull($post->id);
  6. $connect = $this->createDatabaseConnect();
  7. for ($i = 0; $i <= 5; $i++) {
  8. $this->assertSame(
  9. $i + 1,
  10. $connect
  11. ->table('post')
  12. ->insert([
  13. 'title' => 'hello world',
  14. 'user_id' => 1,
  15. 'summary' => 'Say hello to the world.',
  16. 'delete_at' => 0,
  17. ])
  18. );
  19. $this->assertSame(
  20. 1,
  21. $connect
  22. ->table('post_content')
  23. ->insert([
  24. 'post_id' => $i + 1,
  25. 'content' => 'I am content with big data.',
  26. ])
  27. );
  28. }
  29. $posts = Post::eager(['post_content'])->findAll();
  30. $this->assertInstanceof(Collection::class, $posts);
  31. $this->assertCount(6, $posts);
  32. foreach ($posts as $value) {
  33. $postContent = $value->postContent;
  34. $this->assertInstanceof(PostContent::class, $postContent);
  35. $this->assertSame($value->id, $postContent->postId);
  36. $this->assertSame('I am content with big data.', $postContent->content);
  37. }
  38. }

eager 预加载关联支持查询条件过滤

  1. public function testEagerWithCondition(): void
  2. {
  3. $post = Post::select()->where('id', 1)->findOne();
  4. $this->assertInstanceof(Post::class, $post);
  5. $this->assertNull($post->id);
  6. $connect = $this->createDatabaseConnect();
  7. for ($i = 0; $i <= 5; $i++) {
  8. $this->assertSame(
  9. $i + 1,
  10. $connect
  11. ->table('post')
  12. ->insert([
  13. 'title' => 'hello world',
  14. 'user_id' => 1,
  15. 'summary' => 'Say hello to the world.',
  16. 'delete_at' => 0,
  17. ])
  18. );
  19. $this->assertSame(
  20. 1,
  21. $connect
  22. ->table('post_content')
  23. ->insert([
  24. 'post_id' => $i + 1,
  25. 'content' => 'I am content with big data.',
  26. ])
  27. );
  28. }
  29. $posts = Post::eager(['post_content' => function (Relation $select) {
  30. $select->where('post_id', '>', 99999);
  31. }])->findAll();
  32. $this->assertInstanceof(Collection::class, $posts);
  33. $this->assertCount(6, $posts);
  34. foreach ($posts as $value) {
  35. $postContent = $value->postContent;
  36. $this->assertInstanceof(PostContent::class, $postContent);
  37. $this->assertNotSame($value->id, $postContent->postId);
  38. $this->assertNotSame('I am content with big data.', $postContent->content);
  39. $this->assertNull($postContent->postId);
  40. $this->assertNull($postContent->content);
  41. }
  42. }

relation 读取关联

  1. public function testRelationAsMethod(): 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' => 'Say hello to the world.',
  12. 'delete_at' => 0,
  13. ])
  14. );
  15. $this->assertSame(
  16. 1,
  17. $connect
  18. ->table('post_content')
  19. ->insert([
  20. 'post_id' => 1,
  21. 'content' => 'I am content with big data.',
  22. ])
  23. );
  24. $postContentRelation = Post::make()->relation('postContent');
  25. $this->assertInstanceof(HasOne::class, $postContentRelation);
  26. $this->assertSame('id', $postContentRelation->getSourceKey());
  27. $this->assertSame('post_id', $postContentRelation->getTargetKey());
  28. $this->assertInstanceof(Post::class, $postContentRelation->getSourceEntity());
  29. $this->assertInstanceof(PostContent::class, $postContentRelation->getTargetEntity());
  30. $this->assertInstanceof(Select::class, $postContentRelation->getSelect());
  31. }

relation 关联模型数据不存在返回空实体

  1. public function testRelationDataWasNotFound(): void
  2. {
  3. $post = Post::select()->where('id', 1)->findOne();
  4. $this->assertInstanceof(Post::class, $post);
  5. $this->assertNull($post->id);
  6. $connect = $this->createDatabaseConnect();
  7. $this->assertSame(
  8. 1,
  9. $connect
  10. ->table('post')
  11. ->insert([
  12. 'title' => 'hello world',
  13. 'user_id' => 1,
  14. 'summary' => 'Say hello to the world.',
  15. 'delete_at' => 0,
  16. ])
  17. );
  18. $this->assertSame(
  19. 1,
  20. $connect
  21. ->table('post_content')
  22. ->insert([
  23. 'post_id' => 5,
  24. 'content' => 'I am content with big data.',
  25. ])
  26. );
  27. $post = Post::select()->where('id', 1)->findOne();
  28. $this->assertSame(1, $post->id);
  29. $this->assertSame(1, $post['id']);
  30. $this->assertSame(1, $post->getId());
  31. $this->assertSame(1, $post->user_id);
  32. $this->assertSame(1, $post->userId);
  33. $this->assertSame(1, $post['user_id']);
  34. $this->assertSame(1, $post->getUserId());
  35. $this->assertSame('hello world', $post->title);
  36. $this->assertSame('hello world', $post['title']);
  37. $this->assertSame('hello world', $post->getTitle());
  38. $this->assertSame('Say hello to the world.', $post->summary);
  39. $this->assertSame('Say hello to the world.', $post['summary']);
  40. $this->assertSame('Say hello to the world.', $post->getSummary());
  41. $postContent = $post->postContent;
  42. $this->assertInstanceof(PostContent::class, $postContent);
  43. $this->assertNull($postContent->post_id);
  44. $this->assertNull($postContent->postId);
  45. $this->assertNull($postContent['post_id']);
  46. $this->assertNull($postContent['postId']);
  47. $this->assertNull($postContent->getPostId());
  48. $this->assertNull($postContent->content);
  49. $this->assertNull($postContent['content']);
  50. $this->assertNull($postContent->getContent());
  51. }