symfony2:更新数据错误

时间:2012-07-26 12:09:50

标签: php symfony symfony-forms

我正在尝试使用表单从数据库更新数据。

控制器是:

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”:默认:第2行的show.html.twig   500内部服务器错误 - Twig_Error_Runtime

1 个答案:

答案 0 :(得分:0)

这意味着您的产品实体中缺少getId()方法。

public function getId()
{
  return $this->id;
}