0

(私が思うに) 動作するカスタム セッション モデルを作成しました。アクション コントローラーには 2 つのテスト アクションがあります。

最初のアクションは次のとおりです。

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
}

2 番目のアクションは次のとおりです。

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()変更されたデータが存在せず、以前にインスタンス化されたシングルトンが取得されないのはなぜですか?

4

3 に答える 3

0

後でシングルトンで問題が発生する場合

Magento シングルトンは、ページの存続期間中のみ持続します

それらは、いくつかのページを通じて持続しないレジストリパターンに依存します。

私のデータが失われた理由については、新しいシングルトンを作成していたため(別のアクションであり、別のページだったため)、配列を空に再初期化していたためです。

シングルトン/レジストリの有効期間を確認するためのドキュメントを見つけることができなかったので、完全を期すために、誰かがそれを見つけた場合は、ここでコメント/編集してください

于 2013-07-10T16:43:17.847 に答える