【问题标题】:working with temporary table in cakephp 3在 cakephp 3 中使用临时表
【发布时间】:2016-08-16 09:08:26
【问题描述】:

我正在使用 CakePHP 3.2

当用户使用user_id 登录时,我有一个表Carts 可以将产品存储在购物车中。

我也希望用户无需登录即可访问 addToCart。但在这种情况下,我想使用临时表来存储购物车数据,当用户登录时,将所有数据从临时表转移到carts 表并删除temporary table

如何在 CakePHP 3 中使用临时表?使用相同的临时数据是一种好习惯还是有更好的替代方法?

编辑 2

要存储在 cookie/临时表中的值

product_id
seller_id
seller_product_id
quantity

协会

product_idproducts 表的外键 seller_idsellers 表的外键 seller_product_idseller_products 表的外键

产品表进一步与product_attributes相关联

当前使用carts 表和以下列

+-----------+-------------+----------+---------------------+--------+
|  user_id   | product_id  | seller_id | seller_product_id  |quantity |
+-----------+-------------+----------+---------------------+--------+

这就是我正在做的检索相关数据的工作

if (!empty($this->Auth->user('id'))) {
              $user_id = $this->Auth->user('id');

              $g_cart = $this->Carts->find('all', [
                'conditions' => [
                  'user_id' => $user_id,
                ],
                'contain' => [
                  'Products', 'SellerProducts', 'Sellers', 'CartAttributes'
                ]
              ]);
              $g_count = $g_cart->count();

              if (!empty($g_cart)) {
//                foreach($g_cart as $g_c){debug($g_c);}
                  foreach($g_cart as $g_c) {
                    $total_cost += $g_c->quantity * $g_c->seller_product->selling_price;
                  }
              }
          }

希望我对你很清楚。

【问题讨论】:

    标签: cakephp temp-tables cakephp-3.2


    【解决方案1】:

    您可以将其存储在访客的 cookie 中,然后在他们登录时将 cookie 数据传输到表中。


    我还没有测试过这个,但它应该能让球滚动

    关于添加购物车商品的方法:

    $cart = $this->Cookie->read('Cart.items');
    if(!$this->Auth->user()) {
        if(!empty($cart)) {
            $newItem = [
                'Products' => $product->id,
                'Sellers' => $seller->id,
                'SellerProducts' => $sellerProduct->id,
                'quantity' => $quantity
            ];
            $newCart = array_merge($cookie, [$newItem]);
            $this->Cookie->write('Cart', ['items' => $newCart]);
        }else{
            $this->Cookie->write('Cart', [
                'items' => [
                    'products' => $product->id,
                    'sellers' => $seller->id,
                    'seller_products' => $sellerProduct->id,
                    'quantity' => $quantity
                ]
            ]);
    
        }
    
    }else{
        $item = $this->Cart->newEntity();
        if(!empty($cart)) {
            foreach($cart as $items) {
                $data = [
                    'user_id' => $this->Auth->user('id'),
                    'product_id' => $items['Products'],
                    'seller_id' => $items['Sellers'],
                    'seller_product_id' => $items['SellerProducts'],
                    'quantity' => $items['quantity']
                ];
                $item = $this->Cart->patchEntity($item, $data);
                $this->Cart->save($item);
            }
            $this->Cookie->delete('Cart.items');
        }
    }
    

    关于 CartController 视图方法

    $cart = $this->Cookie->read('Cart');    
    $entities = [];
    
    if(!$this->Auth->user() && !empty($cart) {
        $i = 0;
        foreach($cart['items'] as  $items) {
            foreach($items as $model => $id) {
                if($model == 'quantity') {
                    $quantity = $id;
                    continue;
                }
                $this->loadModel($model);
                $entity = $this->$model->get($id, ['contain' => []]);
                $entities[$i][] = $entity;
                $entities[$i]['quantity'] = $quantity;
            }
            $i++;
        }
        $this->set('items', $entities);
    }
    

    关于 CartController 的 view.ctp

    if(!empty($items)) {
        foreach($items as $item) {
            foreach($item as $entity) {
                echo '<div><?=$entity->name</div>';
                echo '<div><?=$entity->quantity</div>';
            }
        }
    }
    

    【讨论】:

    • 但问题是我如何使用 cookie 获取相关数据。因为我只存储 cookie 的主键。
    • 我可以帮助您,您的数据库设置如何以及关联是什么?
    • 已编辑...未经测试,但它应该为您提供要点。
    猜你喜欢
    • 2018-08-29
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    相关资源
    最近更新 更多