私はショッピングカート(カートモデル)に取り組んでいます。その保護されたプロパティの1つは、Productオブジェクトの配列を保持する「_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();
}
コントローラで、ProductMapperを使用してDBからProduct objを取得し、それを「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()
最初に注入された場合(最初のパラメータ)、でProductクラスを宣言するにはどうすればよいですか?新しい宣言(のようなnew Model_Product()
)は、のパラメータ(元のオブジェクト)を上書きしませんaddItem()
か?カートモデルで再度宣言する必要がありますか?
Cannot redeclare class Model_Product
その上、私が...カートでそれを再宣言すれば、私はきっと得るでしょう。