使用 AJAX 调用控制器时,在 Symfony 1.4 中将值写入/设置/更改到用户会话中的替代方法


Alternative to write/set/change Values into User Session in Symfony 1.4 when using AJAX to Call Controllers?

我开始这篇文章是为了知道如何从用户会话(Symfony 1.4)中获取/设置值。Get 完美运行,但 Set 永远不会工作。我可以在调试模式中看到全局会话($_SESSION)已更新,但用户会话未更新。我尝试了这种方式,这种方式,另一种方式,至少是这种方式......没有成功。我从来没有从Symfony 1.4中读到过这样的错误。我使用浏览器(linux)Firefox和Chrome(我也在anonymus Tab中对其进行了测试)。

调试模式用户看起来像:

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

我可以更改 aValue 属性,删除并重新插入它。但是ns_15ns_17可以删除或更改,但此更改不能影响(symfony Debug Mode)用户,但只需输入$ _SESSION(symfony Debug Modus Globals)我可以看到更新的值。

有没有另一种方法可以设置/更改/删除或将值写入symfony 1.4的用户会话,或者它是一个Symfony Bug?

编辑1

我只是在看到这个错误和这个博客之前不明白! 好吧,我正在使用AJAX调用控制器。所以这可能就是我不能在会话上写的原因。另一种方式,我不能真正子类sfWebResponse,因为我的操作已经子类sfActions。:(

编辑2

因为 Edit1 我试图这样做:

  $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();

解决了两次重新加载的行为,但仍然无法写入 $_SESSION 和用户会话。(同样的麻烦也是)。

我尝试总结如何操作用户属性(请注意,这与修改全局$_SESSION不同,请参阅我的评论 此处 了解原因)。

// 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');

您也可以访问整个命名空间:

// 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');

如果在同一命名空间上工作,并且想要省略命名空间参数:

$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);

我知道这真的很冗长,但我认为在symfony中没有其他方法可以做到这一点。您可以在sfNamespacedParameterHolder类中抢劫,看看这些函数是如何工作的。