はじめに
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()