Sylius:如何在自定义 ProductRepository 中注入(请求)参数?

BNE*_*BNE 2 php service arguments symfony sylius

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

sylius_search.repository:
    class: ShopBundle\Entity\SearchIndexRepository
    arguments: ['@doctrine.orm.entity_manager', '@sylius.repository.product', '@request_stack']
Run Code Online (Sandbox Code Playgroud)

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

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

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
Run Code Online (Sandbox Code Playgroud)

有没有一个选项可以注入我不知道的参数?这里的 Repo 扩展了默认的 Repo 并重载了方法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);
    }

}
Run Code Online (Sandbox Code Playgroud)

Cer*_*rad 5

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

sylius.repository.product:
  class:  ShopBundle\Entity\ProductRepository
  factory: ['@doctrine.orm.entity_manager', 'getRepository']
  arguments:
    - 'ShopBundle\Entity\Product'
  calls: [[setRequestStack, ['@request_stack']]]
Run Code Online (Sandbox Code Playgroud)

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

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

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