Zend 框架中的自定义类

发布于 2024-12-07 09:12:24 字数 238 浏览 2 评论 0原文

我正在使用 Zend Framework 和 Propel。使用 Propel 对象作为存储在数据库中的事物的模型非常容易,但如何创建自定义模型,例如使用 propel 对象的购物车?

我正在考虑创建一个名为 CartItem 和 Cart 的类。如果 CartItem 包含一个 propel Product 类,并且 Cart 包含一个 CartItem 数组,那么在会话中存储这似乎非常昂贵。通常我只使用数组,但我试图通过 OOP 变得更好

I'm using Zend Framework with Propel. It's easy enough to use Propel objects as my models for things that are stored in the database, but how do I go about creating a custom model such as a shopping cart that uses propel objects?

I'm thinking of creating a class called CartItem and Cart. If CartItem contains a propel Product class, and Cart contains an array of CartItems, this seems very expensive to store in a session. Normally I'd just use an array, but I'm trying to get better with OOP

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹泡泡o 2024-12-14 09:12:24

您可以使用 PHP 的可序列化接口:http://php.net/manual/de/class。 您可以使用它将

购物车的实例转换为更有效的值(您定义,到底存储什么),在您的情况下是一个二维数组。

尝试这样的操作:

<?php

class Cart implements Serializable {

    // your normal code for the Cart class

    public function serialize() {
        $cartData = array();
        foreach($this->cartItems as $item) {
            $cartData[] = array(
                'count' => $item->getCount(),
                'productId' => $item->getId()
            );
        }
        return serialize($cartData);
    }

    public function unserialize($cartData) {
        $this->cartItem = array();
        foreach($cartData as $item) {
            // replace this with the appropriate propel code
            $product = loadProductWithPropelById($item['id']);
            $this->cartItems[] = new CartItem($item['count'], $product);
        }
    }

}

?>

然后使用序列化将购物车存储在会话中并检索它:

<?php

// Store cart to session
$_SESSION['cart'] = serialize($cart);

// get cart from session
$cart = unserialize($_SESSION['cart']);

?>

祝您的项目好运!

You could use PHP's Interface Serializable: http://php.net/manual/de/class.serializable.php

You can use it to convert an instance of your cart into a more efficient value (you define, what exactly is stored), which is in your case a two-dimensional array.

Try something like this:

<?php

class Cart implements Serializable {

    // your normal code for the Cart class

    public function serialize() {
        $cartData = array();
        foreach($this->cartItems as $item) {
            $cartData[] = array(
                'count' => $item->getCount(),
                'productId' => $item->getId()
            );
        }
        return serialize($cartData);
    }

    public function unserialize($cartData) {
        $this->cartItem = array();
        foreach($cartData as $item) {
            // replace this with the appropriate propel code
            $product = loadProductWithPropelById($item['id']);
            $this->cartItems[] = new CartItem($item['count'], $product);
        }
    }

}

?>

Then use serialize to store the cart in the session and retrieve it:

<?php

// Store cart to session
$_SESSION['cart'] = serialize($cart);

// get cart from session
$cart = unserialize($_SESSION['cart']);

?>

Good Luck with your Project!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文