简体   繁体   English

sylius购物车捆绑包-FattalErrorException:ItemResolver :: resolve()必须与

[英]sylius cart-bundle - FattalErrorException : ItemResolver::resolve() must be compatible with

I've installed sylius cart-bundle in my symfony 2 application. 我已经在symfony 2应用程序中安装了sylius cart-bundle。

When I try to had a product in the cart I have the following exception: 当我尝试在购物车中购买产品时,出现以下异常:

FatalErrorException: Compile Error: Declaration of Lyckee\\StoreBundle\\Cart\\ItemResolver::resolve() must be compatible with Sylius\\Bundle\\CartBundle\\Resolver\\ItemResolverInterface::resolve(Sylius\\Bundle\\CartBundle\\Model\\CartItemInterface $item, $data) in /Applications/MAMP/htdocs/Symfony/src/Lyckee/StoreBundle/Cart/ItemResolver.php line 13 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行

I found somebody having the same problem than me here: Sylius CartBundle Symfony2 我在这里发现有人与我有相同的问题: Sylius CartBundle Symfony2

He solves it with a problem in the service.yml file, but it's not working for me. 他在service.yml文件中解决了一个问题,但是对我不起作用。

My CartItem class: 我的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;
    }}

My Item Resolver 我的项目解析器

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');
    }
}

you got this error because of when you call resolve method 您收到此错误的原因是您在调用resolve方法时

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

at that time you have to pass first parameter $item which has to be instace of CartItem class. 那时,您必须传递第一个参数$item ,该参数必须是CartItem类的实例。

Please checkout the documentation for more detail regarding Cart Provider & Resolver. 请查看文档以获取有关购物车提供商和解决者的更多详细信息。

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

该文档需要更新,您只需要删除“请求”即可:

public function resolve(CartItemInterface $item, $request)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将产品添加到购物车时,“提交的表单无效”:Sylius购物车包 - “Submitted form is invalid” when adding a product to cart : Sylius Cart Bundle Sylius \\ Component \\ Shipping \\ Model \\ ShippingMethodTranslation必须与Sylius \\ Component \\ Shipping \\ Model \\ ShippingMethod :: createTranslation()兼容: - Sylius\Component\Shipping\Model\ShippingMethodTranslation must be compatible with Sylius\Component\Shipping\Model\ShippingMethod::createTranslation(): 添加到购物车时Sylius错误 - Sylius error when adding to cart 在购物车中提交多种产品(Sylius) - Submit multiple products in cart (Sylius) 使用Ajax添加到Sylius中的购物车 - Using Ajax to add to cart in Sylius 如何从sylius中注销捆绑软件? - How to deregister bundle from sylius? symfony 2 sylius bundle lost(TranslationBundle) - symfony 2 sylius bundle lost (TranslationBundle ) 添加项目后Sylius不记得购物车 - Sylius Not Remembering Cart After Adding an Item Sylius \\ Bundle \\ TaxonomiesBundle \\ Doctrine \\ ORM \\ TaxonRepository :: getTaxonsAsList() - Sylius\Bundle\TaxonomiesBundle\Doctrine\ORM\TaxonRepository::getTaxonsAsList() 路径“ sylius_cart.resources.cart.classes.form”的类型无效。 预期的数组,但是得到了字符串symfony 2.8.3 sylius 0.17.0 - Invalid type for path “sylius_cart.resources.cart.classes.form”. Expected array, but got string symfony 2.8.3 sylius 0.17.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM