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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度