dongzhe3171
2013-11-04 12:05
浏览 47
已采纳

如何在Yii会话中更新数组的值

I am using "ajaxSubmitButton" to send some values of 3 fields: registrant, product id and quantity to controller (controller name: actionCart). After submitting the button I receive those values in session array what I did successfully. At this point, if I submit same product id but different quantity, I want to update the quantity key with new value. I have done this by php global $_SESSION but can't using Yii session variable.

public function actionCart()
{

    if(isset($_POST["Order"])){
        $item = $_POST["Order"];
        $registration_id = $item["registration_id"];
        $this->productId = $item["item"];
        $quantity = $item["quantity"];
        $quantity = $item["quantity"]=='' ? 1 : $item["quantity"];

        $productInfo = Products::model()->findByPk(array('id'=>$this->productId));
        $totalPrice = $productInfo->price * $quantity;

        $newItem = array("product_id" => $this->productId , "product_name" => $productInfo->name, "quantity" => $quantity,"price" => $productInfo->price,"totalPrice" => $totalPrice);

        $session = Yii::app()->session;
        $cartArray = $session['cart'];

        $searchArrResult = $this->searchSubArray($session['cart'],'product_id',$this->productId);
        if (!empty($searchArrResult)) {
            /***** this works *****/
            foreach ( $_SESSION['cart'] as $key=>$cart ) {
                if ( $cart["product_id"] == $this->productId ) {
                    $_SESSION['cart'][$key]['quantity']=$quantity;
                    $_SESSION['cart'][$key]['totalPrice']=$totalPrice;
                }
            }
            /***** following commented code does not work *****
             *
             foreach($session['cart'] as $key=>$cart){
                if ($cart["product_id"] == $this->productId){
                    $session['cart'][$key]['quantity'] == $quantity;
                    $session['cart'][$key]['totalPrice'] == $totalPrice;
                }
            }*/

        }
        else {
            $cartArray[] = $newItem;
            $session['cart'] = $cartArray;
        }

        print_r($session['cart']);
        //unset(Yii::app()->session['cart']);

    }

}

In the above code I marked by commenting where I want to update session values. Please help me someone if it is possible do in yii.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyun1972 2013-11-04 12:23
    已采纳

    Try this:

    $carts = $session['cart'];
    foreach($carts as $key=>&$cart){
        if ($cart["product_id"] == $this->productId){
             $cart['quantity'] == $quantity;
             $cart['totalPrice'] == $totalPrice;
        }
    }
    $session['cart'] = $carts;
    

    Yii::app()->session return object of CHttpSession, not reference to $_SESSION.

    $carts = $session['cart'] equals operation $carts = $session->get('cart'); (by means magic method __get in CHttpSession) and $session['cart'] = $carts; equals to $session->set('cart', $carts); (by __set)

    That's why you can't setting by $session['cart'][$key]['quantity'] = $quantity;

    UPDATED full solution (I change logic of saving products - $key = product_id)

    public function actionCart()
    {
        if(isset($_POST["Order"])){
            $item = $_POST["Order"];
            $registration_id = $item["registration_id"];
            $this->productId = $item["item"];
            $quantity = empty(intval($item["quantity"])) ? 1 : intval($item["quantity"]);
            $productInfo = Products::model()->findByPk(array('id'=>$this->productId));.
            if(empty($productInfo))
                 return false; // or other action
            $newItem = array(
                 "product_id" => $this->productId , 
                 "product_name" => $productInfo->name, 
                 "quantity" => $quantity,
                 "price" => $productInfo->price,
                 "totalPrice" => ($productInfo->price * $quantity)
            );
            $cartArray = Yii::app()->session['cart'];
            $cartArray[$this->productId] = $newItem;
            Yii::app()->session['cart'] = $cartArray;
        }
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题