我有一个看起来像这样的sfuser / attributes:
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”)时,他们会在我的示例中存储my showting。因此,没有必要使用 setAttribute(“mysetting”,“val”,15),因为我得到了相同的答案。
如何访问节点 15 或 arrayBunch 使用 setAttribute 在其中输入值或 getAttribute get'em?
答案 0 :(得分:1)
我认为你是以错误的方式(关于参数)这样做。
您想要做的是:
val
15
mysetting
中的但是,你写的参数编号错误。
如果您选中the code,参数为:
public function setAttribute($name, $value, $ns = null)
所以你应该尝试:
->setAttribute('val', 15, 'mysetting');
修改: (抱歉没有注意到arrayBunch
问题)
如果要编辑arrayBunch值:
// 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为the default one:symfony/user/sfUser/attributes
。所以:
$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);