Sylius:如何在自定义 ProductRepository 中注入(请求)参数?
Sylius: How to inject (request) arguments in custom ProductRepository?
我想覆盖 createByTaxonPaginator() 方法,以便 indexByTaxon() 方法返回排序后的结果。该新方法应使用 Request 来获取 sort Get-Parameter。
为了订购搜索结果,我找到了服务并将其覆盖如下:
sylius_search.repository:
class: ShopBundle\Entity\SearchIndexRepository
arguments: ['@doctrine.orm.entity_manager', '@sylius.repository.product', '@request_stack']
也许这不是一个好的做法,我不知道。但它有效...
不幸的是,我没有找到 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
是否有注入我不知道的参数的选项?这里的 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);
}
}
我不确定我是否完全理解这个问题,但这里有一个定义存储库服务然后注入附加服务的示例。您不能对请求堆栈使用构造函数注入,因为存储库本身是使用实体管理器作为工厂创建的:
sylius.repository.product:
class: ShopBundle\Entity\ProductRepository
factory: ['@doctrine.orm.entity_manager', 'getRepository']
arguments:
- 'ShopBundle\Entity\Product'
calls: [[setRequestStack, ['@request_stack']]]
您需要将 setRequestStack 添加到您的自定义存储库。
您可能还想重新考虑使这些服务依赖于请求对象的整个概念。容易变得凌乱。在您的方法调用中将排序参数作为参数传递可能更好。
2019 年 9 月 13 日更新:
这个答案已经过时了。在大多数情况下,您会希望从 ServiceEntityRepository 派生您的存储库,然后将 RequestStack 注入构造函数。此答案对 2.x.
仍然有效
我想覆盖 createByTaxonPaginator() 方法,以便 indexByTaxon() 方法返回排序后的结果。该新方法应使用 Request 来获取 sort Get-Parameter。 为了订购搜索结果,我找到了服务并将其覆盖如下:
sylius_search.repository:
class: ShopBundle\Entity\SearchIndexRepository
arguments: ['@doctrine.orm.entity_manager', '@sylius.repository.product', '@request_stack']
也许这不是一个好的做法,我不知道。但它有效... 不幸的是,我没有找到 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
是否有注入我不知道的参数的选项?这里的 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);
}
}
我不确定我是否完全理解这个问题,但这里有一个定义存储库服务然后注入附加服务的示例。您不能对请求堆栈使用构造函数注入,因为存储库本身是使用实体管理器作为工厂创建的:
sylius.repository.product:
class: ShopBundle\Entity\ProductRepository
factory: ['@doctrine.orm.entity_manager', 'getRepository']
arguments:
- 'ShopBundle\Entity\Product'
calls: [[setRequestStack, ['@request_stack']]]
您需要将 setRequestStack 添加到您的自定义存储库。
您可能还想重新考虑使这些服务依赖于请求对象的整个概念。容易变得凌乱。在您的方法调用中将排序参数作为参数传递可能更好。
2019 年 9 月 13 日更新: 这个答案已经过时了。在大多数情况下,您会希望从 ServiceEntityRepository 派生您的存储库,然后将 RequestStack 注入构造函数。此答案对 2.x.
仍然有效