我创建了一个(我认为)有效的自定义会话模型,并且我的动作控制器中有两个测试动作。
第一个动作是:
public function testAction() {
$session = Mage::getSingleton('mymodule/session');
$session->func1('x');
$var1 = Mage::getSingleton('mymodule/session');
//Tracing through this function reveals that everything behaves as expected,
//$session is created, modified and then when $var1 is created, the same
//reference is returned and the two references refer to the same object
//($session === $var1) = true
}
第二个动作是:
public function testresultAction() {
$session = Mage::getSingleton('mymodule/session');
var_dump($session);
//this method does not appear to work, and upon tracing through the
//getSingleton it is in fact creating a new session object, NOT returning
//the one that already existed.
}
我的会话类如下所示:
class Mystuff_Mymodule_Model_Session extends Mage_Core_Model_Session_Abstract {
public function __construct() {
$namespace = 'Mystuff_Mymodule';
$this->init ( $namespace );
Mage::dispatchEvent ( 'mymodule_session_init', array (
'mymodule_session' => $this
) );
$this->setData('history', [] );
$this->setIndex ( - 1 );
}
public function func1($historyElement){
$history = $this->getData( 'history' );
array_unshift ( $history, $historyElement);
while ( count ( $history ) > 10 ) {
array_pop ( $history );
}
$this->setData ('history', $history);
$this->setIndex(-1);
}
}
我还在其他点将 testresultAction 修改为 justvar_dump($_SESSION)
并且当我这样做时似乎有数据
所以,为什么,当我调用 mytestAction()
并且它创建一个单例并编辑数据时,在并发调用时testresultAction()
不存在任何修改后的数据,为什么它没有获得先前实例化的单例?