【问题标题】:How to update the value of array in Yii session如何在 Yii 会话中更新数组的值
【发布时间】:2013-11-04 12:05:53
【问题描述】:

我正在使用“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 中做,请帮助我。

【问题讨论】:

  • 也许你应该先试试Google
  • 我在google上试过,但没有找到任何解决方案。

标签: php arrays session multidimensional-array yii


【解决方案1】:

试试这个:

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

【讨论】:

  • 感谢很好的解释。但它不起作用。所以,我尝试将一行 $session['cart'] = $carts; 更改为 $session['cart'] = $cart; 但它改变了数组格式,即 Array ( [product_id] => 5 [product_name] => Denim [quantity] => 5 [price] => 500 [totalPrice] => 2500 [0] => Array ( [product_id] => 5 [product_name] => Denim [quantity] => 5 [price] => 500 [totalPrice] => 2500 ) )
  • 不,你必须离开$session['cart'] = $carts;,因为在foreach中我正在使用$key产品并通过引用更改它的信息(查看&$cart,这意味着如果我这样做@987654335 @我改了$carts[$key]['quantity'],我对你的代码做了一些优化)
  • 如果我选择单品就可以了。一旦我选择了多个产品,以下产品值就会与之前产品的值重叠。您已经在我的另一篇文章中回答了与之相关的内容。在这篇文章中,我已经给出了完整的控制器代码。如果我没有做错,请告诉我最终解决方案要解决的问题。
  • 请看我的完整解决方案
  • 您更新的代码运行良好。这真的是一个很酷的解决方案。非常感谢您耐心配合我。
猜你喜欢
  • 2017-06-10
  • 2020-06-06
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 2020-03-25
  • 2015-04-03
  • 1970-01-01
相关资源
最近更新 更多