2010-11-29 96 views
0

我有一個窗體的輸入是optionnal,但我不能有效的表單,如果用戶不填充它,因爲它是一個外鍵和Doctrine顯示我一個錯誤。如何將外鍵設置爲空? (symfony)

SQLSTATE[HY000]: General error: 1452 Cannot add or update a child row: a foreign key constraint fails (`logements`.`bail`, CONSTRAINT `bail_ibfk_3` FOREIGN KEY (`locataire2`) REFERENCES `locataire` (`nud`) ON DELETE CASCADE ON UPDATE CASCADE) 

我已經在phpMyAdmin嘗試過同樣的要求和工作正確:我可以設置外鍵爲空。

那麼,我怎樣才能將外鍵設置爲空和有效的形式沒有學說錯誤?

編輯

Bail: 
    connection: doctrine 
    tableName: bail 
    columns: 
    id: 
     type: integer(2) 
     fixed: false 
     unsigned: true 
     primary: true 
     autoincrement: true 
    locataire1: 
     type: string(20) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    locataire2: 
     type: string(20) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    logement: 
     type: integer(2) 
     fixed: false 
     unsigned: true 
     primary: false 
     notnull: false 
     autoincrement: false 
    datedeb: 
     type: date(25) 
     fixed: false 
     unsigned: false 
     primary: false 
     notnull: false 
     autoincrement: false 
    datefin: 
     type: date(25) 
     fixed: false 
     unsigned: false 
     primary: false 
     default: '0000-00-00' 
     notnull: false 
     autoincrement: false 
    colloc: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: false 
     default: '0' 
     notnull: false 
     autoincrement: false 
    bailglissant: 
     type: string(12) 
     fixed: false 
     unsigned: false 
     primary: false 
     default: 'Non spécifié' 
     notnull: false 
     autoincrement: false 
    relations: 
    Locataire: 
     local: locataire1 
     foreign: nud 
     type: one 
    Logement: 
     local: logement 
     foreign: id 
     type: one 
    Locataire_3: 
     class: Locataire 
     local: locataire2 
     foreign: nud 
     type: one 
+1

請問您可以粘貼您的架構定義和表單嗎?如果你的領域真的被定義爲可選,那麼你不應該得到這樣的消息。錯誤來自數據庫,所以它看起來像教條試圖把'0'放在那裏。 – 2010-11-29 20:49:57

+0

我的窗體小部件是「locataire2」 – Elorfin 2010-11-30 11:31:58

回答

0

我已通過使用方法processValues()(通過save()調用)將屬性設置爲NULL來解決問題。

1

我不知道這是否是這裏的情況,但一到一個關係,你需要的,如果你想將它設置爲null使用Doctrine_Null對象:

$this->setLocataire2(new Doctrine_Null()); 

可能是由於您使用的輸入而不是選擇引起的問題。