关联

Testing Is Documentation

tests/Database/Ddd/Relation/RelationTest.php关联 - 图1

将相关实体连接起来,可以更加方便地操作数据。

关联支持类型

关联类型说明
belongsTo从属关联
hasOne一对一关联
hasMany一对多关联
manyMany多对多关联

Uses

  1. <?php
  2. use Exception;
  3. use Leevel\Database\Ddd\Relation\HasOne;
  4. use Leevel\Database\Ddd\Relation\Relation;
  5. use Tests\Database\DatabaseTestCase as TestCase;
  6. use Tests\Database\Ddd\Entity\Relation\Post;
  7. 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. }