问题
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);
}
}
回答1:
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.
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.
来源:https://stackoverflow.com/questions/33394831/sylius-how-to-inject-request-arguments-in-custom-productrepository