将url参数设置为entity - Symfony

时间:2017-06-01 12:17:34

标签: php symfony fosuserbundle

我已经覆盖了Symfony中的FOSUserbundle注册控制器。我将一个参数传递给url进行注册,我想在我的用户实体中设置该参数。但是我可以传递参数并在控制器中获取参数。但是无法将该值设置为实体。

控制器:

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="a.rafa@gci-brandlab.de"><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>
    

1 个答案:

答案 0 :(得分:1)

首先,检查你的$ request对象

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

并查看survey是否已发布/获取。

如果没有survey,那么您应该更改注册表单并向其添加survery字段检查服务是否在表单命名空间中

要执行此操作,请尝试检查所有提交的数据

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

您还应该告诉我们您的表单是什么样子的。只需复制outterHtml(在Chrome中:按F12,然后选择Elements,找到元素,右键单击 - &gt;复制 - &gt;复制outerHtml)

<强>更新

好的,现在我看到:) 您尝试使用

获取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);
}
你可以尝试直接从$ _POST获取survey_id,但它也会很脏

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