我正在尝试使用表单更新数据库中的数据。
控制器是:
public function addAction($id) {
$em = $this->getDoctrine()->getEntityManager();
$product = $em->getRepository('AcmeStoreBundle:Product')->find($id);
if (!$product) {
$product = new Product();
}
$form = $this->createForm(new PageAdd(), $product);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
$name=$this->get('request')->request->get('name');
$price=$this->get('request')->request->get('price');
$description=$this->get('request')->request->get('description');
if ($form->isValid()) {
$product->setName($name);
$product->setPrice($price);
$product->setDescription($description);
$em->persist($product);
$em->flush();
/*Llamando a la plantilla de listados*/
$product = $em->getRepository('AcmeStoreBundle:Product')->findAll();
/*Enviando los datos a la plantilla y Renderizandola*/
return $this->render('AcmeStoreBundle:Default:pageadd.html.twig', array('Product' => $product));
}
}
return $this->render('AcmeStoreBundle:Default:show.html.twig', array('form' => $form->createView(), 'product' => $product));
}
文件show.html.twig
是
<form action="{{ path('Product_add',{'id':product.id}) }}"
{{ form_enctype(form) }}>
{{ form_errors(form) }}
{{ form_rest(form) }}
<input type="submit" value="Save This Page" class="savebutton" />
</form>
它给了我错误
AcmeStoreBundle 中不存在对象“Acme\StoreBundle\Entity\Product”的方法“id”:默认值:show.html.twig 第 2 500 行内部服务器错误 - Twig_Error_Runtime