- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想重写方法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
是否有一个选项可以注入(inject)我不知道的参数?这里的 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);
}
}
最佳答案
我不确定我完全理解这个问题,但这里是定义存储库服务然后注入(inject)附加服务的示例。您不能对请求堆栈使用构造函数注入(inject),因为存储库本身是使用实体管理器作为工厂创建的:
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 注入(inject)构造函数中。这个答案对于 2.x 仍然有效。
关于php - Sylius:如何在自定义 ProductRepository 中注入(inject)(请求)参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33394831/
我想重写方法createByTaxonPaginator(),以便indexByTaxon()方法返回排序结果。该新方法应使用Request 来获取sort Get-Parameter。为了订购搜索结
我想重写方法createByTaxonPaginator(),以便indexByTaxon()方法返回排序结果。该新方法应使用Request 来获取sort Get-Parameter。为了订购搜索结
我是一名优秀的程序员,十分优秀!