セッションでオブジェクトをシリアル化するときの不完全なPHPクラス
-
29-10-2019 - |
質問
私はショッピングカート(カートモデル)に取り組んでいます。保護されたプロパティの1つは「_ITEMS」で、製品オブジェクトの配列を保持しています。彼ら(製品)はすべて、セッションの居住のためにDBに保存されます(ZF、ZEND_SESSION_SAVEHANDLER_DBTABLE()などを使用)。
public function addItem(Model_Product $product, $qty)
{
$qty = (int) $qty;
$pId = $product->getId();
if ($qty > 0) {
$this->_items[$pId] = array('product' => $product, 'qty' => $qty);
} else {
// if the quantity is zero (or less), remove item from stack
unset($this->_items[$pId]);
}
// add new info to session
$this->persist();
}
コントローラーでは、DBから製品OBJをProductMapperでつかみ、「additem()」に提供します。
$product1 = $prodMapper->getProductByName('cap');
$this->_cart->addItem($product1, 2);
getProductByName()
新しい人口型Model_Productオブジェクトを返します。
私は通常取得します
Please ensure that the class definition "Model_Product" of the object you are trying to operate on was loaded _before_ ...
エラーメッセージ、セッションダンプが明らかに示しています
['__PHP_Incomplete_Class_Name'] => 'Model_Product'
私は「クラスをシリアル化する前に宣言する」ことについて知っています。私の問題はこれです:どうすれば製品クラスを宣言できますか addItem()
, 、そもそもそれが注入されている(最初のパラメーション)?新しい宣言はありません(次のように new Model_Product()
)PARAM(元のオブジェクト)を上書きします addItem()
?カートモデルでもう一度宣言する必要がありますか?
その上、私は確かに取得します Cannot redeclare class Model_Product
もし私が...カートでそれを再排除します。
解決
ZFのブートストラップでは、セッションが開始されました 前 オートローディング。
/**
* Make XXX_* classes available
*/
protected function _initAutoloaders()
{
$loader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'XXX',
'basePath' => APPLICATION_PATH
));
}
public function _initSession()
{
$config = $this->_config->custom->session;
/**
* For other settings, see the link below:
* http://framework.zend.com/manual/en/zend.session.global_session_management.html
*/
$sessionOptions = array(
'name' => $config->name,
'gc_maxlifetime' => $config->ttl,
'use_only_cookies' => $config->onlyCookies,
// 'strict' => true,
// 'path' => '/',
);
// store session info in DB
$sessDbConfig = array(
'name' => 'xxx_session',
'primary' => 'id',
'modifiedColumn' => 'modified',
'dataColumn' => 'data',
'lifetimeColumn' => 'lifetime'
);
Zend_Session::setOptions($sessionOptions);
Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($sessDbConfig));
Zend_Session::start();
}
私が話していたエラーを取得していたとき、メソッド宣言はその逆でした: _initSession()
次に、最初でした _initAutoloaders()
- そして、これはZFがそれらを処理していた正確な順序でした。
もう少しテストしますが、これは機能しているようです(そして論理的)。すべての提案をありがとう。