注意:未定义的变量:eT1 500 内部服务器错误 - 上下文错误异常



如何在使用Doctrine2时访问Symfony2中的ChoiceType(单选按钮)提交值我正在尝试更新事件实体的 eT1 和 eT2 属性的值。用户提交后,来自两个不同 ChoiceType(单选按钮)的输入值存储在 eT1 和 eT2 变量中。当我尝试使用 $events->setET1($eT 1) 方法时,它给出了一个错误 - 注意:未定义的变量:eT1500 内部服务器错误 - 上下文错误异常这是我的控制器代码

<?php
namespace AppBundleController;
use AppBundleEntityevents;
//use AppBundleEntityeventtype;
use AppBundleEntityusers;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentFormExtensionCoreTypeSubmitType;
use SymfonyComponentFormExtensionCoreTypeChoiceType;
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(),
        ));
    }
}

下面是用户实体

<?php
namespace AppBundleEntity;
use FOSUserBundleModelUser as BaseUser;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use DoctrineCommonCollectionsArrayCollection;
/**
 * users
 *
 * @ORMTable(name="users")
 * @ORMEntity(repositoryClass="AppBundleRepositoryusersRepository")
 */
class users extends BaseUser
{
    /**
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * Get id
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * @ORMOneToOne(targetEntity="events", mappedBy="user")
     */
    protected $event;

//    public function __construct()
//    {
//        parent::__construct(); 
//         }
    }

下面是定义 setET1($eT 1) 的事件实体

<?php
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
/**
 * events
 *
 * @ORMTable(name="events")
 * @ORMEntity(repositoryClass="AppBundleRepositoryeventsRepository")
 */
class events {
    /**
     * @var int
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @var int
     *
     * @ORMColumn(name="ET1", type="integer")
     */
    protected $eT1;
    /**
     * @var int
     *
     * @ORMColumn(name="ET2", type="integer")
     */
    protected $eT2;
    /**
     * @ORMOneToOne(targetEntity="users", inversedBy="event")
     * @ORMJoinColumn(name="user_id", referencedColumnName="id")
     */
        protected $user;
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId() {
        return $this->id;
    }
    /**
     * Set user
     *
     * @param users $user
     * @return events
     */
    public function setUser($user) {
        $this->user = $user;
        return $this;
    }
    /**
     * Get user
     *
     * @return events
     */
    public function getUser() {
        return $this->user;
    }

    /**
     * Set eT1
     *
     * @param integer $eT1
     * @return events
     */
    public function setET1($eT1) {
        $this->eT1 = $eT1;
        return $this;
    }
    /**
     * Get eT1
     *
     * @return integer 
     */
    public function getET1() {
        return $this->eT1;
    }
    /**
     * Set eT2
     *
     * @param integer $eT2
     * @return events
     */
    public function setET2($eT2) {
        $this->eT2 = $eT2;
        return $this;
    }
    /**
     * Get eT2
     *
     * @return integer 
     */
    public function getET2() {
        return $this->eT2;
    }
//    public function __construct()
//    {
//        parent::__construct();
//    }
}

更新

您的表单将返回填充的实体,因此您可以执行以下操作:

$eventFromForm = $form->getData(); // This is an instance of events()
$events->setET1($eventFromForm->getET1());
$events->setET2($eventFromForm->getET2());
$em->persist($events);
$em->flush();

$eT 1 没有定义(显然)我猜您正在尝试从表单中获取 eT1 值,在这种情况下,您需要这样做:

$formData = $form->getData();
$events->setET1($formData['eT1']);

相关内容

  • 没有找到相关文章

最新更新