如何在嵌套的会话属性上使用Symfony的get/setAttribute? [英] How to use Symfony get/setAttribute on nested Session Attributes?
问题描述
我有一个sfuser/属性,如下所示:
I have a sfuser/attributes looking like this:
session:
symfony/user/sfUser/attributes: {
symfony/user/sfUser/attributes:{
15:{
Telefon:'+304994994994',
option:'15',
rules:'12',
arrayBunch:[{
this: 'da',
that: "where"
}]
},
17:{...},
mysetting: "val"
},
sfGuardSecurityUser:{},
admin_module:{},
sfGuardUserSwitcher:{}
}}
当我使用 setAttribute("mysetting","val")时,它们将mysetting像在示例中显示的那样存储.因此,不必使用 setAttribute("mysetting","val",15),因为我得到了相同的答案.
When i use setAttribute("mysetting", "val") they stores mysetting like show on my example. It is therefore unnecessary to use setAttribute("mysetting", "val", 15) because i got the same answer.
如何使用 setAttribute 在其中输入值或 getAttribute 来访问节点 15 或 arrayBunch 弄他们?
How can i access the nodes 15 or arrayBunch an use setAttribute to input a value there or getAttribute to get'em?
推荐答案
我认为您做错了(关于参数).
I think you are doing it in a wrong way (about parameters).
您要执行的操作是:
- 添加一个名为
val
的变量
- 值为
15
- 在命名空间
mysetting
中.
- add a variable called
val
- with the value
15
- in the namespace
mysetting
.
但是,您编写的参数顺序错误.
But, you have written parameters in a wrong order.
如果您检查代码,参数为:
public function setAttribute($name, $value, $ns = null)
所以您应该尝试:
->setAttribute('val', 15, 'mysetting');
(抱歉没有注意到arrayBunch
问题)
(sorry didn't notice the arrayBunch
problem)
如果要编辑arrayBunch值:
If you want to edit the arrayBunch value:
// retrieve the current value
$arrayBunch = $this->getUser()->getAttribute("arrayBunch", array(), 15);
// made some modification
$arrayBunch['toto'] = 'titi';
$arrayBunch['this'] = 'do';
// put back the modification
$this->getUser()->setAttribute("arrayBunch", $arrayBunch, 15);
编辑:
仔细查看json,您应该检索整个属性15
,因为namepsace是
With a closer look to your json, you should retrieve the whole attribute 15
, since the namepsace is the default one: symfony/user/sfUser/attributes
. So:
$attribute15 = $this->getUser()->getAttribute(15);
$arrayBunch = $attribute15['arrayBunch'];
如果要应用一些更改:
// update arrayBunch
$arrayBunch['this'] = 'dada';
// do not forget to re-add modified arraybBunch to the whole variable
$attribute15['arrayBunch'] = $arrayBunch
// and if you want to add a value
$attribute15['mysetting'] = 'val';
// then, save every thing to the session again
$this->getUser()->setAttribute(15, $attribute15);
这篇关于如何在嵌套的会话属性上使用Symfony的get/setAttribute?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!