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