0

我正在使用“ajaxSubmitButton”向控制器(控制器名称:actionCart)发送 3 个字段的一些值:注册人、产品 ID 和数量。提交按钮后,我在会话数组中收到了我成功完成的那些值。此时,如果我提交相同的产品 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']);

    }

}

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

4

1 回答 1

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;
    }
}
于 2013-11-04T12:23:42.947 回答