欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

function使用themeConfig错误提示-主键冲突

程序员文章站 2022-05-25 10:25:22
...
在function.php使用themeConfig函数打算做一个功能可以自定义输出内容
function themeConfig($form) {
       $twitter_url = new Typecho_Widget_Helper_Form_Element_Text('twitter_url', NULL, _t(''), _t('Your Twitter URL'), NULL);
    $form->addInput($twitter_url);
    }

出现错误提示"Database Query Error" (数据库连接是正常的,文章什么的都可以输出)

错误代码:

Duplicate entry 'theme:typecho-theme-MaterialDesi-0' for key 'PRIMARY'

exception 'Typecho_Db_Query_Exception' with message 'Duplicate entry 'theme:typecho-theme-MaterialDesi-0' for key 'PRIMARY'' in /Applications/MAMP/htdocs/viosey/blog/var/Typecho/Db/Adapter/Mysql.php:77
Stack trace:
#0 /Applications/MAMP/htdocs/blog/var/Typecho/Db.php(319): Typecho_Db_Adapter_Mysql->query(Object(Typecho_Db_Query), Resource id #29, 2, 'INSERT')
#1 /Applications/MAMP/htdocs/blog/var/Widget/Abstract/Options.php(56): Typecho_Db->query(Object(Typecho_Db_Query))
#2 /Applications/MAMP/htdocs/blog/var/Widget/Themes/Edit.php(130): Widget_Abstract_Options->insert(Array)
#3 /Applications/MAMP/htdocs/blog/var/Widget/Themes/Edit.php(176): Widget_Themes_Edit->config('typecho-theme-M...')
#4 /Applications/MAMP/htdocs/blog/var/Widget/Do.php(81): Widget_Themes_Edit->action()
#5 /Applications/MAMP/htdocs/blog/var/Typecho/Widget.php(222): Widget_Do->execute()
#6 /Applications/MAMP/htdocs/blog/var/Typecho/Router.php(135): Typecho_Widget::widget('Widget_Do', NULL, Array)
#7 /Applications/MAMP/htdocs/blog/index.php(23): Typecho_Router::dispatch

回复内容:

在function.php使用themeConfig函数打算做一个功能可以自定义输出内容

function themeConfig($form) {
       $twitter_url = new Typecho_Widget_Helper_Form_Element_Text('twitter_url', NULL, _t(''), _t('Your Twitter URL'), NULL);
    $form->addInput($twitter_url);
    }

出现错误提示"Database Query Error" (数据库连接是正常的,文章什么的都可以输出)

错误代码:

Duplicate entry 'theme:typecho-theme-MaterialDesi-0' for key 'PRIMARY'

exception 'Typecho_Db_Query_Exception' with message 'Duplicate entry 'theme:typecho-theme-MaterialDesi-0' for key 'PRIMARY'' in /Applications/MAMP/htdocs/viosey/blog/var/Typecho/Db/Adapter/Mysql.php:77
Stack trace:
#0 /Applications/MAMP/htdocs/blog/var/Typecho/Db.php(319): Typecho_Db_Adapter_Mysql->query(Object(Typecho_Db_Query), Resource id #29, 2, 'INSERT')
#1 /Applications/MAMP/htdocs/blog/var/Widget/Abstract/Options.php(56): Typecho_Db->query(Object(Typecho_Db_Query))
#2 /Applications/MAMP/htdocs/blog/var/Widget/Themes/Edit.php(130): Widget_Abstract_Options->insert(Array)
#3 /Applications/MAMP/htdocs/blog/var/Widget/Themes/Edit.php(176): Widget_Themes_Edit->config('typecho-theme-M...')
#4 /Applications/MAMP/htdocs/blog/var/Widget/Do.php(81): Widget_Themes_Edit->action()
#5 /Applications/MAMP/htdocs/blog/var/Typecho/Widget.php(222): Widget_Do->execute()
#6 /Applications/MAMP/htdocs/blog/var/Typecho/Router.php(135): Typecho_Widget::widget('Widget_Do', NULL, Array)
#7 /Applications/MAMP/htdocs/blog/index.php(23): Typecho_Router::dispatch

new Typecho_Widget_Helper_Form_Element_Text() 这个东西不能在functions里面调用,或者调用之前需要判断一下是否已经存在Text组件。
原因:
1).functions.php是每次请求页面的时候都会执行,而不是在安装模板的时候执行
2).Typecho_Widget_Helper_Form_Element_Text(...)会在数据库中创建一个主键为twitter_url的记录
所以结合1)和2),就会出现主键冲突的问题。
详情可以参考这里:http://www.typechodev.com/index.php/archives/32/

相关标签: typecho php mysql