1
1

More than 1 year has passed since last update.

day17(?)の今日はエンティティ・モデルのデータに更新があるか(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->getUnitOfWork()->computeChangeSets();
if ($entityManager->getUnitOfWork()->isEntityScheduled($book1)) {
    echo 'dirty!'.PHP_EOL;
} else {
    echo 'not dirty!'.PHP_EOL;
}

https://github.com/77web/doctrine-vs-eloquent/blob/26769edf7f4874a14870758fd3272a7c844cae69/Doctrine/Usecase/check_if_dirty.php

  • EntityManagerのUnitOfWorkを使い、チェンジセットを計算させて、更新(UPDATE)が必要な状態かどうかを確認します。

Eloquent

<?php

declare(strict_types=1);

use App\Models\Book;

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

/** @var Book $book1 */
$book1 = Book::find(1);
$book1->title = 'Symfony6 The Fast Track';

if ($book1->isDirty()) {
    echo 'dirty!'.PHP_EOL;
} else {
    echo 'not dirty!'.PHP_EOL;
}

https://github.com/77web/doctrine-vs-eloquent/blob/26769edf7f4874a14870758fd3272a7c844cae69/Eloquent/Usecase/check_if_dirty.php

  • モデルの isDirty() メソッドを呼ぶだけでできます
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