您好,欢迎访问一九零五行业门户网

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

在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:77stack 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:77stack 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/
其它类似信息

推荐信息