2014-05-01 2 views
-1

예제 트리가 있습니다.sylius 분류학에 따라 제품을 얻는 방법?

| Brands 
|-- SuperTees 
|-- Stickypicky 
|-- Mugland 
|-- Bookmania 

브랜드별로 모든 제품을 구할 수 있지만 브랜드별로 모든 제품을 구할 수는 없습니다. 도움말 작성 요청을 도와주세요.

+0

모든 하위 범주를 Foreaching입니까? 그리고 현재 코드를 보여주십시오! –

답변

1

나는이 문제를 직접 해결했지만, 무엇이 옳은 일인지 잘 모르겠습니다. 컨트롤러와 저장소를 덮어 씁니다. 내 컨트롤러입니다.

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); 
    } 
} 
관련 문제