1
votes

I'm building a shop using some bundles provided by Sylius. I have installed the ResourceBundle, ProductBundle, OrderBundle and CartBundle so far.

I am having trouble when calling the 'sylius_cart_item_add' path on the product page:

ContextErrorException: Catchable Fatal Error: Argument 1 passed to Sylius\Bundle\CartBundle\Event\CartEvent::__construct() must be an instance of Sylius\Bundle\CartBundle\Model\CartInterface, instance of XXXX\OrderBundle\Entity\Order given, called in /XXXX/XXXX/XXXX/XXXX/vendor/sylius/cart-bundle/Sylius/Bundle/CartBundle/Provider/CartProvider.php on line 103 and defined in /XXXX/XXXX/XXXX/XXXX/vendor/sylius/cart-bundle/Sylius/Bundle/CartBundle/Event/CartEvent.php line 42

I have installed CartBundle following the documentation (I haven't created a CartItem entity, as is optional according to the guide). This is my config file for Sylius bundles (XXXX stands for my company name):

sylius_resource:
    resources:
        dinamic.product:
            driver: doctrine/orm
            classes:
                model: XXXX\ProductBundle\Entity\Product
                interface: Sylius\Bundle\ProductBundle\Model\ProductInterface
                controller: Sylius\Bundle\ResourceBundle\Controller\ResourceController
                repository: Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository

sylius_product:
    driver: doctrine/orm
    classes:
        product:
            model: XXXX\ProductBundle\Entity\Product
            controller: Sylius\Bundle\ProductBundle\Controller\ProductController
            repository: XXXX\ProductBundle\Repository\ProductRepository
            form: Sylius\Bundle\ProductBundle\Form\Type\ProductType

sylius_order:
    driver: doctrine/orm
    classes:
        order:
            model: XXXX\OrderBundle\Entity\Order
        order_item:
            model: XXXX\OrderBundle\Entity\OrderItem

sylius_money:
    driver: doctrine/orm

sylius_cart:
    resolver: XXXX.cart_item_resolver
    classes: ~

stof_doctrine_extensions:
    orm:
        default:
            sluggable: true
            timestampable: true

I would gladly appreciate any help, thanks in advance.

EDIT: looking in Sylius' code, the problem seems to be in CartBundle/Provider/CartProvider/getCart() function (line 101):

$this->cart = $this->repository->createNew();

This returns a Order object instead of a CartInterface one, seems that the repository is not configured correctly, any suggestions?

2

2 Answers

0
votes

Looks like you are missing use Sylius\Bundle\CartBundle\Model\CartInterface.

0
votes

I could work around the problem by making my Order entity implement CartInterface, I also modified my OrderItem entity to implement CartItemInterface.

Finally, this is my config.yml file

sylius_resource:
    resources:
        dinamic.product:
            driver: doctrine/orm
            classes:
                model: XXXX\ProductBundle\Entity\Product
                interface: Sylius\Bundle\ProductBundle\Model\ProductInterface
                controller: Sylius\Bundle\ResourceBundle\Controller\ResourceController
                repository: Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository
        dinamic.cart:
            driver: doctrine/orm
            classes:
                model: XXXX\OrderBundle\Entity\Cart
                interface: Sylius\Bundle\CartBundle\Model\CartInterface
                controller: Sylius\Bundle\ResourceBundle\Controller\ResourceController
                repository: Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository

sylius_product:
    driver: doctrine/orm
    classes:
        product:
            model: XXXX\ProductBundle\Entity\Product
            controller: Sylius\Bundle\ProductBundle\Controller\ProductController
            repository: XXXX\ProductBundle\Repository\ProductRepository
            form: Sylius\Bundle\ProductBundle\Form\Type\ProductType

sylius_order:
    driver: doctrine/orm
    classes:
        order:
            model: XXXX\OrderBundle\Entity\Order
        order_item:
            model: Dinamic\OrderBundle\Entity\OrderItem

sylius_money:
    driver: doctrine/orm

sylius_cart:
    resolver: dinamic.cart_item_resolver
    classes: ~

Also, this is my route entry with the call to the controller:

XXXX_cart_item_add:
    pattern: /agregar
    defaults: { _controller: sylius.controller.cart_item:addAction }

I still want to know why the ResourceBundle repository doesn't use Cart instead of Order