自定义会话对象中的 Magento 数据数组存储


Magento data array storage in custom session object

>我正在使用自己的会话类,在该类中,我使用了一些受保护的数据成员和一些公共数据方法 当我在我的会话类上存储一些变量时,就像

Mage::getSingleton('decision/session')->storeProductInfo('2');

这是函数实现,$this->_productId是我的会话类的私有数据成员。

Public function storeProductInfo($product_id){
    $this->_productId = $product_id;
    return $this;
}

我通过调用以下语句来获取存储的变量,它返回我"null"。

$product_stored_id = Mage::getSingleton('decision/session')->getStoredProductInfo();
public function getStoredProductInfo(){
   return $this->_productId;
}

甚至

Mage:getSingleton('decision/session')->setData('product_id', '2');

没用。你能告诉我我哪里出错了吗?我必须在会话中存储一些数组,这就是为什么我创建了自己的会话类来单独处理我的逻辑。

使用 Magento 魔术方法 获取和设置

为此,当您的观察者将调用时,您可以创建会话并设置其值。

您可以使用 SET 设置会话,使用 GET 获取值,使用 UNS 取消设置会话。

Mage::getSingleton('core/session')->setMySessionVariable('MyValue'); 
$myValue = Mage::getSingleton('core/session')->getMySessionVariable();
echo $myValue;

取消设置会话

Mage::getSingleton('core/session')->unsMySessionVariable();

$inputMessage = 'Hello World';
Mage::getSingleton('core/session')->setWelcomeMessage($inputMessage);

现在,您希望在代码/站点中的其他位置回显"欢迎消息"。

$outputMessage = Mage::getSingleton('core/session')->getWelcomeMessage();
echo $this->__($outputMessage);