dpn517111 2016-03-17 17:12
浏览 29

缺少Sylius \ Bundle \ CartBundle \ Form \ Type \ CartItemType :: __ construct()的参数1

Trying to implement syliusCart.

When i use <a href="{{ path('sylius_cart_item_add', {'productId': product.id}) }}">Add product to cart</a> it adds the product properly but when i try to add product to cart item using form i get error

Warning: Missing argument 1 for Sylius\Bundle\CartBundle\Form\Type\CartItemType::__construct(), called in C:\xampp\htdocs kw\src\AppBundle\Controller\DefaultController.php on line 88 and defined.

BEFORE the error my IDE throw a

required parameter :dataClass missing.

when i decleared the formType new OrderItemType()

confiq

sylius_cart:
resolver: app.cart_item_resolver
resources:
    cart:
        classes:
            model: %sylius.model.order.class%
            form:
                 default: AppBundle\Form\Type\OrderType
    cart_item:
        classes:
            model: %sylius.model.order_item.class%
            form:
                default: AppBundle\Form\Type\OrderItemType

CartItemResolver

use AppBundle\Form\Type\OrderItemType;
use AppBundle\Entity\OrderItem;
use Sylius\Component\Cart\Model\CartItemInterface;
use Sylius\Component\Cart\Resolver\ItemResolverInterface;
use Sylius\Component\Cart\Resolver\ItemResolvingException;
use Doctrine\ORM\EntityManager;

class ItemResolver implements ItemResolverInterface
{
private $entityManager;

public function __construct(EntityManager $entityManager)
{
    $this->entityManager = $entityManager;
}

public function resolve(CartItemInterface $item, $request)
{
    $productId = $request->query->get('productId');
    $item = new OrderItem();
    // 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. Report this to chuma@nimikiddies.com');
    }


    $this->isStockAvailable($product);

    // Assign the product to the item and define the unit price.
    $item->setProduct($product);
    $item->setUnitPrice($product->getPrice());


    // Everything went fine, return the item.
    return $item;
}


private function isStockAvailable($product)
{
}

private function getProductRepository()
{
    return $this->entityManager->getRepository('AppBundle:Product');
}
}

controller

   namespace AppBundle\Controller;

   use Sylius\Bundle\CartBundle\Form\Type\CartItemType;
   use AppBundle\Form\Type\OrderItemType;
   use AppBundle\Entity\OrderItem;
   use AppBundle\Entity\Department;
   use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
   use Symfony\Bundle\FrameworkBundle\Controller\Controller;
   use Symfony\Component\Console\Question\Question;
   use Symfony\Component\HttpFoundation\Request;
   use Symfony\Component\Form\FormFactory;

  class DefaultController extends Controller
  {


    /**
     * @Route("/brows/{id}", name="brows product")
    */
    public function browsProductAction(Request $request,$id)
   {

    $Product = $this->getDoctrine()->getRepository('AppBundle:Product');
    $product = $Product->find($id);

    $customersChoiceProducts = $Product->mostView('20');
    $sponsoredProducts = $Product->findAllRecentProducts('15');
    $similarProducts = $Product->findAllRecentProducts('15');

    //$rescentNews= $Product->findAllRescentPublish('19');
    if ($product) {

        $curView = $product->getView();
        $upView = $curView + 1;
        $product->setView($upView);

        $em = $this->getDoctrine()->getManager();
        $em->persist($product);
        $em->flush();

    }

    $orderItem = new OrderItem();


    $form = $this->createForm(new OrderItemType(),$orderItem);


    $form->handleRequest($request);

    if ($form->isValid()) {

        $orderItem->setProduct($product);
        $orderItem->setUnitPrice($product->getPrice());
        $em = $this->getDoctrine()->getManager();
        $em->persist($orderItem);
        $em->flush();
    }


    return $this->render('default/brows.html.twig', array(

        'selectedProduct' =>  $product,
        'customersChoiceProducts' => $customersChoiceProducts,
        'sponsoredProducts' => $sponsoredProducts,
        'similarProducts'=>$similarProducts,
        'form' =>$form->createView(),

    ));
   }

form OrderItemType

    namespace AppBundle\Form\Type;
    use Sylius\Bundle\CartBundle\Form\Type\CartItemType;
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolver;

  class OrderItemType extends CartItemType
{
/**
 * @param FormBuilderInterface $builder
 * @param array                $options
 */


public function buildForm(FormBuilderInterface $builder, array $options)
{
       parent::buildForm($builder, $options); // Add default fields.

}

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\OrderItem',
    ));


}

}

CartItemType

   namespace Sylius\Bundle\CartBundle\Form\Type;

    use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
    use Symfony\Component\Form\DataMapperInterface;
    use Symfony\Component\Form\FormBuilderInterface;

   /**
    * @author Paweł Jędrzejewski <pawel@sylius.org>
   */
  class CartItemType extends AbstractResourceType
{
/**
 * @var DataMapperInterface
 */
protected $orderItemQuantityDataMapper;

/**
 * @param string $dataClass
 * @param array $validationGroups
 * @param DataMapperInterface $orderItemQuantityDataMapper
 */
public function __construct($dataClass, array $validationGroups = [], DataMapperInterface $orderItemQuantityDataMapper)
{
    parent::__construct($dataClass, $validationGroups);

    $this->orderItemQuantityDataMapper = $orderItemQuantityDataMapper;
}

/**
 * {@inheritdoc}
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('quantity', 'integer', [
            'attr' => ['min' => 1],
            'label' => 'sylius.form.cart_item.quantity',
        ])
        ->setDataMapper($this->orderItemQuantityDataMapper);
}

/**
 * {@inheritdoc}
 */
public function getName()
{
    return 'sylius_cart_item';
}
 }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 个人网站被恶意大量访问,怎么办
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制