更新我的数据库中具有相同名称的所有对象

时间:2017-06-15 13:14:43

标签: database symfony doctrine twig

我想为所有具有相同名称的事件更新StartDate和EndDate(在我的情况下,多个事件具有相同的名称)

从组合框中选择事件的名称,然后选择要更新的开始日期和结束日期

updateEvents.html.twig

{{ form_start(form}}

<div>
    <label for="name">Name of the event</label>
    <div >   {{ form_widget(form.name}}   </div>
</div>
<div >
    <label for="startDate ">Start date </label>
    <div >  {{ form_widget(form.startDate }}   </div>
</div>
<div> 
    <label for=" EndDate ">End date </label>
    <div>    {{ form_widget(form. endDate}}  </div>
</div>
    <div> 
        <button type="submit" ">Update</button>
    </div>
</div>

{{ form_end(form) }}

UpdateEventsType.php:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add(' name ',EntityType::class,array(
        'class' => 'AppBundle: Event', 'choice_label'=>'name')   )
        ->add(' startDate ', DateType::class, array(
            'widget' => 'single_text',
            // this is actually the default format for single_text
            'format' => 'yyyy-MM-dd'))
        ->add(' endDate ', DateType::class, array(
            'widget' => 'single_text',
            // this is actually the default format for single_text
            'format' => 'yyyy-MM-dd'))
    ;   
}

控制器:

public function updateEventsAction(Request $request )
{
    //how to recuperate $choosedName from the combobox????

$qb = $this->getDoctrine()->getManager()->createQueryBuilder();
$qb->select('e')
    ->from('AppBundle:Event', 'e')
    ->where('e.name = :choosedName')
    ->setParameter('name',$choosedName);
$events =  $qb->getQuery()->getResult();

$form = $this->createForm('AppBundle\Form\UpdateEventsType', $events);
foreach ($events as $event) {

    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $event ->setStartDate ($event ->getStartDate ());
        $event ->setEndDate ($event ->getEndDate ());
        $em->flush();
        return $this->redirectToRoute('app_event');
    }


    return $this->render('AppBundle:Event:updateEvents.html.twig', array(
        'form' => $form->createView(),
    ));
    }
}
    }

1 个答案:

答案 0 :(得分:0)

表单提交后,您必须执行操作

public function updateEventsAction(Request $request)
        {

            $form = $this->createForm('AppBundle\Form\ UpdateEventsType ', $ events);    
            $form->handleRequest($request);
            if ($form->isSubmitted() && $form->isValid()) {
                $em = $this->getDoctrine()->getManager();
                $choosedName = $form->getData()['name'];
                $qb = $this->getDoctrine()->getManager()->createQueryBuilder();
                $qb->select('e')->from('AppBundle:Event, 'e')->where('e.name = :choosedName')->setParameter(':choosedName',$choosedName);
                $events =  $qb->getQuery()->getResult();
                foreach ($events as $ event) {
                    $event ->setStartDate ($event ->getStartDate ());
                    $event ->setEndDate ($event ->getEndDate ());
                    $em->persist($event);
                }
                $em->flush();
                return $this->redirectToRoute('app_event');
            }

            return $this->render('AppBundle:Event:updateEvents.html.twig', array(
                'form' => $form->createView(),
            ));
            }
         }
     }

你的代码中有很多我的错误: - 缺少了这种信仰 - createQueryBuilder应该在存储库中完成 - 你的 - &gt; setParameter('name',$ choosedName)与你在你的地方放置的东西不同步

阅读更多文档以避免所有错误