LoginSignup
1
1

More than 1 year has passed since last update.

day13(?)の今日はUNIONクエリを実行する方法を見ていきます。

Doctrine

<?php

declare(strict_types=1);

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

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

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

/** @var array<array{id: int, name: string}> $rows */
$rows = $entityManager->getConnection()->fetchAllAssociative('SELECT id, name FROM authors UNION SELECT id, title FROM books');

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

  • 複数のテーブルにまたがるunionの場合はエンティティにマップしてもしょうがないのでDBALを使ってarrayを取得します。
  • 単一のテーブルで異なる条件のSQL同士をUNIONしたい場合はNativeQueryを使ってエンティティにマップすることもできます。

Eloquent

<?php

declare(strict_types=1);

use App\Models\Author;
use App\Models\Book;
use Illuminate\Database\Eloquent\Collection;

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

/** @var Collection<Author> $authors */
$authors = Author::query()
    ->select('id', 'name')
    ->union(Book::query()->select('id', 'title'))
    ->get()
;
// $authorsにはid, nameのみを持つAuthorモデルのインスタンスと、booksテーブルのid, title(nameという名前で収納される)を持つAuthorモデルのインスタンスが混在したコレクション

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

  • Eloquentのunion機能を使った場合、BookモデルのデータであってもAuthorモデルにマップされます。(ちょっと気持ち悪いですね :sweat_smile:
  • DB::statement() を使うことで生SQLのUNIONクエリを実行し、arrayの形でレコードを得ることもできます。
1
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
1
1