手动插入 joomla 数据库时,密钥“idx_client_id_parent_id_alias_language"的重复条目

2022-01-06 00:00:00 php mysql joomla

Joomla 3

我正在尝试将一些记录手动插入 #__menu.由于对于大多数字段,我只能使用其他记录具有的值,因此我试图从现有记录中获取 stdObject 并将其插入回表中.在此之前,我需要处理可能的密钥重复项.我阅读了表结构,除了id,我发现lftrgt 字段似乎必须是唯一的.所以以下是我的尝试:

I am trying to manually insert some records into #__menu. Since for most fields I can just use the value that other records have, I am trying to get a stdObject from an existing record and insert it back to the table. Before that, I need to take care of the possible key duplicate. I read the table structure, and besides id, I find field lft and rgt seem to have to be unique. So the following is what I try:

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$sql = 'SELECT * FROM `#__menu` WHERE id = 203';
$db->setQuery($sql);
$example = $db->loadObjectList()[0];
unset($example->id);                     
unset($example->lft);
unset($example->rgt);

$db->insertObject('#__menu',$example);

我得到的错误信息是

密钥重复条目0-1-001-*"'idx_client_id_parent_id_alias_language'

Duplicate entry '0-1-001-*' for key 'idx_client_id_parent_id_alias_language'

SQL=INSERT INTO #__menu(menutype,title,alias,note,path,link,type,已发布,parent_id,level,component_id,checked_out,checked_out_time,browserNav,access,img,template_style_id,params,home,language,client_id)VALUES ('hidden','test','001','','001','index.php?option=com_k2&view=item&layout=item&id=1','component','1','1','1','10125','0','0000-00-0000:00:00','0','1','','0','{"menu-anchor_title":"","menu-anchor_css":"","menu_image":"","menu_text":1,"menu_show":1,"page_title":"","show_page_heading":"","page_heading":"","pageclass_sfx":"","menu-meta_description":"","menu-meta_keywords":"","robots":"","secure":0}','0','*','0')

SQL=INSERT INTO #__menu (menutype,title,alias,note,path,link,type,published,parent_id,level,component_id,checked_out,checked_out_time,browserNav,access,img,template_style_id,params,home,language,client_id) VALUES ('hidden','test','001','','001','index.php?option=com_k2&view=item&layout=item&id=1','component','1','1','1','10125','0','0000-00-00 00:00:00','0','1',' ','0','{"menu-anchor_title":"","menu-anchor_css":"","menu_image":"","menu_text":1,"menu_show":1,"page_title":"","show_page_heading":"","page_heading":"","pageclass_sfx":"","menu-meta_description":"","menu-meta_keywords":"","robots":"","secure":0}','0','*','0')

我不明白为什么有一个名为idx_client_id_parent_id_alias_language"的键,它肯定不是表的字段之一.谷歌搜索会返回一些结果,但在我看来它们都与我的问题无关.

I don't understand why there is a key called 'idx_client_id_parent_id_alias_language', it sure is not one of the table's fields. Googling it returns some result, but it seems to me none of them is related to my problem.

推荐答案

我想我会写一个答案,因为 shenkwen 评论说他用谷歌搜索答案并没有发现任何帮助.

Thought that I would write up an answer as shenkwen commented that he googled for an answer and found nothing helpful.

OP 看到的错误是由他手动插入一行引起的,但菜单表上有一个多列键.关键是client_id、parent_id、别名和语言的组合.

The error that OP is seeing as caused by him manually inserting a row but there is a multiple column key on the menu table. The key is a combination of client_id, parent_id, alias and language.

不能有两行共享相同的值.正如 OP 的评论中所指出的,他正在复制别名.

You can not have two rows which share those same values. As noted in the comments from OP he was duplicating the alias.

相关文章