1

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?

2
  • Are you sure about setAttributes? Because sfUser doesn't have setAttributes but has a setAttribute. Commented Aug 6, 2012 at 10:03
  • Sorry you are right! it's setAttribute i edit my post!! Commented Aug 6, 2012 at 10:24

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

15 Comments

in my example "15" is a namespace right? can i set a new value to this namespace with your example?
Well, you cannot assign a value to a namespace. You can assign variable which containt a value: ->setAttribute('my_variable', 'my_value', 'mysetting');
There is no way to insert a new 'my_variable': 'my_value' after 'array_bunch' into 15 ??
'array_bunch' is just a variable name!
Oh i got it!! :) To retrieve: $arrayBunch = sfContext::getInstance()->getUser()->getAttribute(15);; and then $arrayBunch['arrayBunch']; to get the array. I have not Test to made modification... can u update ur Post with that? so i can mark yours as an Answer? ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.