2014-03-03 3 views
0

다음과 같은 문제가 있습니다. 클래스가 있습니다 CartItem입니다. 나는 CartItem의 객체들의 배열을 세션에 저장하려고한다. (사실 나는 장바구니를 구현하고있다.) 내가 잘못이 라인을 쓰고 있어요 생각CakePHP 직렬화 객체

function addToCart(){ 

    $this->loadModel("Cart"); 

    $this->layout = false; 
    $this->render(false); 

    $cart = array(); 

    $tempcart = unserialize($this->Session->read("cart")); 

    if(isset($tempcart)){ 
     $cart = $tempcart; 
    } 

    $productId = $this->request->data("id"); 

    if(!$this->existsInCart($cart, $productId)){ 

     $cartItem = new Cart(); 

     $cartItem->productId = $productId; 
     $cartItem->createdAt = date(); 

     $cart[] = $cartItem; 

     $this->Session->write("cart", serialize($cart)); 

     echo "added"; 
    } 
    else 
     echo "duplicate"; 
} 

:

$tempcart = unserialize($this->Session->read("cart")); 

$this->Session->write("cart", serialize($cart)); 

것은 내가 세션에서 데이터를받지 못했습니다으로

class CartItem extends AppModel{ 

    var $name = "CartItem"; 

    var $useTable = false; 
} 

나는이 시도.

+0

적절한 카트 구현을 위해 https://github.com/burzum/cakephp-cart-plugin을보십시오 – burzum

답변

1

전체 Cart 개체를 세션에 추가하려고합니다.

당신은 당신이 세션에 객체를 추가해야하는 경우, 당신은 __sleep and __wakeup magic functions을 사용할 수 있습니다

$cart[] = array(
    'productId' => $productId, 
    'createdAt' => date('Y-m-d H:i:s') 
); 

처럼 배열을 추가해야하지만이 경우에는 단지에만 제품 ID를 추가하는 것이 좋습니다 생각하고 세션 날짜.

+0

고넬 고마워요.하지만 알고 싶습니다. 세션에 개체 배열을 저장할 수 없습니다. Jvaa에서 Serializable 인터페이스 구현) – Ashutosh

+0

답변을 업데이트했습니다. – cornelb