Sylius:如何注入定制ProductRepository(要求)的论点? [英] Sylius: How to inject (request) arguments in custom ProductRepository?

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

问题描述

我要覆盖的方法的 createByTaxonPaginator()使 indexByTaxon()方式还给排序结果。这种新方法应该使用的请求的获得的排序的获取参数。
对于订购的SearchResult我找到了服务,并推翻了如下:

  sylius_search.repository:
    等级:ShopBundle \\实体\\ SearchIndexRepository
    参数:['@ doctrine.orm.entity_manager','@ sylius.repository.product','@request_stack']

也许这是不是一个好的做法,我不知道。但是,它的工作原理...
不幸的是我没有找到任何sylius.repository.product服务定义为有需要的ARGS一起来看看。

在我的配置我有以下:

  sylius_product:
    类:
        产品:
            型号:ShopBundle \\实体\\产品#我自己的实体
            控制器:Sylius \\包\\ CoreBundle \\控制器\\ ProductController的
            库:ShopBundle \\实体\\ ProductRepository
            #是有争论注入一个选项?
            形成:
                默认:ShopBundle \\表格\\型号\\ ProductType
            翻译:
                型号:ShopBundle \\实体\\ ProductTranslation
                形成:
                    默认:ShopBundle \\表格\\型号\\ ProductTranslationType

是有注射Args中我不知道一个选项?在这里,它扩展了默认的和过载的方法回购 createByTaxonPaginator()

 < PHP命名空间ShopBundle \\实体;使用Sylius \\包\\ CoreBundle \\学说\\ ORM \\ ProductRepository为BaseProductRepository;
使用Sylius \\分量\\核心\\型号\\ TaxonInterface;类ProductRepository扩展BaseProductRepository
{    / **
     *创建特定类群分类下的产品分页程序。
     *修正:增加上市分类单元的排序
     *
     * @参数TaxonInterface $类群
     * @参数数组$标准
     *
     * @返回\\ Pagerfanta \\ Pagerfanta
     * /
    公共职能createByTaxonPaginator(TaxonInterface $类群,数组$ =标准阵列())
    {
        //这里我想有请求$请求ARG ..
        $的QueryBuilder = $这个 - > getCollectionQueryBuilder();
        $ QueryBuilder的
             - > innerJoin('product.taxons','类群')
             - > andWhere($ queryBuilder-> EXPR() - GT;或X(
                '类群=:类群,
                ':左< taxon.left和taxon.right< :对'
            ))
             - >的setParameter('类群',$类群)
             - >的setParameter(左,$ taxon-> getLeft())
             - >的setParameter('右',$ taxon-> GetRight时())
             - >的OrderBy('translation.name')// ...得到这个动态
        ;        $这个 - > applyCriteria($的QueryBuilder,$标准);        返回$这个 - > getPaginator($ QueryBuilder的);
    }}


解决方案

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

  sylius.repository.product:
  等级:ShopBundle \\实体\\ ProductRepository
  工厂:['@ doctrine.orm.entity_manager','getRepository']
  参数:
     - ShopBundle \\实体\\产品'
  呼叫:[[setRequestStack,['@request_stack']]]

您将需要setRequestStack添加到自定义库中。

您还可能要重新考虑使这些服务依赖请求对象的整个概念。往往会导致混乱。也许是更好地传递排序参数在你的方法参数调用。

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']

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

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']]]

You will need to add setRequestStack to your custom repository.

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.

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

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