【问题标题】:Programmatically Create Menu Item in Joomla在 Joomla 中以编程方式创建菜单项
【发布时间】:2012-09-20 23:48:10
【问题描述】:

我在 joomla 2.5 中创建了一个组件,用于创建新文章并将该文章添加到菜单项。

创建文章工作正常,但我在创建菜单项时遇到了一些问题。

我有以下代码:

                //add the article to a menu item
                $menuTable = JTable::getInstance('Menu', 'JTable', array());

                    $menuData = array(
                    'menutype' => 'client-pages',
                    'title' => $data[name],
                    'type' => 'component',
                    'component_id' => 22,                  
                    'link' => 'index.php?option=com_content&view=article&id='.$resultID,
                    'language' => '*',
                    'published' => 1,
                    'parent_id' => '1',
                    'level' => 1,
                );

                // Bind data
                if (!$menuTable->bind($menuData))
                {
                    $this->setError($menuTable->getError());
                    return false;
                }

                // Check the data.
                if (!$menuTable->check())
                {
                    $this->setError($menuTable->getError());
                    return false;
                }

                // Store the data.
                if (!$menuTable->store())
                {
                    $this->setError($menuTable->getError());
                    return false;
                }

错误似乎与设置 parent_id 和级别有关。在调试 library/joomla/database/tablenested.php 时将 parent_id 和 level 设置为 0。这导致我的管理员页面出现以下错误:

警告:str_repeat() [function.str-repeat]:/Applications/MAMP/htdocs/joomla_2_5/administrator/components/com_menus/views/items/tmpl/default 中的第二个参数必须大于或等于 0 .php 在第 129 行

【问题讨论】:

  • 看起来错误信息很明确你做错了什么。改变它有什么问题?
  • 错误信息指向一个 joomla 核心文件,所以我不想在那里乱搞。我想我需要解决为什么 joomla core 不断将我的 parent_id 和 level 重置为 0 的问题

标签: php joomla content-management-system joomla2.5


【解决方案1】:

尝试使用JTableNested::setLocation($referenceId, $position = 'after'):

$table->setLocation($parent_id, 'last-child');

我也认为你需要重建路径:

// Rebuild the tree path.
if (!$table->rebuildPath($table->id)) {
    $this->setError($table->getError());
    return false;
}

如果它仍然不起作用,请尝试找出 MenusModelItem::save 做了哪些你不做的事情。

【讨论】:

    【解决方案2】:
    $table->setLocation($parent_id, 'last-child');
    

    是确保为新菜单项正确创建左/右值所需的全部内容。无需重建路径,因为现在由 JTableMenu 的 store 方法处理。

    另外,可以使用便捷方法“save”来绑定、查看和存储菜单项:

    $menuItem = array(
                'menutype' => 'client-pages',
                'title' => $data[name],
                'type' => 'component',
                'component_id' => 22,                  
                'link' => 'index.php?option=com_content&view=article&id='.$resultID,
                'language' => '*',
                'published' => 1,
                'parent_id' => $parent_id,
                'level' => 1,
            );
    
    $menuTable = JTable::getInstance('Menu', 'JTable', array());
    
    $menuTable->setLocation($parent_id, 'last-child');
    
    if (!$menuTable->save($menuItem)) {
        throw new Exception($menuTable->getError());
        return false;
    }
    

    【讨论】:

      【解决方案3】:

      不知何故$menutable 不会更新数据库表中的parent_idlevel,因此您必须通过joomla 查询手动更新这两个字段。

      $menuTable = JTable::getInstance('Menu', 'JTable', array());
      
              $menuData = array(
                  'menutype' => 'client-pages',
                  'title' => $data[name],
                  'type' => 'component',
                  'component_id' => 22,                  
                  'link' => 'index.php?option=com_content&view=article&id='.$resultID,
                  'language' => '*',
                  'published' => 1,
                  'parent_id' => '1',
                  'level' => 1,
              );
      
              // Bind data
              if (!$menuTable->bind($menuData))
              {
                  $this->setError($menuTable->getError());
                  return false;
              }
      
              // Check the data.
              if (!$menuTable->check())
              {
                  $this->setError($menuTable->getError());
                  return false;
              }
      
              // Store the data.
              if (!$menuTable->store())
              {
                  $this->setError($menuTable->getError());
                  return false;
              }
      
              $db   = $this->getDbo();
              $qry = "UPDATE `#__menu` SET `parent_id` = 1 , `level` = 1 WHERE `id` = ".$menuTable->id;
              $db->setQuery($qry);
              $db->query();
      

      【讨论】:

      • 您应该始终使用 JTableNested API 为您进行更新,这样做不会正确设置嵌套设置值。此外,您没有执行查询,您应该使用 API 进行查询。
      • @mickmackusa 我真的不是最新的 joomla 所以我可能不会那么有帮助。
      【解决方案4】:

      这段代码对我有用

       JTable::addIncludePath(JPATH_ADMINISTRATOR.'/components/com_menus/tables/');
      
                      $menuTable =& JTable::getInstance('menu', 'menusTable');
      
                      $menuData = array(
                              'menutype' => 'client-pages',
                              'title' => 'mytrialmenu',
                              'type' => 'component',
                              'component_id' => 22,
                              'link' => 'index.php?option=index.php?                    option='com_content&view=article&id='.$resultID,
                              'language' => '*',
                              'published' => 1,
                              'parent_id' => 'choose some parent',
                              'level' => 1,
                      );
                      // Bind data
                      if (!$row->bind($menuData))
                      {
                          $this->setError($menuTable->getError());
                          return false;
                      }
      
                      // Check the data.
                      if (!$row->check())
                      {
                          $this->setError($menuTable->getError());
                          return false;
                      }
      
                      // Store the data.
                      if (!$row->store())
                      {
                          $this->setError($menuTable->getError());
                          return false;
                      }
      

      我认为原因是 menusTable 扩展了 JnestedTable,这是在菜单表中操作 lft 和 rgt 字段所必需的

      【讨论】:

        猜你喜欢
        • 2011-03-26
        • 1970-01-01
        • 2011-11-11
        • 1970-01-01
        • 2011-09-21
        • 2014-07-11
        • 1970-01-01
        • 2012-10-26
        • 1970-01-01
        相关资源
        最近更新 更多