1
를 컨트롤러에 교리 Manager 인스턴스를 주입 Doctrine Manager 인스턴스를 수동으로 생성하지 않으면 더 편리 할 것입니다.심포니는 다음과 업데이트 작업을 정의 할 수 있습니다, 메소드 인자 컨트롤러에서
/**
* @Route("/product/edit/{id}")
*/
public function updateAction(Product $product, ObjectManager $em)
{
$product->setName("new name");
$em->flush();
}
대신 긴 코딩의 : 다음과 같이 당신이 할 수 있어야한다, 그래서 나는 아직 Symfony4을 시도했지만 공식 심포니 문서에 근거하지 않은
/**
* @Route("/product/edit/{id}")
*/
public function updateAction($id)
{
$em = $this->getDoctrine()->getManager();
$product = $em->getRepository(Product::class)->find($id);
if (!$product) {
throw $this->createNotFoundException(
'No product found for id '.$id
);
}
$product->setName('New product name!');
$em->flush();
return $this->redirectToRoute('app_product_show', [
'id' => $product->getId()
]);
}
덕분에, 그건 정말 마술! 하지만 어떻게 내부에서 작동합니까? – TangMonk
정말 모르겠지만 반사 수업을 통해 성취했다고 생각합니다. –