我已經重寫了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>
所有的
當您發佈表單數據,那麼'$ surveyID = $請求 - > query-> get('survey');'不正確,因爲它會在$ _GET中查找,而不在$ _POST中 –
@ V-Light是正確的,對於發佈數據,使用$ request-> request->獲得( '調查'); –
@ V-Light好吧,我現在就試試 – Arcv