2
votes

I started this post to know how i can get/set Values from the User Session(Symfony 1.4). The Get works perfectly but the Set never works. I can see in the Debug Modus that the Global Session($_SESSION) is updated but the User Session dont. I tried this way and this way and another one and at least this... Without Success. I never read about such a Bug from Symfony 1.4. I use as Browser(linux) Firefox and Chrome (I tested it into anonymus Tab too).

Debug Modus User looks like :

attributeHolder:
myNS: { 
     aValue: '15', 
     ns_15: { 
         Tel: '78568951', 
         myID: '15', 
         anotherID: '120', 
         anArray: [{ 
             arrayID: '78', 
             Company: 'myComp'
         }] }, 
     ns_17: { ... } 
}

I can change the aValue attribute, delete and reinsert it. But the ns_15 or ns_17 can be delete or change but this Change cant be affect (symfony Debug Mode) User, but just into $_SESSION (symfony Debug Modus Globals ) i can see the updated values.

Is there another way to set/change/delete or write a value into an User Session with symfony 1.4 or it's a Symfony Bug?

EDIT1

I just dont figure it out til i see this Bug and this Blog !! well i'm using AJAX to call the Controller. So that's maybe the reason why i cant write on Session. Another Way i can't really subclass sfWebResponse cause my actions already subclass sfActions. :(

EDIT2

Because Edit1 I tried to make this:

  $sessionData = $this->getUser()->getAttribute("ns_15", array(), "myNS");//all good
  $sessionData["Tel"] = "0111111110";
  var_dump($sessionData["Tel"]);//show me 0111111110
  $this->getUser()->setAttribute("ns_15", $sessionData, "myNS");
  $this->getUser()->shutdown();
  sfContext::getInstance()->getStorage()->shutdown();

The Behavior with reloading twice is solved,but still cant write into $_SESSION and USER SESSION. (Same Trouble too).

1

1 Answers

0
votes

I try to summary how you can manipulate the user attributes (note that this not the same as modifying the $_SESSION global see my comment here to know why).

// set a singe value in a namespace
$this->getUser()->setAttribute('aValue', 15, 'myNS');
// set an array in a namespace
$this->getUser()->setAttribute('ns_15', array('Tel' => '78568951', 'myID' => '15', 'anotherID' => '120', 'anArray' => array(array('arrayID' => '78', 'Company' => 'myComp'))), 'myNS');

// get the array from a namespace
$attributes = $this->getUser()->getAttribute('ns_15', array(), 'myNS');
// modify some value in the array
$attributes['Tel'] = '12345';
// remove some value from the array
unset($attributes['myID']);
// ... and save your changes back
$this->getUser()->setAttribute('ns_15', $attributes, 'myNS');
// remove the whole array from the namespase
$this->getUser()->getAttributeHolder()->remove('ns_15', null, 'myNS');
// remove the whole namespace
$this->getUser()->getAttributeHolder()->removeNamespace('myNS');

You can access a whole namespace as well:

// get all attributes from a namespace
$allAttributes = $this->getUser()->getAttributeHolder()->getAll('myNS');
// manipulate the whole namespace here...
// ...and save your changes back
$this->getUser()->getAttributeHolder()->add($allAttributes, 'myNS');

If you work on a same namespace and want to omit the namespace parameter:

$originalDefaultNs = $this->getUser()->getAttributeHolder()->getDefaultNamespace();
$this->getUser()->getAttributeHolder()->setDefaultNamespace('myNS', false);
// ...
$attributes = $this->getUser()->getAttribute('ns_15', array());
// ...
// don't forget to set back the original ns
$this->getUser()->getAttributeHolder()->setDefaultNamespace($originalDefaultNS, false);

I know it's really verbose, but I think there is no other way to do it in symfony. You can take a loot at the sfNamespacedParameterHolder class to see how these functions work.