0

Zend_Navigation延伸Zend_Navigation_Container。、和函数都递归搜索页面,但findOneBy()不是。这意味着仅当它是根级导航节点时才有效。有没有其他人遇到过这个并找到了解决方法?findAllBy()findBy()removePage()$navigation->removePage($navigation->findOneBy('id', 'page_10'));page_10


我找到了自己的解决方案,并接受了其中一个作为我实施它的方式。如果它比我的更好,我会从其他人那里选择一个解决方案。

4

2 回答 2

2

扩展Zend_NavigationZend_Navigation_Container递归删除页面。

创建My_Navigation_Container扩展Zend_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;
    }
}

Zend_Navigation制作一个扩展的副本My_Navigation_Container

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');
        }
    }
}
于 2011-05-24T17:10:06.253 回答
0

找到父级,然后删除子级。这需要了解父母的属性:

$navigation->findOneBy('id', 'parent_id')
        ->removePage($navigation->findOneBy('id', 'child_id'));
于 2011-05-24T17:10:46.533 回答