2013-03-08 63 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')); 
    } 
}