如何在使用Doctrine2时访问Symfony2中的ChoiceType(Radiobutton)提交值 我正在尝试更新事件实体的eT1和eT2属性的值。来自两个不同ChoiceType(Radiobutton)的输入值在用户提交后存储在eT1和eT2变量中。当我尝试使用$ events-> setET1($ eT1)方法时,它会出错 - 注意:未定义的变量:eT1 500内部服务器错误 - ContextErrorException 这是我的控制器代码
&a
以下是用户实体
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\events;
//use AppBundle\Entity\eventtype;
use AppBundle\Entity\users;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
class DefaultController extends Controller {
/**
* @Route("/home", name="homepage")
*/
public function indexAction(Request $request) {
$events = new events();
$form = $this->createFormBuilder($events)
->add('eT1', ChoiceType::class, array(
'choices' => array(
'Poker' => 1,
'Chess' => 2,
'Cricket' => 3,
'Marbles' => 4,
'Football' => 5,
),
'choices_as_values' => true,
'expanded' => true,
'multiple' => false,
'label' => 'Choose After Breakfast Event',
))
->add('eT2', ChoiceType::class, array(
'choices' => array(
'Poker' => 1,
'Chess' => 2,
'Cricket' => 3,
'Marbles' => 4,
'Football' => 5,
),
'choices_as_values' => true,
'expanded' => true,
'multiple' => false,
'label' => 'Choose After Snacks Event',
))
->add('save', SubmitType::class, array('label' => 'Submit'))
->getForm();
if ($request->isMethod('POST')) {
$form->submit($request);
$user = $this->container->get('security.context')->getToken()->getUser();
$events->setUser($user);
$x = $events->getUser()->getID();
if ($form->isValid()) {
//$em = $this->getDoctrine()->getManager();
$events = $this->getDoctrine()->getRepository('AppBundle:events')->findOneBy(array('user' => $x));
// var_dump($events);
// exit;
if (!$events) {
echo 'insert';
// tells Doctrine you want to (eventually) save the Product (no queries yet)
$em->persist($events);
// actually executes the queries (i.e. the INSERT query)
$em->flush();
return $this->redirectToRoute('homepage');
} else {
echo 'update';
// var_dump($form);
// exit;
$events->setET1($eT1);
$events->setET2($eT2);
// $em->persist($events);
$em->flush();
return $this->redirectToRoute('homepage');
}
}
}
return $this->render('default/index.html.twig', array(
'form' => $form->createView(),
));
}
}
下面是定义了setET1($ eT1)的事件实体
<?php
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* users
*
* @ORM\Table(name="users")
* @ORM\Entity(repositoryClass="AppBundle\Repository\usersRepository")
*/
class users extends BaseUser
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* Get id
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @ORM\OneToOne(targetEntity="events", mappedBy="user")
*/
protected $event;
// public function __construct()
// {
// parent::__construct();
// }
}
答案 0 :(得分:1)
<强>更新强>
您的表单将返回已填充的实体,因此您可以执行此操作:
$eventFromForm = $form->getData(); // This is an instance of events()
$events->setET1($eventFromForm->getET1());
$events->setET2($eventFromForm->getET2());
$em->persist($events);
$em->flush();
$ eT1未定义(显然)我猜你正在尝试从表单中获取eT1值,在这种情况下你需要这样做:
$formData = $form->getData();
$events->setET1($formData['eT1']);