使用数据库表创建 Zend_Navigation

发布于 2024-10-04 10:22:24 字数 322 浏览 4 评论 0 原文

我现在使用 xml 文件来创建 Zend 导航,

$navContainerConfig = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');
 $navContainer = new Zend_Navigation($navContainerConfig);

我的问题是,如果可能的话,我可以使用数据库表而不是 xml 文件来创建 zend_navgation 吗

?请给我一些关于如何使用数据库表制作 Zend_Navigation 的示例吗?

i use a xml file for creating a Zend navigation

$navContainerConfig = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');
 $navContainer = new Zend_Navigation($navContainerConfig);

now my question ,could i use a table of database instead of xml file for creating zend_navgation

if it is possible , please give me any example on how i make Zend_Navigation with a table of database?

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

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

发布评论

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

评论(4

画离情绘悲伤 2024-10-11 10:22:24

创建一个表,其中的列等于 MVC 和 URI 页面可能的组合属性 有两个例外:添加 id 列,而不是页面列,而是添加列 parent_id。子页面应在此处包含其父页面的 ID。

然后使用 Zend_Db_* 中的任何一个来获取该表中的所有数据。结果将是导航中所有页面的平面数组。您可以将其用作 容器 并将其提供给 Zend_Navigation。但是,如果您有嵌套导航,则该数组将无法执行,因为该数组将是扁平的。 遍历数组使其根据parent_id关系嵌套。然后将其提供给 Zend_Navigation

之后,您只需使用 导航助手来呈现导航。由于您的导航可能不会经常更改,因此您应该缓存从数据库中获取的内容。每次显示页面时,无需对数据库进行代价高昂的往返。

Create a table with columns equal to the combined properties possible for MVC and URI pages with two exceptions: add an id column and instead of having a pages column, you add a column parent_id. Subpages should contain the id of their parent page in here.

Then use any of Zend_Db_* to fetch all data in that table. The result will be a flat array of all pages in your navigation. You can use this as container and feed it to Zend_Navigation. However, if you have nested navigation that array won't do, because the array will be flat. Traverse the array to make it nested according to the parent_id relations. Then feed it to Zend_Navigation.

After that, you just have to use one of the Navigation Helpers to render the navigation. Since your navigation is likely not changing that often, you should cache the fetching from the database. There is no need to do a costly roundtrip to the database each time the page is shown.

半夏半凉 2024-10-11 10:22:24

假设您有这个表:

CREATE TABLE IF NOT EXISTS `menu` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `label` varchar(50) NOT NULL,
  `url` varchar(50) NOT NULL,
  `name` varchar(50) NOT NULL,
  `title` varchar(50) NOT NULL,
  `class` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3;

具有这些值:

INSERT INTO `menu` (`id`, `label`, `url`, `name`, `title`, `class`) VALUES
(1, 'Home', 'index/home', 'home', 'Home title', 'someClass'),
(2, 'About Us', 'index/about', 'about', 'About title', 'aboutClass');

您可以这样做:

    $dbAdapter = Zend_Db_Table::getDefaultAdapter();
    $dbAdapter->setFetchMode(Zend_Db::FETCH_ASSOC);
    $menuArray = $dbAdapter->fetchAll("SELECT * FROM menu");
    $navContainer = new Zend_Navigation();

    foreach ( $menuArray as $value )
    {
        $navContainer->addPage(
            Zend_Navigation_Page::factory(array(
                'uri' => $value['url'],
                'label' => $value['label'],
                'title' => $value['title'],
                'class' => $value['class'],
            ))
        );
    }

现在请小心,因为这个“设计”不允许您拥有子菜单项,因此您需要使用它,但希望它能让您更轻松地开始。

Asuming you have this table :

CREATE TABLE IF NOT EXISTS `menu` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `label` varchar(50) NOT NULL,
  `url` varchar(50) NOT NULL,
  `name` varchar(50) NOT NULL,
  `title` varchar(50) NOT NULL,
  `class` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3;

Wtih these values :

INSERT INTO `menu` (`id`, `label`, `url`, `name`, `title`, `class`) VALUES
(1, 'Home', 'index/home', 'home', 'Home title', 'someClass'),
(2, 'About Us', 'index/about', 'about', 'About title', 'aboutClass');

You can do this :

    $dbAdapter = Zend_Db_Table::getDefaultAdapter();
    $dbAdapter->setFetchMode(Zend_Db::FETCH_ASSOC);
    $menuArray = $dbAdapter->fetchAll("SELECT * FROM menu");
    $navContainer = new Zend_Navigation();

    foreach ( $menuArray as $value )
    {
        $navContainer->addPage(
            Zend_Navigation_Page::factory(array(
                'uri' => $value['url'],
                'label' => $value['label'],
                'title' => $value['title'],
                'class' => $value['class'],
            ))
        );
    }

Carefull now couse this "design" whont allow you to have submenu items so you'll need to play around with it , but hopefully it will get you started easyer .

奶茶白久 2024-10-11 10:22:24

我推荐以下解决方案之一:

  1. 序列化 XML 并将其存储在数据库中
  2. 使用 Xml_Writer 形式的 Zend 库并直接对写入到磁盘上的文件进行操作。
    我认为可写 XML 配置是最好的。写入操作并不常见,也不需要快如闪电,而且您可能会缓存 HTML 输出,因此读取量很小)。当您假设每个项目都有其唯一的 ID 时,使用迭代器很容易实现。
  3. 使用Nested Set结构将容器保存到数据库中。

I'd recommend one of the following solutions:

  1. Serialize the XML and store it in database
  2. Use Xml_Writer form Zend library and operate directly on the file written on disk.
    Writable XML config is the best in my opinion. Write operations are not so common and don't need to be lightning fast, and you will probably cache the HTML output, so the read is minimal). Easy to implement using iterators, when you assume each item has it's unique ID.
  3. Use Nested Set structure to save the container to the database.
记忆で 2024-10-11 10:22:24

你可以做一些这样的功能:)

/**
 * Gets assoc array (fetched from db) and transforms it to Zend_Navigation object
 * @param assoc array $array
 * @param string $idField
 * @param string $parentField
 * @return \Zend_Navigation 
 */
private function getNavigationFromAssocArray($array, $idField = 'menu_item_id', $parentField = 'parent_menu_item_id') {
    $navigation = new Zend_Navigation();

    foreach ($array as $key => $item) {
        $item['uri'] = $item['url'];

        if(empty($parentField) ) {    
            $navigation->addPage(
                Zend_Navigation_Page::factory( $item )
            );
        } else {
            $page = $navigation->findBy($idField, $parentField );
            $page->addPage(Zend_Navigation_Page::factory( $item ));
        }
    }
    return $navigation;
}

You can do some function like this :)

/**
 * Gets assoc array (fetched from db) and transforms it to Zend_Navigation object
 * @param assoc array $array
 * @param string $idField
 * @param string $parentField
 * @return \Zend_Navigation 
 */
private function getNavigationFromAssocArray($array, $idField = 'menu_item_id', $parentField = 'parent_menu_item_id') {
    $navigation = new Zend_Navigation();

    foreach ($array as $key => $item) {
        $item['uri'] = $item['url'];

        if(empty($parentField) ) {    
            $navigation->addPage(
                Zend_Navigation_Page::factory( $item )
            );
        } else {
            $page = $navigation->findBy($idField, $parentField );
            $page->addPage(Zend_Navigation_Page::factory( $item ));
        }
    }
    return $navigation;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文