magento 使用 <session_save> 数据库值写入或读取会话

magento write to or read from session using <session_save> db value

我在一家选择使用数据库在 magento 中进行会话处理的公司工作。具体来说,在 /app/etc/local.xml 中有这个条目:

    <session_save><![CDATA[db]]></session_save>

我了解到数据正在 table core_session 中保存。但是,我不熟悉如何读取和写入会话对象。

session_start()很简单,我就写

$_SESSION['status']='OK'; //write
$status= $_SESSION['status']; //read

当 magento 使用 db 作为会话存储方法时,等效方法是什么?我假设这是一个 class 方法。谢谢。

Magento 中的每个模块都可以有自己的会话对象,用于保存命名空间值 to/from 会话。例如,要在 "core" 会话命名空间中设置变量 foo_bar,您需要做的就是调用

Mage::getSingleton('core/session')->setFooBar('Some Value');

要在 "customer" 会话命名空间中做同样的事情,

Mage::getSingleton('customer/session')->setFooBar('Some Value');

然后您将使用

获取这些值
Mage::getSingleton('core/session')->getFooBar();
Mage::getSingleton('customer/session')->getFooBar();

基本思想是 Magento 为您提供这些会话对象,因此您无需担心 starting/stopping 会话,或管理 $_SESSION 中的冲突。在幕后,Magento 仍在使用 $_SESSIONsession_start——但它会为您处理这些细节,因此您可以像使用 Magento 中的任何模型一样使用会话 model/singleton。

您可能还会发现 this answer 有用。