function使用themeConfig錯誤提示-主鍵衝突

來源:互聯網
上載者:User
在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/

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.