2017-06-01 46 views
0

我已经重写了Symfony中的FOSUserbundle注册控制器。我传递一个参数到url进行注册,我想在我的用户实体中设置该参数。不过,我可以通过参数,并获取控制器中的参数。但是无法为实体设定价值。将url参数设置为实体 - Symfony

控制器:

public function registerAction(Request $request) 
{ 
    $user = $userManager->createUser(); 
    $user->setEnabled(true);  

    $event = new GetResponseUserEvent($user, $request); 
    $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event); 

    //Getting parameter 
    $surveyID = $request->query->get('survey'); 
    //Setting parameter- Not working 
    $user->setSurveyID($surveyID); 
    //echo $surveyID; 

    if (null !== $event->getResponse()) { 
     return $event->getResponse(); 
    } 


    $form = $formFactory->createForm(); 
    $form->setData($user); 


    $form->handleRequest($request); 

    if ($form->isSubmitted()) { 
     if ($form->isValid()) { 
      $event = new FormEvent($form, $request); 
      $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event); 

      $userManager->updateUser($user); 

      if (null === $response = $event->getResponse()) { 
       $url = $this->generateUrl('fos_user_registration_confirmed'); 
       $response = new RedirectResponse($url); 
      } 

      $dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response)); 

      return $response; 
     } 

     $event = new FormEvent($form, $request); 
     $dispatcher->dispatch(FOSUserEvents::REGISTRATION_FAILURE, $event); 

     if (null !== $response = $event->getResponse()) { 
      return $response; 
     } 
    } 

    return $this->render('@FOSUser/Registration/register.html.twig', array(
     'form' => $form->createView(), 
    )); 
} 

$user->setSurveyID($surveyID)不工作,但它与$user->setSurveyID('324324')

实体作品:user.php的

/** 
* @ORM\Column(type="integer", nullable=true) 
*/ 
private $surveyID; 

    /** 
* @return mixed 
*/ 
public function getSurveyID() 
{ 
    return $this->surveyID; 
} 

/** 
* @param mixed $surveyID 
*/ 
public function setSurveyID($surveyID = null) 
{ 
    $this->surveyID = $surveyID; 
} 

我应该怎么办得到呢?谢谢!

编辑:

<form name="fos_user_registration_form" method="post" action="/register" class="fos_user_registration_register"> 

<div id="fos_user_registration_form"><div class="form-group"><label class="control-label required" for="fos_user_registration_form_firstname">First Name *</label><input type="text" id="fos_user_registration_form_firstname" name="fos_user_registration_form[firstname]" required="required" class="form-control" value="aasda"></div><div class="form-group"><label class="control-label required" for="fos_user_registration_form_lastname">Last Name *</label><input type="text" id="fos_user_registration_form_lastname" name="fos_user_registration_form[lastname]" required="required" class="form-control" value="asd"></div><div class="form-group"><label class="control-label required" for="fos_user_registration_form_username">Username *</label><input type="text" id="fos_user_registration_form_username" name="fos_user_registration_form[username]" required="required" class="form-control" value="asd"></div><div class="form-group"><label class="control-label required">Sex *</label><div id="fos_user_registration_form_sex"><div class="radio">                    <label class="required"><input type="radio" id="fos_user_registration_form_sex_0" name="fos_user_registration_form[sex]" required="required" value="M"> Male</label> 
</div><div class="radio">                    <label class="required"><input type="radio" id="fos_user_registration_form_sex_1" name="fos_user_registration_form[sex]" required="required" value="F" checked="checked"> Female</label> 
</div></div></div><div class="form-group has-error"><label class="control-label required" for="fos_user_registration_form_email">Email *</label><input type="email" id="fos_user_registration_form_email" name="fos_user_registration_form[email]" required="required" class="form-control" value="[email protected]"><span class="help-block"> <ul class="list-unstyled"><li><span class="glyphicon glyphicon-exclamation-sign"></span> fos_user.email.already_used</li></ul> 
</span></div><input type="hidden" id="fos_user_registration_form_plainPassword" name="fos_user_registration_form[plainPassword]" value="Kjk5XsachYkA"><input type="hidden" id="fos_user_registration_form_survey_id" name="fos_user_registration_form[survey_id]" value="12323"><div class="form-group"><label class="control-label" for="fos_user_registration_form_city">City</label><input type="text" id="fos_user_registration_form_city" name="fos_user_registration_form[city]" class="form-control"></div><div class="form-group"><label class="control-label" for="fos_user_registration_form_zip_code">Zip Code</label><input type="text" id="fos_user_registration_form_zip_code" name="fos_user_registration_form[zip_code]" class="form-control"></div><div class="form-group"><label class="control-label" for="fos_user_registration_form_age">Age</label><input type="number" id="fos_user_registration_form_age" name="fos_user_registration_form[age]" class="form-control"></div><div class="form-group"><div class="checkbox">               <label class="required"><input type="checkbox" id="fos_user_registration_form_privacy" name="fos_user_registration_form[privacy]" required="required" value="1" checked="checked"> Accept data privacy</label> 
</div></div><input type="hidden" id="fos_user_registration_form__token" name="fos_user_registration_form[_token]" value="IXsOFEj4S-9oYxUcGK53BrG0CSn-EyRbYkXuC74H1Ic"></div> 
所有的

+0

当您发布表单数据,那么'$ surveyID = $请求 - > query-> get('survey');'不正确,因为它会在$ _GET中查找,而不在$ _POST中 –

+0

@ V-Light是正确的,对于发布数据,使用$ request-> request->获得( '调查'); –

+0

@ V-Light好吧,我现在就试试 – Arcv

回答

1

首先,检查你的$ request对象

dump($request->request->get($form->getName()), $request->getMethod()) 
return; 

,看看是否survey被贴/ GETed可言。

如果没有survey那么你应该改变你的登记表和survery字段添加到它OR检查,如果述略是在形式命名空间

要做到这一点,尽量检查所有提交的数据

//dumps $_POST, $_GET and the Submit-Method 
dump($request->request->all(), $request->query->all(), $request->getMethod()) 
return; 

你也应该告诉我们你的表格是怎样的。只需复制outterHtml(在Chrome:按F12键,然后选择Elements,寻找元素,右击 - >复制 - >复制outerHtml)

UPDATE:

好了,现在我明白了:) 你试试得到servey

$surveyId = $request->request->get('survey'); 

但是你的隐藏字段具有不同的名称name="fos_user_registration_form[survey_id]

所以你应该尝试ŧ他之后的handleRequest()

if($form->has('survey_id')) 
{ 
    $surveyID = $form->get('survey_id')->getNormData(); 
$user->setSurveyID($surveyID); 
} 
五言的

你也可以尝试让survey_id直接从$ _ POST,但它也将是肮脏

$postedData = $request->request->get('fos_user_registration_form'); 
$surveyId = @$postedData['survey_id']; 
+0

调查已GET并且表单已发布。而且我已经在我的表格中进行了隐藏的实地调查。 – Arcv

+0

也加了outerhtml,请看编辑! – Arcv

+0

@Arcv我更新了我的回答 –