0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

day8の今日はday7で定義したリレーション先テーブルのデータを利用する方法を見ていきます。

Doctrine

<?php

declare(strict_types=1);

use App\Entity\Author;
use App\Entity\Book;
use Doctrine\ORM\EntityManagerInterface;

require __DIR__.'/../vendor/autoload.php';

/** @var EntityManagerInterface $entityManager */
$entityManager = require __DIR__.'/bootstrap.php';

$book1 = $entityManager->getRepository(Book::class)->find(1);
echo $book1->getAuthor()->getName(); // Authorのnameを見ることができる

$author1 = $entityManager->getRepository(Author::class)->find(1);
// $author1の著作タイトルをすべて表示
foreach ($author1->getBooks() as $book) {
    echo $book->getTitle().PHP_EOL;
}

https://github.com/77web/doctrine-vs-eloquent/blob/f35c85cce094c389ca54689ca86925e58c66d3ce/Doctrine/Usecase/join.php

Eloquent

<?php

declare(strict_types=1);

use App\Models\Author;
use App\Models\Book;

require __DIR__.'/../vendor/autoload.php';

$book1 = Book::find(1);
echo $book1->author->name; // Authorのnameを見ることができる

$author1 = Author::find(1);
// $author1の著作タイトルをすべて表示
foreach ($author1->books as $book) {
    echo $book->title.PHP_EOL;
}

https://github.com/77web/doctrine-vs-eloquent/blob/f35c85cce094c389ca54689ca86925e58c66d3ce/Eloquent/Usecase/join.php

  • Doctrineと違い、リレーション定義時に定義したのは author() books() メソッドでしたがデータを読み取るのは author books プロパティになる点に注意が必要です。
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?