Php 在Joomla中以编程方式创建菜单项
我在Joomla2.5中创建了一个组件,它创建了一篇新文章,并将该文章添加到菜单项中 创建文章工作正常,但是我在创建菜单项时遇到了一些问题 我有以下代码:Php 在Joomla中以编程方式创建菜单项,php,joomla,content-management-system,joomla2.5,Php,Joomla,Content Management System,Joomla2.5,我在Joomla2.5中创建了一个组件,它创建了一篇新文章,并将该文章添加到菜单项中 创建文章工作正常,但是我在创建菜单项时遇到了一些问题 我有以下代码: //add the article to a menu item $menuTable = JTable::getInstance('Menu', 'JTable', array()); $menuData = array(
//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;
}
错误似乎与设置父项id和级别有关。在调试库/joomla/database/tablenested.php时,将父\u id和级别设置为0。这导致“我的管理员”页面出现以下错误:
警告:str_repeat()[function.str repeat]:第129行的/Applications/MAMP/htdocs/joomla_2_5/administrator/components/com_menu/views/items/tmpl/default.php中的第二个参数必须大于或等于0
尝试使用
JTableNested::setLocation($referenceId,$position='after'):
$table->setLocation($parent_id,'last child');
我还认为您需要重建路径:
//重建树路径。
如果(!$table->重建路径($table->id)){
$this->setError($table->getError());
返回false;
}
如果它仍然不起作用,试着找出哪些是你不起作用的。这段代码对我很有用
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字段所必需的不知何故,
$menutable
不会更新数据库表中的父id
和级别
,因此您必须通过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();
是确保为新菜单项正确创建左/右值所需的全部。无需重建路径,因为这现在由JTableMenu的store方法处理
此外,方便的方法“保存”可用于绑定、检查和存储菜单项:
$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;
}
看起来,错误消息非常明确地说明了您做错了什么。更改它有什么问题?错误消息指向一个joomla核心文件,所以我不想在那里乱动。我认为我需要解决为什么joomla core一直将我的父id和级别重置为0的问题。您应该始终使用JTableNested API来为您进行更新,这样做将无法正确设置嵌套集值。另外,您没有执行查询,您应该使用API进行查询。@mickmackusa我在joomla上不是最新的,所以我可能不会有什么帮助。
$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;
}