我有一个示例树。
| Brands
|-- SuperTees
|-- Stickypicky
|-- Mugland
|-- Bookmania
我可以通过品牌的子类别获得所有产品,但我无法通过品牌获得所有产品。 请帮忙创建查询
答案 0 :(得分:2)
我自己解决了这个问题,但我不确定做对了什么。 我覆盖控制器和存储库。 这是我的控制者。
use Symfony\Component\HttpFoundation\Request;
use Sylius\Bundle\CoreBundle\Controller\ProductController as BaseProductController;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Sylius\Component\Taxonomy\Model\Taxon;
class ProductController extends BaseProductController
{
public function indexByTaxonomyAction(Request $request, $permalink)
{
/** @var Taxon $taxon */
$taxon = $this->get('sylius.repository.taxon')
->findOneByPermalink($permalink);
if (!isset($taxon)) {
throw new NotFoundHttpException('Requested taxon does not exist.');
}
$ids = array($taxon->getId());
/** @var Taxon $child */
foreach ($taxon->getChildren() as $child) {
array_push($ids, $child->getId());
}
$paginator = $this
->getRepository()
->createByTaxonsPaginator($ids);
$paginator->setMaxPerPage($this->config->getPaginationMaxPerPage());
$paginator->setCurrentPage($request->query->get('page', 1));
return $this->render(
$this->config->getTemplate('indexByTaxonomy.html'),
array(
'taxon' => $taxon,
'products' => $paginator,
)
);
}
}
这是我的存储库
use Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository as BaseProductRepository;
class ProductRepository extends BaseProductRepository
{
public function createByTaxonsPaginator(array $ids)
{
$queryBuilder = $this->getCollectionQueryBuilder();
$queryBuilder
->innerJoin('product.taxons', 'taxon')
->andWhere('taxon.id IN ( :ids )')
->setParameter('ids', $ids)
;
return $this->getPaginator($queryBuilder);
}
}