[英]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.