2010-11-29 95 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()); 

可能是由于您使用的输入而不是选择引起的问题。