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

时间:2013-11-04 12:05:54

标签: php arrays session multidimensional-array yii

我使用“ajaxSubmitButton”发送3个字段的值:注册人,产品ID和数量到控制器(控制器名称:actionCart)。提交按钮后,我在会话数组中收到了我成功完成的那些值。此时,如果我提交相同的产品ID但数量不同,我想用新值更新数量密钥。我已经通过php global $ _SESSION完成了这个,但不能使用Yii会话变量。

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']);

    }

}

在上面的代码中,我通过注释来标记我想要更新会话值的位置。如果可能的话,请帮助我。

1 个答案:

答案 0 :(得分:2)

试试这个:

$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返回CHttpSession的对象,而不是引用$ _SESSION。

$carts = $session['cart']等于操作$carts = $session->get('cart');(通过CHttpSession中的魔术方法__get)和$session['cart'] = $carts;等于$session->set('cart', $carts);(按__set)

这就是你无法通过$session['cart'][$key]['quantity'] = $quantity;

进行设置的原因

更新完整解决方案(我更改了保存产品的逻辑 - $ 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;
    }
}