2011-05-24 9 views
0

Zend_NavigationZend_Navigation_Containerを継承します。 findOneBy()findAllBy()、およびfindBy()はすべて再帰的にページを検索しますが、removePage()は機能しません。これは、page_10がルートレベルのナビゲーションノードである場合にのみ、$navigation->removePage($navigation->findOneBy('id', 'page_10'));が機能することを意味します。誰かがこれに遭遇し、回避策を見つけましたか?Zend Navigation(Container) - removePage()は再帰的ではありません


自分の解決策を見つけて、それを実装した方法の1つを受け入れました。私はそれが私より優れているなら、他の誰かから解決策を選択します。

答えて

2

Zend_NavigationZend_Navigation_Containerを再帰的に削除します。

Zend_Navigation_Containerを拡張My_Navigation_Containerを作成します。

abstract class My_Navigation_Container extends Zend_Navigation_Container 
{ 
    /** 
    * Remove page(s) matching $property == $value 
    * 
    * @param string $property 
    * @param mixed $value 
    * @param bool $all 
    * @return My_Navigation_Container 
    */ 
    public function removeBy($property, $value, $all = false) 
    { 
     $pages = array(); 

     if ($all) { 
      $pages = $this->findAllBy($property, $value); 
     } else { 
      if ($page = $this->findOneBy($property, $value)) { 
       $pages[] = $page; 
      } 
     } 

     foreach ($pages as $page) { 
      $this->removePageRecursive($page); 
     } 

     return $this; 
    } 


    /** 
    * Recursively removes the given page from the container 
    * 
    * @param Zend_Navigation_Page $page 
    * @return boolean 
    */ 
    public function removePageRecursive(Zend_Navigation_Page $page) 
    { 
     if ($this->removePage($page)) { 
      return true; 
     } 

     $iterator = new RecursiveIteratorIterator($this, RecursiveIteratorIterator::SELF_FIRST); 
     foreach ($iterator as $pageContainer) { 
      if ($pageContainer->removePage($page)) { 
       return true; 
      } 
     } 

     return false; 
    } 
} 

My_Navigation_Containerを拡張Zend_Navigationのコピーを作成します。

class My_Navigation extends My_Navigation_Container 
{ 
    /** 
    * Creates a new navigation container 
    * 
    * @param array|Zend_Config $pages [optional] pages to add 
    * @throws Zend_Navigation_Exception if $pages is invalid 
    */ 
    public function __construct($pages = null) 
    { 
     if (is_array($pages) || $pages instanceof Zend_Config) { 
      $this->addPages($pages); 
     } elseif (null !== $pages) { 
      throw new Zend_Navigation_Exception('Invalid argument: $pages must be an array, an instance of Zend_Config, or null'); 
     } 
    } 
} 
0

は親を見つけて、子供を削除します。これには、親の属性に関する知識が必要です。

$navigation->findOneBy('id', 'parent_id') 
     ->removePage($navigation->findOneBy('id', 'child_id')); 
関連する問題