LoginSignup
2
1

More than 1 year has passed since last update.

day5の今日は、1件のレコードを削除する(SQL的にはDELETE)やり方を見ていきます。

Doctrine

<?php

declare(strict_types=1);

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);

$entityManager->remove($book1);
$entityManager->flush();

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

  • day3の方法でBookエンティティのインスタンスを取得します。
  • EntityManagerの remove() メソッドにBookエンティティを渡し、その後にEntityManagerの flush() メソッドを呼びます。
  • DELETE文はremove()を呼び出したときでなくflush()を呼び出したときに実行されます。

Eloquent

<?php

declare(strict_types=1);

use App\Models\Book;

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

$book1 = Book::find(1);
$book1->delete();

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

  • day3 の方法でBookモデルのインスタンスを取得します。
  • Bookモデルのインスタンスの delete() メソッドを呼び出します。
2
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
2
1