我已经在我的symfony 2应用程序中安装了sylius cart-bundle。
当我尝试在购物车中放入产品时,我有以下例外:
FatalErrorException:编译错误:Lyckee \ StoreBundle \ Cart \ ItemResolver :: resolve()声明必须与Sylius \ Bundle \ CartBundle \ Resolver \ ItemResolverInterface :: resolve兼容(Sylius \ Bundle \ CartBundle \ Model \ CartItemInterface $ item ,$ data)/Applications/MAMP/htdocs/Symfony/src/Lyckee/StoreBundle/Cart/ItemResolver.php第13行
我发现有人在这里遇到与我相同的问题:Sylius CartBundle Symfony2
他在service.yml文件中遇到问题解决了这个问题,但它对我不起作用。
我的CartItem类:
namespace Lyckee\StoreBundle\Entity;
use Sylius\Bundle\CartBundle\Model\CartItem as BaseCartItem;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table()
* @ORM\Entity
* @ORM\Table(name="lyckee_cart_item_1")
*/
class CartItem extends BaseCartItem
{
/**
*@ORM\ManyToOne(targetEntity="Lyckee\StoreBundle\Entity\Product")
* @ORM\JoinColumn(nullable=false))
*/
private $product;
/**
* Get product
*
* @return \Lyckee\StoreBundle\Entity\Product
*/
public function getProduct()
{
return $this->product;
}
/**
* Set product
*
* @param \Lyckee\StoreBundle\Entity\Product $product
* @return CartItem
*/
public function setProduct(\Lyckee\StoreBundle\Entity\Product $product)
{
$this->product = $product;
}}
我的物品解析器
namespace Lyckee\StoreBundle\Cart;
use Sylius\Bundle\CartBundle\Model\CartItemInterface;
use Sylius\Bundle\CartBundle\Resolver\ItemResolverInterface;
use Sylius\Bundle\CartBundle\Resolver\ItemResolvingException;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManager;
class ItemResolver implements ItemResolverInterface
{
private $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function resolve(CartItemInterface $item, Request $request)
{
//$productId = $request->query->get('productId');
// If no product id given, or product not found, we throw exception with nice message.
if (!$productId || !$product = $this->getProductRepository()->find($productId)) {
throw new ItemResolvingException('Requested product was not found');
}
// Assign the product to the item and define the unit price.
$item->setProduct($product);
$item->setUnitPrice($product->getPrix());
// Everything went fine, return the item.
return $item;
}
private function getProductRepository()
{
return $this->entityManager->getRepository('LyckeeStoreBundle:Product');
}
}
答案 0 :(得分:0)
由于您调用resolve方法时出现此错误
public function resolve(CartItemInterface $item, Request $request)
那时你必须传递第一个参数$item
,它必须是CartItem类的实例。
请查看文档,了解有关Cart Provider& amp ;;的详细信息。解析器。
http://sylius.readthedocs.org/en/latest/bundles/SyliusCartBundle/services.html
答案 1 :(得分:0)
需要更新文档,您只需要删除"请求" :
public function resolve(CartItemInterface $item, $request)