2015-04-17 58 views
0

我想查詢一個動態類別邊欄,我使用的是Sonata ClassificationBundle。我創建了幾個類別(父級)和一些子類別(子級)。我能夠顯示類別,但是我無法理解如何查詢特定類別下的子類別。我想我需要檢查該類別是否有孩子並顯示它?我不知道怎麼做,如果沒有適當的文件..Symfony2 SonataClassificationBundle從數據庫查詢

這是側邊欄控制器:

<?php 

namespace Mp\ShopBundle\Controller; 

use Sonata\ClassificationBundle\Entity\Category; 
use Sonata\ClassificationBundle\Model\CategoryInterface; 
use Sonata\Component\Currency\CurrencyDetector; 
use Sonata\Component\Product\Pool; 
use Sonata\ProductBundle\Entity\ProductSetManager; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 

class SidebarController extends Controller 
{ 
    /** 
    * @ Route("/hello/{name}") 
    * @ Template() 
    */ 
    public function sidebarAction() 
    { 
     $page  = $this->getRequest()->get('page', 1); 
     $displayMax = $this->getRequest()->get('max', 9); 
     $displayMode = $this->getRequest()->get('mode', 'grid'); 
     $filter  = $this->getRequest()->get('filter'); 
     $option  = $this->getRequest()->get('option'); 

     if (!in_array($displayMode, array('grid'))) { // "list" mode will be added later 
      throw new NotFoundHttpException(sprintf('Given display_mode "%s" doesn\'t exist.', $displayMode)); 
     } 

     $category = $this->retrieveCategoryFromQueryString(); 

     return $this->render('sidebar.html.twig', array(
      'display_mode' => $displayMode,   
      'category' => $this->getCategoryManager()->findBy(
      array('parent' => 1)),   
      'subcategory' => $this->getCategoryManager()->findBy(
      array('children' => true)), ////// ERROR You cannot search for the association field 'Application\Sonata\ClassificationBundle\Entity\Category#children', because it is the inverse side of an association. Find methods only work on owning side associations." 
      'provider'  => $this->getProviderFromCategory($category), 
     )); 
    } 


      /* $em = $this->getDoctrine()->getManager(); 
      $products = $em->getRepository('MpShopBundle:Product')->findAll(); 
       return $this->render('sidebar.html.twig', array(
       'products'=>$products  
       )); */ 

    /** 
    * Retrieve Category from its id and slug, if any. 
    * 
    * @return CategoryInterface|null 
    */ 
    protected function retrieveCategoryFromQueryString() 
    { 
     $categoryId = $this->getRequest()->get('category_id'); 
     $categorySlug = $this->getRequest()->get('category_slug'); 

     if (!$categoryId || !$categorySlug) { 
      return null; 
     } 

     return $this->getCategoryManager()->findOneBy(array(
      'id'  => $categoryId, 
      'enabled' => true, 
     )); 
    } 

這是我嘗試顯示側邊欄:

{% for categories in category %} 

      {% for subcategories in subcategory %} 

       <li class="subMenu"><a> {{ categories.name }} [{{ categories|length }}]</a> 
       <ul> 

        <li><a href="{{ path('products') }}">{{ subcategories.name }} ({{ categories|length }})</a></li> 

       </ul> 
       </li> 

      {% endfor %} 

      {% endfor %} 

的實體類:

<?php 

/* 
* This file is part of the Sonata project. 
* 
* (c) Sonata Project <https://github.com/sonata-project/SonataClassificationBundle/> 
* 
* For the full copyright and license information, please view the LICENSE 
* file that was distributed with this source code. 
*/ 

namespace Sonata\ClassificationBundle\Model; 

use Sonata\MediaBundle\Model\MediaInterface; 

interface CategoryInterface 
{ 
    /** 
    * @param $name 
    * 
    * @return mixed 
    */ 
    public function setName($name); 

    /** 
    * Get name 
    * 
    * @return string $name 
    */ 
    public function getName(); 

    /** 
    * Set enabled 
    * 
    * @param boolean $enabled 
    */ 
    public function setEnabled($enabled); 

    /** 
    * Get enabled 
    * 
    * @return boolean $enabled 
    */ 
    public function getEnabled(); 

    /** 
    * Set slug 
    * 
    * @param integer $slug 
    */ 
    public function setSlug($slug); 

    /** 
    * Get slug 
    * 
    * @return integer $slug 
    */ 
    public function getSlug(); 

    /** 
    * Set description 
    * 
    * @param string $description 
    */ 
    public function setDescription($description); 

    /** 
    * Get description 
    * 
    * @return string $description 
    */ 
    public function getDescription(); 

    /** 
    * @param integer $position 
    */ 
    public function setPosition($position); 

    /** 
    * @return integer 
    */ 
    public function getPosition(); 

    /** 
    * Add Children 
    * 
    * @param CategoryInterface $children 
    * @param boolean   $nested 
    */ 
    public function addChild(CategoryInterface $children, $nested = false); 

    /** 
    * Get Children 
    * 
    * @return \Doctrine\Common\Collections\Collection $children 
    */ 
    public function getChildren(); 

    /** 
    * Set children 
    * 
    * @param $children 
    */ 
    public function setChildren($children); 

    /** 
    * Return true if category has children 
    * 
    * @return boolean 
    */ 
    public function hasChildren(); 

    /** 
    * Set Parent 
    * 
    * @param CategoryInterface $parent 
    * @param boolean   $nested 
    */ 
    public function setParent(CategoryInterface $parent = null, $nested = false); 

    /** 
    * Get Parent 
    * 
    * @return CategoryInterface $parent 
    */ 
    public function getParent(); 

    /** 
    * @param MediaInterface $media 
    */ 
    public function setMedia(MediaInterface $media = null); 

    /** 
    * @return MediaInterface 
    */ 
    public function getMedia(); 

    /** 
    * @param ContextInterface $context 
    */ 
    public function setContext(ContextInterface $context); 

    /** 
    * @return ContextInterface 
    */ 
    public function getContext(); 
} 

我的想法 好吧,因爲所有父類別的parent_id都是1,所以我很容易顯示它們。子類別具有與父類別的ID相同的parent_id。有沒有可能以某種方式查詢這種情況?像parent_id = id?

回答

0

您應該簡單地在每個父類別上使用getChildren():實體應該是水合的,因此查詢已經爲您完成。

+0

你的意思是我應該寫categories.getChildren()並且子類別會顯示嗎?因爲我收到錯誤:「可捕獲的致命錯誤:類Doctrine \ ORM \ PersistentCollection的對象無法轉換爲字符串」。或者getChildren方法應該在別的地方?對不起,如果問題是愚蠢的,但這是我第一週與symfony。 :( – Dominykas55

+0

它返回一個數組,所以你需要在另一個循環中使用它來獲得循環 – Jean

+0

你再次保存了我的神經系統! – Dominykas55