使用cakephp 3

时间:2016-08-16 09:08:27

标签: cakephp temp-tables cakephp-3.2

我在CakePHP 3.2工作

我有一个表格Carts,用于在用户使用user_id登录时将商品存储在购物车中。

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

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

  

编辑2

存储在cookie / temp表中的值

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;
                  }
              }
          }

希望我对你很清楚。

1 个答案:

答案 0 :(得分:0)

您可以将其存储在客户的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>';
        }
    }
}