phpsymfonyrequestsymfony2

Add Parameter to symfony2 Request deep array


I want to add a parameter to the ParameterBag in a symfony2 Request.

So the array looks like this:

array (size=2)
  'editor' => 
    array (size=6)
      '_token' => string '5797a4faf1fced89404b80fb04b3cadffc99695e' (length=40)
      'login' => string 'editor' (length=6)
      'firstName' => string 'Joh' (length=3)
      'lastName' => string 'Ha' (length=2)
      'address' => 
        array (size=6)
          'time_zone_code' => string 'Africa/Abidjan' (length=14)

And I want to add a field to the editor array. I tried this

$request->request->add(array('editor[password]' => $password));

But of course this just adds a field named editor[password] after the editor array.

Do I have to replace the whole ParameterBag or is there a method to add the value?


Solution

  • You can get the editor array and then add a value to it and set it again if editor is not the only array in the ParameterBag:

    $data = $this->getRequest()->request->get('editor');
    $data['password'] = 'string';
    $this->getRequest()->request->set('editor', $data);
    

    Edit

    Looks like there is a question in a google group that is similar with a similar answer: https://groups.google.com/forum/?fromgroups=#!topic/symfony-devs/2-SWFtFKwxQ