LoginSignup
0
0

More than 5 years have passed since last update.

OneToMany データ更新(Update)

Posted at

はじめに

OneToManyの関係のテーブルにデータを更新(Update)する。
OneToMany データ取得(Select)」の続き。

データ更新(Update)

Controllerよりアクセス

ControllerからRepository経由でアクセス

app/Customize/Controller/SamplePageController.php
/**
 * @Route("/sample/{id}", name="sample_index")
 * @Template("Sample/index.twig")
 */
public function index($id)
{
    $product = $this->productRepository->find($id);
    dump($product);

    $comments = $product->getProductComments();
    if (!$comments->isEmpty()) {
        $keys = $comments->getKeys();
        foreach ($keys as $key) {
            $comment = $comments->get($key);
            $comment->setComment(date("Y/m/d H:i:s")."=\r\n".$comment->getComment());
        }
        $this->entityManager->persist($product);
        $this->entityManager->flush();
    } else {
    }
}

ブラウザよりアクセス

/sample/1 でdtb_product_commentのデータを更新できた。

まとめ

find()して値を設定してpersist()してflush()

参照

OneToMany データ取得(Select)

0
0
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
0