Sylius:如何在自定义 ProductRepository 中注入(请求)参数? [英] Sylius: How to inject (request) arguments in custom ProductRepository?

查看:18
本文介绍了Sylius:如何在自定义 ProductRepository 中注入(请求)参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想覆盖方法 createByTaxonPaginator() 以便 indexByTaxon() 方法返回排序结果.这个新方法应该使用 Request 来获取 sort Get-Parameter.为了订购搜索结果,我找到了该服务并将其覆盖如下:

I want to override the method createByTaxonPaginator() so that the indexByTaxon() method gives back sorted results. That new method should use Request to get the sort Get-Parameter. For ordering the searchresults i found the service and overrode that as following:

sylius_search.repository:
    class: ShopBundle\Entity\SearchIndexRepository
    arguments: ['@doctrine.orm.entity_manager', '@sylius.repository.product', '@request_stack']

也许这不是一个好习惯,我不知道.但它有效...不幸的是,我没有找到 sylius.repository.product 的任何服务定义来查看所需的参数.

maybe that is not a good practice, i dont know. But it works... unfortunately i didnt find any service definition for sylius.repository.product to have a look on the required args.

在我的配置中,我有以下内容:

in my config i have following:

sylius_product:
    classes:
        product:
            model: ShopBundle\Entity\Product # My Own Entity
            controller: Sylius\Bundle\CoreBundle\Controller\ProductController
            repository: ShopBundle\Entity\ProductRepository
            # is there an option for injecting arguments?
            form:
                default: ShopBundle\Form\Type\ProductType
            translation:
                model: ShopBundle\Entity\ProductTranslation
                form:
                    default: ShopBundle\Form\Type\ProductTranslationType

是否有注入我不知道的 args 的选项?这里是扩展默认的 Repo 并重载方法 createByTaxonPaginator()

is there an option for injecting args which i didn't know? Here the Repo which extends the default one and overloads the Method createByTaxonPaginator()

 <?php

namespace ShopBundle\Entity;

use Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository as BaseProductRepository;
use Sylius\Component\Core\Model\TaxonInterface;

class ProductRepository extends BaseProductRepository
{

    /**
     * Create paginator for products categorized under given taxon.
     * Modified: Sorting of Taxon listing added
     *
     * @param TaxonInterface $taxon
     * @param array          $criteria
     *
     * @return \Pagerfanta\Pagerfanta
     */


    public function createByTaxonPaginator(TaxonInterface $taxon, array $criteria = array())
    {
        // Here i want to have the Request $request arg..
        $queryBuilder = $this->getCollectionQueryBuilder();
        $queryBuilder
            ->innerJoin('product.taxons', 'taxon')
            ->andWhere($queryBuilder->expr()->orX(
                'taxon = :taxon',
                ':left < taxon.left AND taxon.right < :right'
            ))
            ->setParameter('taxon', $taxon)
            ->setParameter('left', $taxon->getLeft())
            ->setParameter('right', $taxon->getRight())
            ->orderBy('translation.name') // ... to get this dynamic
        ;

        $this->applyCriteria($queryBuilder, $criteria);

        return $this->getPaginator($queryBuilder);
    }

}

推荐答案

我不确定我是否完全理解这个问题,但这里有一个定义存储库服务然后注入附加服务的示例.您不能对请求堆栈使用构造函数注入,因为存储库本身是使用实体管理器作为工厂创建的:

I'm not sure I completely understand the question but here is an example of defining a repository service and then injecting an additional service. You cannot use constructor injection for the request stack because the repository itself is created using the entity manager as a factory:

sylius.repository.product:
  class:  ShopBundle\Entity\ProductRepository
  factory: ['@doctrine.orm.entity_manager', 'getRepository']
  arguments:
    - 'ShopBundle\Entity\Product'
  calls: [[setRequestStack, ['@request_stack']]]

您需要将 setRequestStack 添加到您的自定义存储库.

You will need to add setRequestStack to your custom repository.

您可能还想重新考虑使这些服务依赖于请求对象的整个概念.容易变得凌乱.在方法调用中将 sort 参数作为参数传递可能会更好.

You might also want to rethink the whole notion of making these services depend on the request object. Tends to get messy. Might be better to pass the sort parameter as an argument in your methods calls.

2019 年 9 月 13 日更新:这个答案已经过时了.在大多数情况下,您需要从 ServiceEntityRepository 派生您的存储库,然后将 RequestStack 注入构造函数.这个答案对 2.x 仍然有效.

Update 13 Sep 2019: This answer is obsolete. In most cases you will want to derive your repository from the ServiceEntityRepository and then inject the RequestStack into the constructor. This answer is still valid for 2.x.

这篇关于Sylius:如何在自定义 ProductRepository 中注入(请求)参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆