duanji4870 2017-02-25 04:56
浏览 33
已采纳

将EntityManager注入AbstractResourceType Sylius

I've been looking all day for a way to access a channel entity within the ProductType (which extends AbstractResourceType) in Sylius. Specifically, I need to get the 'US_Web_Store' channel entity within the buildForm() function of the ProductType class (src/Sylius/Bundle/ProductBundle/Form/Type/ProductType.php)

Here's my code:

src/Sylius/Bundle/ProductBundle/Form/Type/ProductType.php

namespace Sylius\Bundle\ProductBundle\Form\Type;

use Doctrine\ORM\EntityManagerInterface;

class ProductType extends AbstractResourceType
{

    private $variantResolver;
    private $em;

    public function __construct($dataClass, $validationGroups, ProductVariantResolverInterface $variantResolver, EntityManagerInterface $em) {
        parent::__construct($dataClass, $validationGroups);

        $this->variantResolver = $variantResolver;
        $this->em = $em;
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {

        $product = $builder->getData();
        $product->addChannel($this->container->get('sylius.repository.channel')->findOneBy(['code' => 'US_Web_Store']));

        [...]
    }

}

Then in src/Sylius/Bundle/ProductBundle/Resources/config/services.xml:

<service id="product.form.type.product" class="Sylius\Bundle\ProductBundle\Form\Type\ProductType">
    <argument type="service" id="doctrine.orm.entity_manager"/>
</service>

And the error I'm getting is:

Type error: Argument 4 passed to Sylius\Bundle\ProductBundle\Form\Type\ProductType::__construct() must implement interface Doctrine\ORM\EntityManagerInterface, none given

I've also tried passing the channel repository via:

But had the same issue. Nothing is ever passed to the controller, so I'm sure I'm missing something fundamental here.

  • 写回答

1条回答 默认 最新

  • dongpo2340 2017-02-26 00:10
    关注

    There are a few issues in your code.

    1. You're using a ProductType from Sylius, which means it will be used instead of the default one. However, your service id is different than this in Sylius (you have product.form.type.product, in Sylius it's sylius.form.type.product). In a result, you'll have two services using the same Sylius\Bundle\ProductBundle\Form\Type\ProductType. Exception that you have is caused by the sylius.form.type.product service which now uses your class that requires fourth parameter, but it does not get it. Event if you'd name the service with the same key, it would still throw an exception, as you need to pass all 4 parameters into constructor in service definition, as it already have been notices by craigh. If you don't know what they are, you should look at the definition of sylius.form.type.product service. To get known about the most proper way of overriding Sylius forms, checkout the Customizing Forms chapter from docs.
    2. If you want to get the channel by code, you should always use repository for that (sylius.repository.channel service).
    3. If you just want to set a channel on newly created product, why just not use the custom factory for that? It's more clean way and should not result in any unexpected errors in the future. Again, check out proper documentation chapter to find out how to do it properly.

    Hope it will help you ;)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题