我想通过更新开始日期和结束日期来更新我的所有具有相同名称的活动
我之前已经发布了这个问题,但现在我已经更改了一些代码,但仍然无效?
代码重定向到 app_event 路线而不更新!!
错误在哪里?
Controller.php:
public function updateEventsAction(Request $request )
{
$form = $this->createForm('AppBundle\Form\UpdateEventType');
$form->handleRequest($request);
$event_name=$form[name]->getData();
$choosedName = $event_name->getName(); //get name from combobox
$em = $this->getDoctrine()->getManager();
$repository = $this
->getDoctrine()
->getManager()
->getRepository('AppBundle:Event')
;
$events = $repository->findBy(
array('name' => $choosedName) // array of events have same name choosen from combobox
);
if ($form->isSubmitted() && $form->isValid()) {
//foreach event update start and end dates
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(),
));
}
Event.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'))
;
}
更新
现在我已经更新了我的代码,这似乎是一个日期格式问题,因为更新我的代码后这是错误:
An exception occurred while executing 'UPDATE event SET startDate = '$ choosedStartDate ', endDate = '$ choosedEndDate ' WHERE name = ?' with params ["Events16"]:
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '$choosedDateDebut' for column startDate at row 1
这是新代码
public function updateEventsAction(Request $request )
{
$form = $this->createForm('AppBundle\Form\UpdateEventType');
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$event_name=$form['name']->getData();
$choosedName = $event_name->getName();
$choosedStartDate=$form[startDate]->getData();
$choosedEndDate =$form[endDate]->getData();
print_r("***********choosed Start date****************"); print_r($choosedStartDate);
print_r("***********choosed end date****************"); print_r($choosedEndDate
$em = $this->getDoctrine()->getManager();
$repository = $this
->getDoctrine()
->getManager()
->getRepository('AppBundle:Event)
;
$listevents= $repository->findBy(
array('name' => $choosedName) // Critere
);
foreach ($listevents as $event) {
$datestart_bd =$event->getDateDebut();
print_r("Start date from database");
print_r($datestart _bd);
}
$q = $em->createQuery('update AppBundle\Entity\Event e SET e.startDate =\'$choosedStartDate \' , e.endDate =\'$choosedEndDate \' WHERE e.name= ?1' );
$q->setParameter(1, $choosedName);
$q->execute();
return $this->redirectToRoute('app_event');
}
return $this->render('AppBundle:Event:updateEvents.html.twig', array(
'form' => $form->createView(),
));
}
UPDATE2
现在代码工作正常我不再使用createQuery了
我的代码工作:
public function updateEventsAction(Request $request )
{
$form = $this->createForm('AppBundle\Form\UpdateEventType');
$form->handleRequest($request);
$event_name=$form[name]->getData();
$choosedName = $event_name->getName(); //get name from combobox
$choosedStartDate=$form[‘startDate’]->getData();
$choosedEndDate =$form[‘endDate’]->getData();
$em = $this->getDoctrine()->getManager();
$repository = $this
->getDoctrine()
->getManager()
->getRepository('AppBundle:Event')
;
$events = $repository->findBy(
array('name' => $choosedName) // array of events have same name choosen from combobox
);
if ($form->isSubmitted() && $form->isValid()) {
//foreach event update start and end dates
foreach ($events as $event) {
$event ->setStartDate($choosedStartDate)
$event ->setEndDate($choosedEndDate);
$em->flush();
}
return $this->redirectToRoute(‘app_event’);
}
return $this->render('AppBundle:Event:updateEvents.html.twig', array(
'form' => $form->createView(),
));
}
答案 0 :(得分:0)
当您从数据库中获取事件时,您正在为事件设置相同的日期,而您可能希望从表单中设置它们。
更改此代码片段:
foreach ($events as $event) {
//$event_name variable holds your form data
$event->setStartDate($event_name->getStartDate());
$event->setEndDate($event_name->getEndDate());
$em->persist($event);//note that you only need to persist new events, so this is unnecesarry
$em->flush();//I'd recommend 'batch' flushes
}
有关batch processing的更多信息。