public function addcontestAction(Request $request)
{
$contest = new Contest();
$contest
->setName('Новый конкурс');
$form = $this->createForm(new AddType(), $contest);
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if($form->isValid()){
$em = $this->getDoctrine()->getManager();
$em->persist($contest);
//var_dump($contest); $em->flush();
}
return $this->redirect($this->generateUrl('acme_random_homepage'));
}
return $this->render('AcmeRandomBundle:Default:addcontest.html.twig', array(
'form' => $form->createView(),
));
class AddType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array(
'required' => false,
))
->add('prizes', 'collection', array(
'type' => new PrizeType(),
'allow_add' => true,
))
->add('save', 'submit');
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Acme\RandomBundle\Entity\Contest',
);
}
public function getName()
{
return 'addContest';
}
}
class PrizeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('place', 'text', array(
'label' => 'Место',
'required' => false,
))
->add('name', 'text', array(
'label' => 'Приз',
'required' => false,
));
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Acme\RandomBundle\Entity\Prize',
);
}
public function getName()
{
return 'prize';
}
}