为什么 martyshka/ShoppingCart ZF2 组件不工作(可能是 hidrator 问题)?
Why is martyshka/ShoppingCart ZF2 component not working (hidrator issue maybe)?
我在尝试实现 martyshka/ShoppingCart 组件时非常头疼。我所发现的只是在添加项目时 hydrator 为 null,但是当我强制它(在组件上设置它)时它也不起作用。
<?php
namespace Publico\Controller;
use Doctrine\ORM\EntityManager;
use ShoppingCart\Controller\Plugin\ShoppingCart;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class CarrinhoController extends AbstractActionController
{
private $carrinho;
private $entityManager;
protected function setCarrinho(ShoppingCart $cart)
{
$this->carrinho = $cart;
return $this;
}
protected function getCarrinho()
{
if (null === $this->carrinho) {
$this->setCarrinho(new ShoppingCart());
}
return $this->carrinho;
}
/*...*/
public function indexAction()
{
try {
$carrinho = $this->getCarrinho();
} catch (\Exception $e) {
die($e->getMessage());
}
$carrinhoItems = [
'carrinho' => $carrinho->cart(),
'valorTotal' => $carrinho->total_sum(),
'qtdTotal' => $carrinho->total_items(),
];
die($carrinhoItems);
return new ViewModel([
'carrinho' => $this->carrinho->cart(),
'valorTotal' => $this->carrinho->total_sum(),
'qtdTotal' => $this->carrinho->total_items(),
]);
}
/*...*/
}
本组件提供插件"ShoppingCart"使用,无需创建ShoppingCart对象
如果您正确地将此组件安装为模块,
只需在你的行动中使用它-
$this->ShoppingCart()
我在尝试实现 martyshka/ShoppingCart 组件时非常头疼。我所发现的只是在添加项目时 hydrator 为 null,但是当我强制它(在组件上设置它)时它也不起作用。
<?php
namespace Publico\Controller;
use Doctrine\ORM\EntityManager;
use ShoppingCart\Controller\Plugin\ShoppingCart;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class CarrinhoController extends AbstractActionController
{
private $carrinho;
private $entityManager;
protected function setCarrinho(ShoppingCart $cart)
{
$this->carrinho = $cart;
return $this;
}
protected function getCarrinho()
{
if (null === $this->carrinho) {
$this->setCarrinho(new ShoppingCart());
}
return $this->carrinho;
}
/*...*/
public function indexAction()
{
try {
$carrinho = $this->getCarrinho();
} catch (\Exception $e) {
die($e->getMessage());
}
$carrinhoItems = [
'carrinho' => $carrinho->cart(),
'valorTotal' => $carrinho->total_sum(),
'qtdTotal' => $carrinho->total_items(),
];
die($carrinhoItems);
return new ViewModel([
'carrinho' => $this->carrinho->cart(),
'valorTotal' => $this->carrinho->total_sum(),
'qtdTotal' => $this->carrinho->total_items(),
]);
}
/*...*/
}
本组件提供插件"ShoppingCart"使用,无需创建ShoppingCart对象
如果您正确地将此组件安装为模块,
只需在你的行动中使用它-
$this->ShoppingCart()