StreetCoder
StreetCoder

Reputation: 10061

How to update the value of array in Yii session

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.

Upvotes: 0

Views: 2541

Answers (1)

CreatoR
CreatoR

Reputation: 1652

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

Upvotes: 2

Related Questions