Zend Navigation(容器)-removePage() 不是递归的

发布于 2024-11-09 14:01:28 字数 420 浏览 6 评论 0原文

Zend_Navigation 扩展了 Zend_Navigation_ContainerfindOneBy()findAllBy()findBy() 函数都以递归方式搜索页面,但 removePage()代码> 没有。这意味着 $navigation->removePage($navigation->findOneBy('id', 'page_10')); 仅当 page_10 是根目录时才有效 -级别导航节点。有其他人遇到过这种情况并找到解决方法吗?


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

Zend_Navigation extends Zend_Navigation_Container. The findOneBy(), findAllBy(), and findBy() functions all search for pages recursively, but removePage() does not. This means that $navigation->removePage($navigation->findOneBy('id', 'page_10')); will work ONLY if page_10 is a root-level navigation node. Has anyone else encountered this and found a work-around?


I have found my own solutions and accepted one of them as how I have implemented it. I will select a solution from someone else if it's better than mine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素年丶 2024-11-16 14:01:28

扩展 Zend_NavigationZend_Navigation_Container 以递归方式删除页面。

创建扩展 Zend_Navigation_ContainerMy_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_ContainerZend_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');
        }
    }
}

Extend Zend_Navigation and Zend_Navigation_Container to recursively remove pages.

Create My_Navigation_Container that extends 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;
    }
}

Make a copy of Zend_Navigation that extends 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');
        }
    }
}
亽野灬性zι浪 2024-11-16 14:01:28

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

$navigation->findOneBy('id', 'parent_id')
        ->removePage($navigation->findOneBy('id', 'child_id'));

Find the parent, and then remove the child. This requires knowledge of the parent's attributes:

$navigation->findOneBy('id', 'parent_id')
        ->removePage($navigation->findOneBy('id', 'child_id'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文