Slide 1

Slide 1 text

/ 147 30 Doctrine 方 2 025 / 11 / 2 4 PHP 香 2025 @ttskch  1 # #phpconkagawa

Slide 2

Slide 2 text

/ 147  2 Doctrine 人 🙋🙋

Slide 3

Slide 3 text

/ 147  3 Doctrine 人 🙋🙋

Slide 4

Slide 4 text

/ 147  4

Slide 5

Slide 5 text

/ 147  5

Slide 6

Slide 6 text

/ 147  6 Doctrine

Slide 7

Slide 7 text

/ 147  7 日 Doctrine 大風

Slide 8

Slide 8 text

/ 147  8 #phpconkagawa 𝕏 🤝✨

Slide 9

Slide 9 text

/ 147 PHP 14 Symfony 13 phpcon 2025 行 長 Kannade  9 @ttskch

Slide 10

Slide 10 text

/ 147 Doctrine Eloquent 比 Doctrine Doctrine 方  10 🔖

Slide 11

Slide 11 text

/ 147 Doctrine Eloquent 比 Doctrine Doctrine 方  11 🔖

Slide 12

Slide 12 text

/ 147 Symfony 用 PHP ORM Doctrine OSS Doctrine DBAL Doctrine ORM Doctrine ⾒ Doctrine ORM Doctrine  12

Slide 13

Slide 13 text

/ 147 Doctrine ORM Doctrine DBAL 用 Doctrine ORM Doctrine DBAL Doctrine  13

Slide 14

Slide 14 text

/ 147 Database Abstraction Layer DB DB DB DB DBAL DB DB 一 Doctrine DBAL  14

Slide 15

Slide 15 text

/ 147 Object-Relational Mapper OOP 言 RDB DB PHP ORM Eloquent ORM Laravel ORM Doctrine ORM  15

Slide 16

Slide 16 text

/ 147 Doctrine Eloquent 比 Doctrine Doctrine 方  16 🔖

Slide 17

Slide 17 text

/ 147 ORM 2 Eloquent Active Record Doctrine Data Mapper PoEAA Eloquent 比  17 https://www.martinfowler.com/eaaCatalog/activeRecord.html https://www.martinfowler.com/eaaCatalog/dataMapper.html

Slide 18

Slide 18 text

/ 147 自 DB 👍 手 高 👎 DB DB Active Record  18

Slide 19

Slide 19 text

/ 147 DB 👍 DB DB 👎 手 Data Mapper  19

Slide 20

Slide 20 text

/ 147 DB 👍 DB DB 👎 手 Data Mapper  20

Slide 21

Slide 21 text

/ 147 自 FW 用 ⾒ DB  21

Slide 22

Slide 22 text

/ 147 Active Record vs Data Mapper  22 Active Record Data Mapper 手 長 高 比 高

Slide 23

Slide 23 text

/ 147 Doctrine Eloquent 比 Doctrine Doctrine 方  23 🔖

Slide 24

Slide 24 text

/ 147  24 Doctrine PHP DB

Slide 25

Slide 25 text

/ 147  25 Doctrine PHP DB

Slide 26

Slide 26 text

/ 147  26

Slide 27

Slide 27 text

/ 147 Doctrine DDD 用 一 2 見  27

Slide 28

Slide 28 text

/ 147 Doctrine DDD DDD ⾒ DB 一 1 PoEAA Identity Map  28 https://martinfowler.com/eaaCatalog/identityMap.html

Slide 29

Slide 29 text

/ 147 // Eloquent dump(spl_object_hash( Article::find(1), )); dump(spl_object_hash( Article::find(1), )); dump(spl_object_hash( Article::find(1), )); // Doctrine dump(spl_object_hash( $articleRepository->find(1), )); dump(spl_object_hash( $articleRepository->find(1), )); dump(spl_object_hash( $articleRepository->find(1), ));  29 // "000000000000085b0000000000000000" // "000000000000085b0000000000000000" // "000000000000085b0000000000000000" // "00000000000012250000000000000000" // "00000000000011f90000000000000000" // "00000000000011f70000000000000000"

Slide 30

Slide 30 text

/ 147 Doctrine Data Mapper 方  30

Slide 31

Slide 31 text

/ 147 DB  31 PHP a b c a b c

Slide 32

Slide 32 text

/ 147  32 #[ORM\Entity] #[ORM\Table(name: 'article')] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(name: 'id', type: Types::INTEGER)] public ?int $id = null; #[ORM\Column(name: 'title', type: Types::STRING, length: 255)] public ?string $title = null; #[ORM\Column(name: 'content', type: Types::TEXT, nullable: true)] public ?string $content = null; }

Slide 33

Slide 33 text

/ 147  33 #[ORM\Entity] #[ORM\Table(name: 'article')] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(name: 'id', type: Types::INTEGER)] public ?int $id = null; #[ORM\Column(name: 'title', type: Types::STRING, length: 255)] public ?string $title = null; #[ORM\Column(name: 'content', type: Types::TEXT, nullable: true)] public ?string $content = null; } POPO Plain Old PHP Object DB

Slide 34

Slide 34 text

/ 147  34 POPO Plain Old PHP Object DB

Slide 35

Slide 35 text

/ 147  35 Doctrine PHP DB

Slide 36

Slide 36 text

/ 147  36

Slide 37

Slide 37 text

/ 147  37 Doctrine PHP DB

Slide 38

Slide 38 text

/ 147  38 Doctrine PHP DB 用 Doctrine

Slide 39

Slide 39 text

/ 147  39 Doctrine PHP DB Doctrine

Slide 40

Slide 40 text

/ 147  40 Doctrine PHP DB DB

Slide 41

Slide 41 text

/ 147  41 Doctrine PHP DB

Slide 42

Slide 42 text

/ 147  42 Doctrine PHP DB DB 用

Slide 43

Slide 43 text

/ 147  43 Doctrine PHP DB

Slide 44

Slide 44 text

/ 147  44 Doctrine PHP DB

Slide 45

Slide 45 text

/ 147 Doctrine Eloquent 比 Doctrine Doctrine 方  45 🔖 10

Slide 46

Slide 46 text

/ 147  46

Slide 47

Slide 47 text

/ 147 Data Mapper 方 3 見 DB Doctrine  47 DB

Slide 48

Slide 48 text

/ 147 言 3 大 Doctrine DB Doctrine  48 Doctrine DB

Slide 49

Slide 49 text

/ 147 Doctrine 3 Unit of Work 一 3 Doctrine Doctrine 方  49

Slide 50

Slide 50 text

/ 147 Doctrine 口 Doctrine  50

Slide 51

Slide 51 text

/ 147 Doctrine DB 一 PoEAA Unit of Work Unit of Work  51 https://www.martinfowler.com/eaaCatalog/unitOfWork.html

Slide 52

Slide 52 text

/ 147 DB PoEAA Repository  52 https://martinfowler.com/eaaCatalog/repository.html

Slide 53

Slide 53 text

/ 147  53 ・

Slide 54

Slide 54 text

/ 147 行 口 Doctrine DB  54

Slide 55

Slide 55 text

/ 147 DB  55 Unit of Work DB Doctrine

Slide 56

Slide 56 text

/ 147 DB  56 Unit of Work DB 口 Doctrine

Slide 57

Slide 57 text

/ 147 DB  57 Unit of Work DB Doctrine

Slide 58

Slide 58 text

/ 147 DB  58 Unit of Work DB DB Doctrine

Slide 59

Slide 59 text

/ 147 DB  59 Unit of Work DB Unit of Work Doctrine

Slide 60

Slide 60 text

/ 147 DB  60 Unit of Work DB Doctrine

Slide 61

Slide 61 text

/ 147  61

Slide 62

Slide 62 text

/ 147  62

Slide 63

Slide 63 text

/ 147  63 DB Unit of Work if 文

Slide 64

Slide 64 text

/ 147  64 DB Unit of Work if 文

Slide 65

Slide 65 text

/ 147 DB  65 Unit of Work DB Doctrine

Slide 66

Slide 66 text

/ 147 DB  66 Unit of Work DB Doctrine

Slide 67

Slide 67 text

/ 147 Doctrine DB Unit of Work DB Doctrine DB 口 Doctrine DB  67

Slide 68

Slide 68 text

/ 147 DB  68 Unit of Work DB Doctrine

Slide 69

Slide 69 text

/ 147 DB  69 Unit of Work DB Doctrine 口

Slide 70

Slide 70 text

/ 147 DB  70 Unit of Work DB Doctrine Unit of Work

Slide 71

Slide 71 text

/ 147 DB  71 Unit of Work DB Doctrine Unit of Work 自身 DB 行

Slide 72

Slide 72 text

/ 147 DB  72 Unit of Work DB Doctrine Unit of Work 自身

Slide 73

Slide 73 text

/ 147 DB  73 Unit of Work DB Doctrine 一

Slide 74

Slide 74 text

/ 147 DB  74 Unit of Work DB Doctrine

Slide 75

Slide 75 text

/ 147 DB  75 Unit of Work DB Doctrine

Slide 76

Slide 76 text

/ 147 Doctrine Eloquent 比 Doctrine Doctrine 方  76 🔖 17

Slide 77

Slide 77 text

/ 147  77 DB DB

Slide 78

Slide 78 text

/xxx ຊจϨϕϧ1 ຊจϨϕϧ2 ຊจϨϕϧ3 ຊจϨϕϧ4 ຊจϨϕϧ5 use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table(name: 'article')] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(name: 'id', type: Types::INTEGER)] public ?int $id = null; #[ORM\Column(name: 'title', type: Types::STRING, length: 255)] public ?string $title = null; #[ORM\Column(name: 'content', type: Types::TEXT, nullable: true)] public ?string $content = null; }

Slide 79

Slide 79 text

/xxx ຊจϨϕϧ1 ຊจϨϕϧ2 ຊจϨϕϧ3 ຊจϨϕϧ4 ຊจϨϕϧ5 use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table(name: 'article')] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(name: 'id', type: Types::INTEGER)] public ?int $id = null; #[ORM\Column(name: 'title', type: Types::STRING, length: 255)] public ?string $title = null; #[ORM\Column(name: 'content', type: Types::TEXT, nullable: true)] public ?string $content = null; } Article article

Slide 80

Slide 80 text

/xxx ຊจϨϕϧ1 ຊจϨϕϧ2 ຊจϨϕϧ3 ຊจϨϕϧ4 ຊจϨϕϧ5 use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table(name: 'article')] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(name: 'id', type: Types::INTEGER)] public ?int $id = null; #[ORM\Column(name: 'title', type: Types::STRING, length: 255)] public ?string $title = null; #[ORM\Column(name: 'content', type: Types::TEXT, nullable: true)] public ?string $content = null; } ORM Doctrine

Slide 81

Slide 81 text

/xxx ຊจϨϕϧ1 ຊจϨϕϧ2 ຊจϨϕϧ3 ຊจϨϕϧ4 ຊจϨϕϧ5 use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table(name: 'article')] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(name: 'id', type: Types::INTEGER)] public ?int $id = null; #[ORM\Column(name: 'title', type: Types::STRING, length: 255)] public ?string $title = null; #[ORM\Column(name: 'content', type: Types::TEXT, nullable: true)] public ?string $content = null; } $id ID Doctrine 自 id integer

Slide 82

Slide 82 text

/xxx ຊจϨϕϧ1 ຊจϨϕϧ2 ຊจϨϕϧ3 ຊจϨϕϧ4 ຊจϨϕϧ5 use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table(name: 'article')] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(name: 'id', type: Types::INTEGER)] public ?int $id = null; #[ORM\Column(name: 'title', type: Types::STRING, length: 255)] public ?string $title = null; #[ORM\Column(name: 'content', type: Types::TEXT, nullable: true)] public ?string $content = null; } $title title string 大長 255 文

Slide 83

Slide 83 text

/xxx ຊจϨϕϧ1 ຊจϨϕϧ2 ຊจϨϕϧ3 ຊจϨϕϧ4 ຊจϨϕϧ5 use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table(name: 'article')] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(name: 'id', type: Types::INTEGER)] public ?int $id = null; #[ORM\Column(name: 'title', type: Types::STRING, length: 255)] public ?string $title = null; #[ORM\Column(name: 'content', type: Types::TEXT, nullable: true)] public ?string $content = null; } $content content text NULL

Slide 84

Slide 84 text

/xxx ຊจϨϕϧ1 ຊจϨϕϧ2 ຊจϨϕϧ3 ຊจϨϕϧ4 ຊจϨϕϧ5 use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table(name: 'article')] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(name: 'id', type: Types::INTEGER)] public ?int $id = null; #[ORM\Column(name: 'title', type: Types::STRING, length: 255)] public ?string $title = null; #[ORM\Column(name: 'content', type: Types::TEXT, nullable: true)] public ?string $content = null; }

Slide 85

Slide 85 text

/xxx ຊจϨϕϧ1 ຊจϨϕϧ2 ຊจϨϕϧ3 ຊจϨϕϧ4 ຊจϨϕϧ5 use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table(name: 'article')] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(name: 'id', type: Types::INTEGER)] public ?int $id = null; #[ORM\Column(name: 'title', type: Types::STRING, length: 255)] public ?string $title = null; #[ORM\Column(name: 'content', type: Types::TEXT, nullable: true)] public ?string $content = null; } 方

Slide 86

Slide 86 text

/ 147 Symfony Doctrine ⾒ bin/console make:entity 行 大 🙆  86

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

✨

Slide 89

Slide 89 text

/xxx ຊจϨϕϧ1 ຊจϨϕϧ2 ຊจϨϕϧ3 ຊจϨϕϧ4 ຊจϨϕϧ5 use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table(name: 'article')] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(name: 'id', type: Types::INTEGER)] public ?int $id = null; #[ORM\Column(name: 'title', type: Types::STRING, length: 255)] public ?string $title = null; #[ORM\Column(name: 'content', type: Types::TEXT, nullable: true)] public ?string $content = null; }

Slide 90

Slide 90 text

/xxx ຊจϨϕϧ1 ຊจϨϕϧ2 ຊจϨϕϧ3 ຊจϨϕϧ4 ຊจϨϕϧ5 use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table(name: 'article')] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(name: 'id', type: Types::INTEGER)] public ?int $id = null; #[ORM\Column(name: 'title', type: Types::STRING, length: 255)] public ?string $title = null; #[ORM\Column(name: 'content', type: Types::TEXT, nullable: true)] public ?string $content = null; }

Slide 91

Slide 91 text

/xxx ຊจϨϕϧ1 ຊจϨϕϧ2 ຊจϨϕϧ3 ຊจϨϕϧ4 ຊจϨϕϧ5 use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table(name: 'article')] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(name: 'id', type: Types::INTEGER)] public ?int $id = null; #[ORM\Column(name: 'title', type: Types::STRING, length: 255)] public ?string $title = null; #[ORM\Column(name: 'content', type: Types::TEXT, nullable: true)] public ?string $content = null; } string integer text

Slide 92

Slide 92 text

/xxx ຊจϨϕϧ1 ຊจϨϕϧ2 ຊจϨϕϧ3 ຊจϨϕϧ4 ຊจϨϕϧ5 use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table(name: 'article')] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(name: 'id', type: Types::INTEGER)] public ?int $id = null; #[ORM\Column(name: 'title', type: Types::STRING, length: 255)] public ?string $title = null; #[ORM\Column(name: 'content', type: Types::TEXT, nullable: true)] public ?string $content = null; } string integer text 身 文

Slide 93

Slide 93 text

/ 147 integer string text Doctrine DBAL DB  93 DBAL MySQL PostgreSQL SQLite integer INT UNSIGNED INT INTEGER string VARCHAR VARCHAR VARCHAR text TEXT TEXT VARCHAR(MAX) https://www.doctrine-project.org/projects/doctrine-dbal/en/ 4 . 3 /reference/types.html#mapping-matrix

Slide 94

Slide 94 text

make:entity ? DBAL 一 見 大 🙆

Slide 95

Slide 95 text

/ 147  95 DB DB

Slide 96

Slide 96 text

/ 147  96 DB DB

Slide 97

Slide 97 text

/ 147  97 DB DB DB

Slide 98

Slide 98 text

/ 147 DB  98 Unit of Work DB Doctrine

Slide 99

Slide 99 text

/ 147  99 DB

Slide 100

Slide 100 text

/ 147 // ΤϯςΟςΟϚωʔδϟʔ͔Β // ArticleΤϯςΟςΟ༻ͷϦϙδτϦΛऔಘ $articleRepository = $entityManager->getRepository(Article::class);  100 DB

Slide 101

Slide 101 text

/ 147 // ΤϯςΟςΟϚωʔδϟʔ͔Β // ArticleΤϯςΟςΟ༻ͷϦϙδτϦΛऔಘ $articleRepository = $entityManager->getRepository(Article::class); // ϦϙδτϦܦ༝Ͱ // ID͕1ͷArticleΤϯςΟςΟΛऔಘ $article = $articleRepository->find(1);  101 DB

Slide 102

Slide 102 text

/ 147 // ΤϯςΟςΟϚωʔδϟʔ͔Β // ArticleΤϯςΟςΟ༻ͷϦϙδτϦΛऔಘ $articleRepository = $entityManager->getRepository(Article::class); // ϦϙδτϦܦ༝Ͱ // ID͕1ͷArticleΤϯςΟςΟΛऔಘ $article = $articleRepository->find(1);  102 DB Doctrine 色 💡

Slide 103

Slide 103 text

/ 147 // ΤϯςΟςΟϚωʔδϟʔ͔Β // ArticleΤϯςΟςΟ༻ͷϦϙδτϦΛऔಘ $articleRepository = $entityManager->getRepository(Article::class); // ϦϙδτϦܦ༝Ͱ // ID͕1ͷArticleΤϯςΟςΟΛऔಘ $article = $articleRepository->find(1);  103 $entityManager

Slide 104

Slide 104 text

/ 147 // ΤϯςΟςΟϚωʔδϟʔ͔Β // ArticleΤϯςΟςΟ༻ͷϦϙδτϦΛऔಘ $articleRepository = $entityManager->getRepository(Article::class); // ϦϙδτϦܦ༝Ͱ // ID͕1ͷArticleΤϯςΟςΟΛऔಘ $article = $articleRepository->find(1);  104 $entityManager

Slide 105

Slide 105 text

/ 147 #[Route('/article', name: 'article_')] class ArticleController extends AbstractController { #[Route('/', name: 'index', methods: ['GET'])] public function index(EntityManagerInterface $entityManager): Response { $articleRepository = $entityManager->getRepository(Article::class); // ... } }  105 DI Symfony

Slide 106

Slide 106 text

/ 147 #[Route('/article', name: 'article_')] class ArticleController extends AbstractController { #[Route('/', name: 'index', methods: ['GET'])] public function index(EntityManagerInterface $entityManager): Response { $articleRepository = $entityManager->getRepository(Article::class); // ... } }  106 DI Symfony

Slide 107

Slide 107 text

/ 147 #[Route('/article', name: 'article_')] class ArticleController extends AbstractController { #[Route('/', name: 'index', methods: ['GET'])] public function index(EntityManagerInterface $entityManager): Response { $articleRepository = $entityManager->getRepository(Article::class); // ... } }  107 DI Symfony DI 🙏

Slide 108

Slide 108 text

/ 147 // ΤϯςΟςΟϚωʔδϟʔ͔Β // ArticleΤϯςΟςΟ༻ͷϦϙδτϦΛऔಘ $articleRepository = $entityManager->getRepository(Article::class); // ϦϙδτϦܦ༝Ͱ // ID͕1ͷArticleΤϯςΟςΟΛऔಘ $article = $articleRepository->find(1);  108

Slide 109

Slide 109 text

/ 147 // ΤϯςΟςΟϚωʔδϟʔ͔Β // ArticleΤϯςΟςΟ༻ͷϦϙδτϦΛऔಘ $articleRepository = $entityManager->getRepository(Article::class); // ϦϙδτϦܦ༝Ͱ // ID͕1ͷArticleΤϯςΟςΟΛऔಘ $article = $articleRepository->find(1);  109 $articleRepository-> fi nd(1)

Slide 110

Slide 110 text

/ 147  110 fi nd() fi ndAll() fi ndBy() ⾒ fi ndOneBy() ⾒ 一

Slide 111

Slide 111 text

/ 147 // ID͕1ͷArticleΤϯςΟςΟΛऔಘ $article = $articleRepository->find(1); // ͢΂ͯͷArticleΤϯςΟςΟͷ഑ྻΛऔಘ $articles = $articleRepository->findAll(); // ެ։ࡁΈͷArticleΤϯςΟςΟͷ഑ྻΛऔಘ $articles = $articleRepository->findBy(['published' => true]); // ެ։ࡁΈ͔ͭɺλΠτϧ͕ʮPHPΧϯϑΝϨϯε߳઒ʯͰ͋ΔArticleΤϯςΟςΟΛ1ͭऔಘ $article = $articleRepository->findOneBy([ 'published' => true, 'title' => 'PHPΧϯϑΝϨϯε߳઒', ]);  111

Slide 112

Slide 112 text

/ 147 // ID͕1ͷArticleΤϯςΟςΟΛऔಘ $article = $articleRepository->find(1); // ͢΂ͯͷArticleΤϯςΟςΟͷ഑ྻΛऔಘ $articles = $articleRepository->findAll(); // ެ։ࡁΈͷArticleΤϯςΟςΟͷ഑ྻΛऔಘ $articles = $articleRepository->findBy(['published' => true]); // ެ։ࡁΈ͔ͭɺλΠτϧ͕ʮPHPΧϯϑΝϨϯε߳઒ʯͰ͋ΔArticleΤϯςΟςΟΛ1ͭऔಘ $article = $articleRepository->findOneBy([ 'published' => true, 'title' => 'PHPΧϯϑΝϨϯε߳઒', ]);  112

Slide 113

Slide 113 text

/ 147 // ެ։ࡁΈ͔ͭɺ // λΠτϧʹʮPHPΧϯϑΝϨϯε߳઒ʯΛؚΉArticleΤϯςΟςΟͷ഑ྻΛɺ // IDͷ߱ॱͰऔಘ $articles = $articleRepository->...  113

Slide 114

Slide 114 text

/ 147 // ެ։ࡁΈ͔ͭɺ // λΠτϧʹʮPHPΧϯϑΝϨϯε߳઒ʯΛؚΉArticleΤϯςΟςΟͷ഑ྻΛɺ // IDͷ߱ॱͰऔಘ $articles = $articleRepository->...  114 🤔

Slide 115

Slide 115 text

/ 147 ⾒ SQL 立  115

Slide 116

Slide 116 text

/ 147 // ެ։ࡁΈ͔ͭɺ // λΠτϧʹʮPHPΧϯϑΝϨϯε߳઒ʯΛؚΉArticleΤϯςΟςΟͷ഑ྻΛɺ // IDͷ߱ॱͰऔಘ $articles = $articleRepository->...  116

Slide 117

Slide 117 text

/ 147 // ެ։ࡁΈ͔ͭɺ // λΠτϧʹʮPHPΧϯϑΝϨϯε߳઒ʯΛؚΉArticleΤϯςΟςΟͷ഑ྻΛɺ // IDͷ߱ॱͰऔಘ $articles = $articleRepository->createQueryBuilder('a') ->where('a.published = :published') ->andWhere('a.title LIKE :query') ->orderBy('a.id', 'DESC') ->setParameter('published', true) ->setParameter('query', '%PHPΧϯϑΝϨϯε߳઒%') ->getQuery() ->getResult();  117

Slide 118

Slide 118 text

/ 147 // ެ։ࡁΈ͔ͭɺ // λΠτϧʹʮPHPΧϯϑΝϨϯε߳઒ʯΛؚΉArticleΤϯςΟςΟͷ഑ྻΛɺ // IDͷ߱ॱͰऔಘ $articles = $articleRepository->createQueryBuilder('a') ->where('a.published = :published') ->andWhere('a.title LIKE :query') ->orderBy('a.id', 'DESC') ->setParameter('published', true) ->setParameter('query', '%PHPΧϯϑΝϨϯε߳઒%') ->getQuery() ->getResult();  118 生 立 Article a

Slide 119

Slide 119 text

/ 147 // ެ։ࡁΈ͔ͭɺ // λΠτϧʹʮPHPΧϯϑΝϨϯε߳઒ʯΛؚΉArticleΤϯςΟςΟͷ഑ྻΛɺ // IDͷ߱ॱͰऔಘ $articles = $articleRepository->createQueryBuilder('a') ->where('a.published = :published') ->andWhere('a.title LIKE :query') ->orderBy('a.id', 'DESC') ->setParameter('published', true) ->setParameter('query', '%PHPΧϯϑΝϨϯε߳઒%') ->getQuery() ->getResult();  119 WHERE :published

Slide 120

Slide 120 text

/ 147 // ެ։ࡁΈ͔ͭɺ // λΠτϧʹʮPHPΧϯϑΝϨϯε߳઒ʯΛؚΉArticleΤϯςΟςΟͷ഑ྻΛɺ // IDͷ߱ॱͰऔಘ $articles = $articleRepository->createQueryBuilder('a') ->where('a.published = :published') ->andWhere('a.title LIKE :query') ->orderBy('a.id', 'DESC') ->setParameter('published', true) ->setParameter('query', '%PHPΧϯϑΝϨϯε߳઒%') ->getQuery() ->getResult();  120 WHERE :query

Slide 121

Slide 121 text

/ 147 // ެ։ࡁΈ͔ͭɺ // λΠτϧʹʮPHPΧϯϑΝϨϯε߳઒ʯΛؚΉArticleΤϯςΟςΟͷ഑ྻΛɺ // IDͷ߱ॱͰऔಘ $articles = $articleRepository->createQueryBuilder('a') ->where('a.published = :published') ->andWhere('a.title LIKE :query') ->orderBy('a.id', 'DESC') ->setParameter('published', true) ->setParameter('query', '%PHPΧϯϑΝϨϯε߳઒%') ->getQuery() ->getResult();  121 ORDER BY

Slide 122

Slide 122 text

/ 147 // ެ։ࡁΈ͔ͭɺ // λΠτϧʹʮPHPΧϯϑΝϨϯε߳઒ʯΛؚΉArticleΤϯςΟςΟͷ഑ྻΛɺ // IDͷ߱ॱͰऔಘ $articles = $articleRepository->createQueryBuilder('a') ->where('a.published = :published') ->andWhere('a.title LIKE :query') ->orderBy('a.id', 'DESC') ->setParameter('published', true) ->setParameter('query', '%PHPΧϯϑΝϨϯε߳઒%') ->getQuery() ->getResult();  122 :published :query

Slide 123

Slide 123 text

/ 147 // ެ։ࡁΈ͔ͭɺ // λΠτϧʹʮPHPΧϯϑΝϨϯε߳઒ʯΛؚΉArticleΤϯςΟςΟͷ഑ྻΛɺ // IDͷ߱ॱͰऔಘ $articles = $articleRepository->createQueryBuilder('a') ->where('a.published = :published') ->andWhere('a.title LIKE :query') ->orderBy('a.id', 'DESC') ->setParameter('published', true) ->setParameter('query', '%PHPΧϯϑΝϨϯε߳઒%') ->getQuery() ->getResult();  123 立

Slide 124

Slide 124 text

/ 147 // ެ։ࡁΈ͔ͭɺ // λΠτϧʹʮPHPΧϯϑΝϨϯε߳઒ʯΛؚΉArticleΤϯςΟςΟͷ഑ྻΛɺ // IDͷ߱ॱͰऔಘ $articles = $articleRepository->createQueryBuilder('a') ->where('a.published = :published') ->andWhere('a.title LIKE :query') ->orderBy('a.id', 'DESC') ->setParameter('published', true) ->setParameter('query', '%PHPΧϯϑΝϨϯε߳઒%') ->getQuery() ->getResult();  124 SQL 行

Slide 125

Slide 125 text

/ 147 // ެ։ࡁΈ͔ͭɺ // λΠτϧʹʮPHPΧϯϑΝϨϯε߳઒ʯΛؚΉArticleΤϯςΟςΟͷ഑ྻΛɺ // IDͷ߱ॱͰऔಘ $articles = $articleRepository->createQueryBuilder('a') ->where('a.published = :published') ->andWhere('a.title LIKE :query') ->orderBy('a.id', 'DESC') ->setParameter('published', true) ->setParameter('query', '%PHPΧϯϑΝϨϯε߳઒%') ->getQuery() ->getResult();  125

Slide 126

Slide 126 text

/ 147 // ެ։ࡁΈ͔ͭɺ // λΠτϧʹʮPHPΧϯϑΝϨϯε߳઒ʯΛؚΉArticleΤϯςΟςΟͷ഑ྻΛɺ // IDͷ߱ॱͰऔಘ $articles = $articleRepository->createQueryBuilder('a') ->where('a.published = :published') ->andWhere('a.title LIKE :query') ->orderBy('a.id', 'DESC') ->setParameter('published', true) ->setParameter('query', '%PHPΧϯϑΝϨϯε߳઒%') ->getQuery() ->getResult();  126

Slide 127

Slide 127 text

/ 147  127 DB DB DB

Slide 128

Slide 128 text

/ 147 DB  128 Unit of Work DB Doctrine

Slide 129

Slide 129 text

/ 147 // લఏͱͯ͠ϦϙδτϦ͔ΒΤϯςΟςΟΛऔಘ $article = $articleRepository->find(1); // ී௨ʹΤϯςΟςΟͷ಺༰Λมߋ͢Ε͹ // Unit of Work͕ͦͷมߋ಺༰Λݕ஌͢Δ $article->title = '৽͍͠λΠτϧ'; // ͦͯ͠...  129 DB

Slide 130

Slide 130 text

/ 147 // લఏͱͯ͠ϦϙδτϦ͔ΒΤϯςΟςΟΛऔಘ $article = $articleRepository->find(1); // ී௨ʹΤϯςΟςΟͷ಺༰Λมߋ͢Ε͹ // Unit of Work͕ͦͷมߋ಺༰Λݕ஌͢Δ $article->title = '৽͍͠λΠτϧ'; // ͦͯ͠... // มߋ಺༰ͷDB΁ͷ൓өΛґཔ $entityManager->flush();  130 DB

Slide 131

Slide 131 text

/ 147 // લఏͱͯ͠ϦϙδτϦ͔ΒΤϯςΟςΟΛऔಘ $article = $articleRepository->find(1); // ී௨ʹΤϯςΟςΟͷ಺༰Λมߋ͢Ε͹ // Unit of Work͕ͦͷมߋ಺༰Λݕ஌͢Δ $article->title = '৽͍͠λΠτϧ'; // ͦͯ͠... // มߋ಺༰ͷDB΁ͷ൓өΛґཔ $entityManager->flush();  131 DB DB Unit of Work 💡

Slide 132

Slide 132 text

/ 147 // ΤϯςΟςΟΛ৽ن࡞੒ $article = new Article(); $article->title = 'λΠτϧ'; // ΤϯςΟςΟΛUnit of Workʹ௥Ճ $entityManager->persist($article); // มߋ಺༰ͷDB΁ͷ൓өΛґཔ $entityManager->flush();  132

Slide 133

Slide 133 text

/ 147 // ΤϯςΟςΟΛ৽ن࡞੒ $article = new Article(); $article->title = 'λΠτϧ'; // ΤϯςΟςΟΛUnit of Workʹ௥Ճ $entityManager->persist($article); // มߋ಺༰ͷDB΁ͷ൓өΛґཔ $entityManager->flush();  133 Unit of Work 行 🤦

Slide 134

Slide 134 text

/ 147 // ΤϯςΟςΟΛ৽ن࡞੒ʢ͜ͷ࣌఺Ͱ͸·ͩUnit of Workͷ؅ཧԼʹͳ͍ʣ $article = new Article(); $article->title = 'λΠτϧ'; // ΤϯςΟςΟΛUnit of Workʹొ࿥ $entityManager->persist($article); // มߋ಺༰ʢ৽ن௥ՃΛؚΉʣͷDB΁ͷ൓өΛґཔ $entityManager->flush();  134 fl ush()

Slide 135

Slide 135 text

/ 147 // લఏͱͯ͠ϦϙδτϦ͔ΒΤϯςΟςΟΛऔಘ $article = $articleRepository->find(1); // ͦͯ͠...  135

Slide 136

Slide 136 text

/ 147 // લఏͱͯ͠ϦϙδτϦ͔ΒΤϯςΟςΟΛऔಘ $article = $articleRepository->find(1); // ͦͯ͠... // ΤϯςΟςΟΛ࡟আ͢ΔҙࢥΛUnit of Workʹ఻͑Δ $entityManager->remove($article); // มߋ಺༰ʢ࡟আΛؚΉʣͷDB΁ͷ൓өΛґཔ $entityManager->flush();  136

Slide 137

Slide 137 text

/ 147 // લఏͱͯ͠ϦϙδτϦ͔ΒΤϯςΟςΟΛऔಘ $article = $articleRepository->find(1); // ͦͯ͠... // ΤϯςΟςΟΛ࡟আ͢ΔҙࢥΛUnit of Workʹ఻͑Δ $entityManager->remove($article); // มߋ಺༰ʢ࡟আΛؚΉʣͷDB΁ͷ൓өΛґཔ $entityManager->flush();  137 Unit of Work

Slide 138

Slide 138 text

/ 147 // ΤϯςΟςΟΛ৽ن࡞੒ʢ͜ͷ࣌఺Ͱ͸·ͩUnit of Workͷ؅ཧԼʹͳ͍ʣ $article = new Article(); $article->title = 'λΠτϧ'; // ΤϯςΟςΟΛ࡟আ͢ΔҙࢥΛUnit of Workʹ఻͑Δ $entityManager->remove($article); // มߋ಺༰ʢ࡟আΛؚΉʣͷDB΁ͷ൓өΛґཔ $entityManager->flush();  138

Slide 139

Slide 139 text

/ 147 // ΤϯςΟςΟΛ৽ن࡞੒ʢ͜ͷ࣌఺Ͱ͸·ͩUnit of Workͷ؅ཧԼʹͳ͍ʣ $article = new Article(); $article->title = 'λΠτϧ'; // ΤϯςΟςΟΛ࡟আ͢ΔҙࢥΛUnit of Workʹ఻͑Δ $entityManager->remove($article); // มߋ಺༰ʢ࡟আΛؚΉʣͷDB΁ͷ൓өΛґཔ $entityManager->flush();  139 Unit of Work 示 行 ✋

Slide 140

Slide 140 text

/ 147  140 DB ・ ・ DB DB

Slide 141

Slide 141 text

/ 147  141 DB ・ ・ DB DB

Slide 142

Slide 142 text

/ 147 Doctrine Eloquent 比 Doctrine Doctrine 方  142 🔖

Slide 143

Slide 143 text

/ 147 Doctrine Data Mapper 方 手 ・ 心 Doctrine Doctrine ・ Unit of Work ・ 3 方・ 方 方 ・ ・ 方 30  143

Slide 144

Slide 144 text

/ 147  144

Slide 145

Slide 145 text

/ 147  145

Slide 146

Slide 146 text

/ 147  146

Slide 147

Slide 147 text

/ 147  147 ・ 🫰 @ttskch Thanks! /

Slide 148

Slide 148 text

/ 147  148 用

Slide 149

Slide 149 text

/ 147  149 $ bin/console make:entity Article

Slide 150

Slide 150 text

No content

Slide 151

Slide 151 text

/xxx ຊจϨϕϧ1 ຊจϨϕϧ2 ຊจϨϕϧ3 ຊจϨϕϧ4 ຊจϨϕϧ5 use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table(name: 'article')] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(name: 'id', type: Types::INTEGER)] public ?int $id = null; #[ORM\Column(name: 'title', type: Types::STRING, length: 255)] public ?string $title = null; #[ORM\Column(name: 'content', type: Types::TEXT, nullable: true)] public ?string $content = null; }

Slide 152

Slide 152 text

/ 147  152

Slide 153

Slide 153 text

/xxx ຊจϨϕϧ1 ຊจϨϕϧ2 ຊจϨϕϧ3 ຊจϨϕϧ4 ຊจϨϕϧ5 use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table(name: 'article')] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(name: 'id', type: Types::INTEGER)] public ?int $id = null; #[ORM\Column(name: 'title', type: Types::STRING, length: 255)] public ?string $title = null; #[ORM\Column(name: 'content', type: Types::TEXT, nullable: true)] public ?string $content = null; }

Slide 154

Slide 154 text

/xxx ຊจϨϕϧ1 ຊจϨϕϧ2 ຊจϨϕϧ3 ຊจϨϕϧ4 ຊจϨϕϧ5 use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] #[ORM\Table(name: 'article')] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(name: 'id', type: Types::INTEGER)] public ?int $id = null; #[ORM\Column(name: 'title', type: Types::STRING, length: 255)] public ?string $title = null; #[ORM\Column(name: 'content', type: Types::TEXT, nullable: true)] public ?string $content = null; }

Slide 155

Slide 155 text

/xxx ຊจϨϕϧ1 ຊจϨϕϧ2 ຊจϨϕϧ3 ຊจϨϕϧ4 ຊจϨϕϧ5 use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: Types::INTEGER)] public ?int $id = null; #[ORM\Column(type: Types::STRING, length: 255)] public ?string $title = null; #[ORM\Column(type: Types::TEXT, nullable: true)] public ?string $content = null; }

Slide 156

Slide 156 text

/xxx ຊจϨϕϧ1 ຊจϨϕϧ2 ຊจϨϕϧ3 ຊจϨϕϧ4 ຊจϨϕϧ5 use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: Types::INTEGER)] public ?int $id = null; #[ORM\Column(type: Types::STRING, length: 255)] public ?string $title = null; #[ORM\Column(type: Types::TEXT, nullable: true)] public ?string $content = null; } PHP PHP

Slide 157

Slide 157 text

/xxx ຊจϨϕϧ1 ຊจϨϕϧ2 ຊจϨϕϧ3 ຊจϨϕϧ4 ຊจϨϕϧ5 use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: Types::INTEGER)] public ?int $id = null; #[ORM\Column(type: Types::STRING, length: 255)] public ?string $title = null; #[ORM\Column(type: Types::TEXT, nullable: true)] public ?string $content = null; } PHP string DBAL text string 示

Slide 158

Slide 158 text

/xxx ຊจϨϕϧ1 ຊจϨϕϧ2 ຊจϨϕϧ3 ຊจϨϕϧ4 ຊจϨϕϧ5 use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity] class Article { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] public ?int $id = null; #[ORM\Column(length: 255)] public ?string $title = null; #[ORM\Column(type: Types::TEXT, nullable: true)] public ?string $content = null; }

Slide 159

Slide 159 text

No content

Slide 160

Slide 160 text

No content

Slide 161

Slide 161 text

1 方 make:entity

Slide 162

Slide 162 text

Symfony src 自 ArticleRepository DI 入

Slide 163

Slide 163 text

/ 147  163

Slide 164

Slide 164 text

/ 147 #[Route('/article', name: 'article_')] class ArticleController extends AbstractController { #[Route('/', name: 'index', methods: ['GET'])] public function index(ArticleRepository $articleRepository): Response { // ... } }  164

Slide 165

Slide 165 text

/ 147  165 2

Slide 166

Slide 166 text

/ 147  166

Slide 167

Slide 167 text

/ 147  167

Slide 168

Slide 168 text

https://qiita.com/advent-calendar/ 2022 /php-doctrine-orm-vs-eloquent

Slide 169

Slide 169 text

/ 147  169