问题
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:{}
}}
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.
How can i access the nodes 15 or arrayBunch an use setAttribute to input a value there or getAttribute to get'em?
回答1:
I think you are doing it in a wrong way (about parameters).
What you want to do is:
- add a variable called
val
- with the value
15
- in the namespace
mysetting
.
But, you have written parameters in a wrong order.
If you check the code, parameters are :
public function setAttribute($name, $value, $ns = null)
So you should try:
->setAttribute('val', 15, 'mysetting');
Edit: (sorry didn't notice the arrayBunch
problem)
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);
Edit:
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'];
And if you want to apply some changes:
// 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);
来源:https://stackoverflow.com/questions/11825664/how-to-use-symfony-get-setattribute-on-nested-session-attributes