2

私はショッピングカート(カートモデル)に取り組んでいます。その保護されたプロパティの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その上、私が...カートでそれを再宣言すれば、私はきっと得るでしょう。

4

1 に答える 1

4

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がエラーを処理していた正確な順序でした。

もう少しテストしますが、これは機能しているようです(そして論理的です)。すべての提案をありがとう。

于 2011-05-05T16:46:52.173 に答える