2013-07-27 35 views
3

我目前正在研究ZF2的博客模塊,並且我找到了一種方法來遍歷我的類別並檢查他們是否有孩子。Doctrine2 + ZF2 - 與子類別迭代

的問題是,我總是得到這個錯誤:

Fatal error: Call to a member function isEmpty() on a non-object in /var/www/microweb2/module/Blog/src/Blog/Entity/CategoryIterator.php on line 19 

這裏的第19行:return $this->_data->current()->getSubCategories()->isEmpty();

那麼現在,我CategoryIterator是實現\RecursiveIterator一類,但它不工作......我把這個代碼從這個博客:http://wildlyinaccurate.com/simple-nested-sets-in-doctrine-2

這裏是我的代碼:

Entity\Category

<?php 

namespace Blog\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use \Application\Entity\AbstractEntity; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* @ORM\Entity 
* @ORM\Table(name="blog_categories") 
*/ 
class Category extends AbstractEntity { 

    // OTHER USELESS CODE 

    /** 
    * Set parent 
    * 
    * @param \Blog\Entity\Category $parent 
    * @return Category 
    */ 
    public function setParent(\Blog\Entity\Category $parent = null) 
    { 
     $this->parent = $parent; 

     return $this; 
    } 

    /** 
    * Get parent 
    * 
    * @return \Blog\Entity\Category 
    */ 
    public function getParent() 
    { 
     return $this->parent; 
    } 


    /** 
    * Add subcategories 
    * 
    * @param \Blog\Entity\Category $subcategories 
    * @return Category 
    */ 
    public function addSubCategory(\Blog\Entity\Category $subcategories) 
    { 
     $this->subcategories[] = $subcategories; 

     return $this; 
    } 

    /** 
    * Remove subcategories 
    * 
    * @param \Blog\Entity\Category $subcategories 
    */ 
    public function removeSubCategory(\Blog\Entity\Category $subcategories) 
    { 
     $this->subcategories->removeElement($subcategories); 
    } 

    /** 
    * Get subcategories 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getSubCategories() 
    { 
     return $this->subcategories; 
    } 
} 

CategoryIterator

<?php 

namespace Blog\Entity; 

use Doctrine\Common\Collections\Collection; 

class CategoryIterator implements \RecursiveIterator 
{ 

    private $_data; 

    public function __construct(Collection $data) 
    { 
     $this->_data = $data; 
    } 

    public function hasChildren() 
    { 
     return $this->_data->current()->getSubCategories()->isEmpty(); 
    } 

    public function getChildren() 
    { 
     return new CategoryIterator($this->_data->current()->getSubCategories()); 
    } 

    public function current() 
    { 
     return $this->_data->current(); 
    } 

    public function next() 
    { 
     $this->_data->next(); 
    } 

    public function key() 
    { 
     return $this->_data->key(); 
    } 

    public function valid() 
    { 
     return $this->_data->current() instanceof \Blog\Entity\Category; 
    } 

    public function rewind() 
    { 
     $this->_data->first(); 
    } 

} 

而且我IndexController::indexAction()

public function indexAction() 
{ 
    $adapter = new SelectableAdapter($this->doctrine()->getRepository('Blog\Entity\Article')); 
    $paginator = new Paginator($adapter); 
    $paginator->setCurrentPageNumber($this->params()->fromQuery('page', 1)) 
       ->setItemCountPerPage(10); 


    $categories = $this->doctrine()->getRepository('Blog\Entity\Category')->findBy(array(), array('id' => 'ASC')); 

    $iterator = new RecursiveIteratorIterator(
     new CategoryIterator(
      new ArrayCollection($categories) 
     ), 
     RecursiveIteratorIterator::SELF_FIRST 
    ); 

    return new ViewModel(array(
     'articles' => $paginator, 
     'categories' => $iterator, 
    )); 
} 

別生氣,我所有的use的陳述是正確的!

這裏是我使用的categories變量局部視圖(一切順利通過,因爲它是沒有小類前工作...):

<h3>Catégories</h3> 
<ul class="unstyled side-links"> 
    <?php foreach ($this->categories as $index => $cat) : ?> 
     <?php 
     if ($cat->getParent() !== null) { 
      continue; 
     } 

     $class = ''; 
     if (isset($this->current)) { 
      if ($this->current === $cat->getId()) { 
       $class = 'class="active"'; 
      } 
     } 
     ?> 
     <li <?= $class; ?>> 
      <a href="<?= $this->url('blog/category', array('alias' => $cat->getAlias())); ?>"><?= $cat->getName(); ?></a> 
      <?php if ($this->categories->hasChildren()) : ?> 
      <ul class="unstyled side-links"> 
       <?php foreach ($this->categories->getChildren() as $scat) : ?> 
       <?php $class2 = ($scat->getId() === $this->current ? 'class="active"' : ''); ?> 
        <li <?= $class2; ?>> 
         <a href="<?= $this->url('blog/category', array('alias' => $scat->getAlias())); ?>"><?= $scat->getName(); ?></a> 
        </li> 
       <?php endforeach; ?> 
      </ul> 
      <?php endif; ?> 
     </li> 
    <?php endforeach; ?> 
</ul> 

所以,如果有人能告訴我,爲什麼它工作的傢伙,但不適合我,好吧,歡迎任何想法!

+2

使用Collection時,總是有一個'__construct()'並將你的集合設置爲空集合。當沒有設置任何東西時,該值就是空值;)和null沒有'isEmpty()';) – Sam

+0

我的'Category'實體有一個'__construct()'方法來初始化ArrayCollection。這就是爲什麼我不明白... –

回答

0

對於任何人在這裏出現同樣的問題 - 後鏈接顯示函數hasChildren略有不同。

public function hasChildren() 
{ 
    return (! $this->_data->current()->getChildCategories()->isEmpty()); 
} 

一旦添加了這個迭代器,就會顯示子元素。