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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀