我正在使用 symfony2.16 中产品的 id 更新值,也使用 MngoDBbundle
但我收到这样的错误
在第 1 行的 AcmeStoreBundle:Default:index.html.twig 中呈现模板期间引发了异常(““acme_store_update”路由缺少一些强制参数(“id”)。”)。
这是我的控制器,请检查更新操作
<?php
namespace Acme\StoreBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Acme\StoreBundle\Document\Product;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
/**
* @Route("/hello/{name}")
* @Template()
*/
public function indexAction($name)
{
return array('name' => $name);
}
// ...
/**
* @Route("/create", name="acme_store_create")
* @Template()
*/
public function createAction(Request $request)
{
$product = new Product();
//$product->setName('apple');
//$product->setPrice('19.99');
$form = $this->createFormBuilder($product)
->add('name', 'text')
->add('price', 'text')
->getForm();
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$req = $request->request->get('form');
$product->setName($req['name']);
$product->setPrice($req['price']);
$dm = $this->get('doctrine.odm.mongodb.document_manager');
$dm->persist($product);
$dm->flush();
return new Response('Name ='.$product->getName().', price ='.$product->getPrice());
}
}
return $this->render('AcmeStoreBundle:Default:index.html.twig', array(
'form' => $form->createView(),
));
}
// ...
/**
* @Route("/show/{id}", name="acme_store_show")
* @Template()
*/
public function showAction($id)
{
$product = $this->get('doctrine.odm.mongodb.document_manager')
->getRepository('AcmeStoreBundle:Product')
->find($id);
if (!$product) {
throw $this->createNotFoundException('No product found for id '.$id);
}
return new Response('Name ='.$product->getName().', price ='.$product->getPrice());
// do something, like pass the $product object into a template
}
// ...
/**
* @Route("/update/{id}", name="acme_store_update")
* @Template()
*/
public function updateAction(Request $request,$id)
{
$dm = $this->get('doctrine.odm.mongodb.document_manager');
$product = $dm->getRepository('AcmeStoreBundle:Product')->find($id);
$form = $this->createFormBuilder($product)
->add('name', 'text')
->add('price', 'text')
->getForm();
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$req = $request->request->get('form');
$product->setName($req['name']);
$product->setPrice($req['price']);
$dm->flush();
return new Response('Name ='.$product->getName().', price ='.$product->getPrice());
}
}
return $this->render('AcmeStoreBundle:Default:index.html.twig', array(
'form' => $form->createView(),
));
}
// ...
}
我的 index.html.twig 是
<form action="{{ path('acme_store_update') }}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<input type="submit" />
</form>
如何使用 id 更新值请帮助我