2013-03-08 127 views
-1

我是Symfony2中的新手。我的问题是:我想将Twig格式的值传递给数据库。 控制器:从Twig形式传递变量

public function createAction() 

{ 
    $product = new Product(); 
    $product->setName('name'); 
    $product->setPrice('price'); 
    $product->setDescription('description'); 

    $em = $this->getDoctrine()->getEntityManager(); 
    $em->persist($product); 
    $em->flush(); 

    return new Response('Izveidots produkts id '.$product->getId()); 
} 

我想从形式,而不是使用恒定的人(“姓名”,“价格”)的使用值。该视图:

<form action="{{ path('blogger_blog_create') }}" method="post" {{ form_enctype(form) }}> 
    {{ form_errors(form) }} 

    {{ form_row(form.name)}} 
    {{ form_row(form.price) }} 
    {{ form_row(form.description) }} 

    {{ form_rest(form) }} 

    <input type="submit" /> 
</form> 

我想弄明白了几个小时。

回答

2

您可以在official symfony documentation中找到所需的全部信息。最简单的例子:

if ($request->isMethod('POST')) { 
    $form->bind($request); 

    if ($form->isValid()) { 
     // perform some action, such as saving the task to the database 

     return $this->redirect($this->generateUrl('task_success')); 
    } 
}