LoginSignup
3
1

More than 1 year has passed since last update.

day4の今日はday3で取得したBookレコードのタイトルを更新する(SQL的にはUPDATEする)方法を見ていきます。

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);
$book1->setTitle('Symfony6 The Fast Track');

$entityManager->flush();

https://github.com/77web/doctrine-vs-eloquent/blob/11bda766bcf88a1e54ac015a328c5ee9d02acb11/Doctrine/Usecase/update_one_record.php

  • day3の方法でBookエンティティのインスタンスを取得します。
  • Bookエンティティのtitleプロパティの値を更新します。(setterを利用)
  • Bookエンティティを取得するのに使ったEntityManagerの flush() メソッドを呼びます。

Eloquent

<?php

declare(strict_types=1);

use App\Models\Book;

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

$book1 = Book::find(1);
$book1->title = 'Symfony6 The Fast Track';
$book1->save();

https://github.com/77web/doctrine-vs-eloquent/blob/11bda766bcf88a1e54ac015a328c5ee9d02acb11/Eloquent/Usecase/update_one_record.php

  • day3 の方法でBookモデルのインスタンスを取得します。
  • Bookインスタンスのtitleプロパティに新しい値(更新したい値)をセットします。
  • Bookインスタンスの save() メソッドを呼びます。
3
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
3
1