我是symfony的初学者,我必须使用收集表格建立一个网站来预订节目的门票。
界面很简单,用户选择他想要的门票数量,然后显示尽可能多的表格原型(姓名和姓氏输入)作为所需的门票。 这部分适合我。
我在mysql中有2个表“预订”和“票证”,我希望在提交表单后获得此渲染:
表格预订:
预订ID = 1
表格票证
Ticket id =“1”name =“thomas”,surname =“good”,reservation_id =“1”
Ticket id =“2”name =“laura”,surname =“senior”,reservation_id =“1”
但是当我提交表单时,我的表中会得到+1保留,但我的票表中没有数据。
class AdvertController extends Controller
{
public function formAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$reservation = new Reservations();
$form = $this->createForm(new ReservationsType(), $reservation, array(
'action' => $this->generateUrl('louvre_pages_homepage'),
'method' => 'POST',
));
$form->handleRequest($request);
$em->persist($reservation);
$em->flush();
return $this->render('LouvrePagesBundle:Pages:index.html.twig', array(
'form' => $form->createView(),
));
}
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('tickets', CollectionType::class, array(
'entry_type' => TicketType::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'prototype' => true,
)
)
->add('save', 'submit', array(
'attr' => array(
'class' => 'btn btn-lg btn-success'
)
));
}
public function getName()
{
return 'reservations';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Louvre\PagesBundle\Entity\Reservations',
));
}
我的控制员是对的吗?
答案 0 :(得分:1)
我在formAction
中看到了2个问题:
$em->flush();
$em->persist($reservation);
尝试这样做:
if ($form->isSubmitted() && $form->isValid()) {
$em->persist($reservation);
$em->flush();
// now redirect, add flash message, etc.
}
请参阅http://symfony.com/doc/current/book/forms.html#handling-form-submissions