Add Parameter to symfony2 Request deep array

不羁的心 提交于 2020-01-04 09:03:19

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/14214428/add-parameter-to-symfony2-request-deep-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!