1

我想做的是下一步:

  1. 使用 FormBuilder 创建简单的表单

  2. 提交表单时,将结果保存到特定用户的数据库中(基于其 ID)

另外是来自控制器的代码:

public function helloAction(Request $request, $id){//displaying individual results for particular user//


// find the username which was in the view//

  $em = $this->getDoctrine()->getManager();
        $query = $em->createQuery('SELECT b FROM AcmeWebBundle:baza b WHERE b.id = :id' )
        ->setParameter('id',$id);
        $total = $query->getResult();  

$baza = new baza ();

    $em = $this->getDoctrine()->getManager();
        $em->persist($baza);
        $form = $this->createFormBuilder($baza) 
                    ->add ('rating','choice',array('label'=>'TEST44','choices'=>array(
                        '1'=>'1',
                        '2'=>'2',
                        '3'=>'3',
                        '4'=>'4'
                        ),

                    'expanded'=>true,
                    'multiple'=>false
                    ))
                    ->getForm();


        if ($request->getMethod() == 'POST') {
            $form->bindRequest($request);

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

                return new Response('<h1>THANKS FOR Your feedback !!!!</h1>');

            }


        }


return $this->render('AcmeWebBundle:Default:hello.html.twig',array('all'=>$total,'id'=>$id ,'form'=>$form->createView()));
}
}

但这会在数据库中创建新行,并且只为评级列添加值。此外,id 字段、用户名等都是空的。

我想要做的是,要为列评级添加评级,但要为特定的 ID 添加评级。

4

2 回答 2

0

您可以像这样将评级设置为用户实体..

if ($form->isValid())
    $rating = $form->get('rating')->getData();
    $user->setRating($rating);
    // Assuming $user is the user entity

    // etc..
于 2013-04-05T13:52:27.650 回答
0

在下面的示例中,我创建了一个表单,获取数据POST,然后持久化新对象或修改过的对象,持久化一个空对象是没有意义的。

public function historialAction(Request $request)
{
$form = $this->createFormBuilder()
->add('phone', 'text', array('attr' => array('autofocus' => '')))
->add('period', 'number', array('attr' => array('value' => '12')))
->getForm();

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

    // data is an array with "phone" and "period" keys
    $data = $form->getData();

    $em = $this->getDoctrine()->getManager();

    $contract = $em->getRepository('FrontBundle:Contract')->getContractByPhone($data["phone"]);
    $contract->setOwner("John Doe");
    $contract->setPhone($data["phone"]);

    // or this could be $contract = new Contract("John Doe", $data["phone"], $data["period"]);


    $em->persist($contract); // I set/modify the properties then persist
}
于 2013-04-05T14:29:55.787 回答