0

我已经在我的 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');
    }
}
4

2 回答 2

0

您收到此错误是因为您调用 resolve 方法时

 public function resolve(CartItemInterface $item, Request $request)

那时你必须传递第一个参数$item,它必须是 CartItem 类的实例。

请查看文档以获取有关 Cart Provider & Resolver 的更多详细信息。

http://sylius.readthedocs.org/en/latest/bundles/SyliusCartBundle/services.html

于 2014-06-06T10:24:33.670 回答
0

文档需要更新,您只需要删除 "Request" :

public function resolve(CartItemInterface $item, $request)
于 2014-06-06T18:41:09.430 回答