我已經創建了一個定製的會話模型(我認爲)能夠工作,並且在我的動作控制器中有兩個測試動作。Magento會話getSingleton失敗
的第一個動作是:
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其他指向只是var_dump($_SESSION)
,似乎有它的數據,當我做
SO,爲什麼當我打電話給我的testAction()
,它創建一個單例並編輯數據時,併發調用testresultAction()
是否沒有修改的數據存在,爲什麼它沒有得到先前實例化的單例?
向我們展示您的會話類 –
@碧西爲什麼要重要?那裏被調用的唯一方法已經顯示出來了? – Nanos
你的會話類正在擴展'Mage_Core_Model_Session_Abstract'類我希望,同意Bixi,我們需要更多的代碼來真正瞭解發生了什麼。 – input