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

时间:2012-08-10 07:07:18

标签: php symfony1 symfony-1.4

我开始使用post来了解我如何从用户会话中获取/设置值(Symfony 1.4)。 Get完美无缺,但Set永远不会有效。我可以在Debug Modus中看到全局会话($ _ SESSION)已更新,但用户会话没有。我尝试了this waythis way以及another one,至少this ...没有成功。我从未从Symfony 1.4中读到过这样的Bug。我使用浏览器(linux)Firefox和Chrome(我也将它测试到anonymus Tab中)。

调试Modus用户看起来像:

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

我可以更改 aValue 属性,删除并重新插入。但 ns_15 ns_17 可以删除或更改,但此更改无法生效(symfony调试模式)用户,但只能进入 $ _SESSION (symfony Debug Modus Globals )我可以看到更新的值。

是否有其他方法可以使用symfony 1.4设置/更改/删除或将值写入用户会话,或者它是Symfony Bug?

EDIT1

我直到看到这个BugBlog时才弄明白!好吧,我正在使用AJAX来调用Controller。这也许就是为什么我不能写Session的原因。另一种方法我不能真正子类化sfWebResponse导致我的行为已经是sfActions的子类。 :(

EDIT2

因为 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和USER SESSION。 (Same Trouble too)。

1 个答案:

答案 0 :(得分:0)

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

// 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课程中获取战利品,了解这些功能的工作原理。