0

我创建了一个(我认为)有效的自定义会话模型,并且我的动作控制器中有两个测试动作。

第一个动作是:

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()不存在任何修改后的数据,为什么它没有获得先前实例化的单例?

4

3 回答 3

1

You are right. As PHP is stateless, data not put to session only exists during the request-response lifecycle. With getSingleton you are simply fetching an instantiated object if it has been spawned yet.

于 2013-07-10T18:55:56.410 回答
1

Singletons exist for an execution scope (as you figured out). Session model instances however can store & retrieve data from session storage, meaning that while you don't have access to the same model instance, you do have persistent storage of instance properties.

Therefore, in one execution scope, you could:

$session = Mage::getSingleton('core/session');
$session->setFoo(array('bar'));
//$session->_data['foo'] = array(0=>'bar')
//aka $_SESSION['core'][0]['bar']

And then in the next execution scope:

$session = Mage::getSingleton('core/session');
var_dump($session->getFoo()); //array (size=1){ 0 => string 'bar' (length=3) }

I think you are not seeing $history because every time you initialize your session model you are overwriting it with

$this->setData('history', [] );
于 2013-07-10T18:57:32.747 回答
0

For those who end up having issues with singletons later

Magento singletons persist only during the life of the page

They depend on the registry pattern which does not persist through several pages.

as for why my data was lost, that would be because since it was creating a NEW singleton(because it was a different action, and thus a different page) it was re-initializing the array to empty

I was not able to find any documentation to verify the lifetime of singletons/registry so if anyone finds that please comment/edit it under here, for completeness

于 2013-07-10T16:43:17.847 回答