2

我想在 symfony2 请求中向 ParameterBag 添加一个参数。

所以数组看起来像这样:

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)

我想在editor array. 我试过这个

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

但是当然这只是添加了一个editor[password]editor array.

我是否必须替换整个 ParameterBag 或者是否有添加值的方法?

4

1 回答 1

7

如果 editor 不是 ParameterBag 中的唯一数组,您可以获取 editor 数组,然后为其添加一个值并再次设置它:

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

编辑

看起来谷歌组中有一个与类似答案相似的问题:https ://groups.google.com/forum/?fromgroups=#!topic/symfony-devs/2-SWFtFKwxQ

于 2013-01-08T12:13:07.057 回答