首頁 >後端開發 >php教程 >function使用themeConfig错误提示-主键冲突

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

WBOY
WBOY原創
2016-06-06 20:17:351475瀏覽

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

<code>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);
    }
</code>

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

错误代码:

<code>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</code>

回复内容:

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

<code>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);
    }
</code>

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

错误代码:

<code>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</code>

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/

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn