我有一个购物车类,我希望序列化以存储在会话变量中。
我的购物车类是
namespace Application\Model\Cart;
use Application\Model\AbstractModel;
use Zend\ServiceManager\ServiceManager;
use Application\Model\Cart\Product;
use Application\Model\Cart\ProductOptions;
use Application\Entity\Products;
class Cart extends AbstractModel
{
protected $products;
protected $totalIncVat;
protected $totalExVat;
public function __construct(ServiceManager $serviceManager = NULL)
{
parent::__construct($serviceManager);
$this->products = array();
$product = new Product();
$product->setProductId(1);
$option = new ProductOptions();
$product->addOption($option);
$this->products[] = $product;
}
public function __sleep()
{
return $this->products;
}
}
从构造函数中可以看出,我正在添加测试产品和产品选项。产品类存储在数组$this->products
中。
我的产品类是
namespace Application\Model\Cart;
use Application\Model\Cart\ProductOptions;
class Product
{
protected $productId;
protected $title;
protected $priceEachIncVat;
protected $priceEachVat;
protected $vat;
protected $qty;
protected $total;
protected $options;
public function __construct()
{
$this->options = array();
}
public function getProductId()
{
return $this->productId;
}
public function getTitle()
{
return $this->title;
}
public function getPriceEachIncVat()
{
return $this->priceEachIncVat;
}
public function getPriceEachVat()
{
return $this->priceEachVat;
}
public function getVat()
{
return $this->vat;
}
public function getQty()
{
return $this->qty;
}
public function getTotal()
{
return $this->total;
}
public function getOptions()
{
return $this->options;
}
public function setProductId($productId)
{
$this->productId = $productId;
return $this;
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function setPriceEachIncVat($priceEachIncVat)
{
$this->priceEachIncVat = $priceEachIncVat;
return $this;
}
public function setPriceEachVat($priceEachVat)
{
$this->priceEachVat = $priceEachVat;
return $this;
}
public function setVat($vat)
{
$this->vat = $vat;
return $this;
}
public function setQty($qty)
{
$this->qty = $qty;
return $this;
}
public function setTotal($total)
{
$this->total = $total;
return $this;
}
public function setOptions(Array $options)
{
$this->options = $options;
return $this;
}
public function addOption(ProductOptions $option)
{
$this->options[] = $option;
return $this;
}
}
最后我的产品选项类是
namespace Application\Model\Cart;
class ProductOptions
{
protected $optionId;
protected $name;
protected $price;
protected $valueId;
protected $value;
public function getOptionId()
{
return $this->optionId;
}
public function getName()
{
return $this->name;
}
public function getPrice()
{
return $this->price;
}
public function getValueId()
{
return $this->valueId;
}
public function getValue()
{
return $this->value;
}
public function setOptionId($optionId)
{
$this->optionId = $optionId;
return $this;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function setPrice($price)
{
$this->price = $price;
return $this;
}
public function setValueId($valueId)
{
$this->valueId = $valueId;
return $this;
}
public function setValue($value)
{
$this->value = $value;
return $this;
}
}
我遇到的问题是这些类没有正确序列化。
$serializer = new \Zend\Serializer\Adapter\PhpSerialize();
$serialized = $serializer->serialize($cart);
$cart = $serializer->unserialize($serialized);
当我从购物车构造中移除测试产品时,一切正常。产品阵列导致了这个问题。
我得到的错误是unserialize():41个字节的偏移40处的错误。
返回的序列化字符串是
0:27: “应用程序\模型\车\购物车”:1:{N;}
有谁知道我错过了什么?
非常感谢提前。
答案 0 :(得分:0)
我明白了。购物车类中的睡眠魔术方法应包含return array('products');