Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms 如何将外键设置为null?(symfony)_Forms_Symfony1_Doctrine_Foreign Keys - Fatal编程技术网

Forms 如何将外键设置为null?(symfony)

Forms 如何将外键设置为null?(symfony),forms,symfony1,doctrine,foreign-keys,Forms,Symfony1,Doctrine,Foreign Keys,我有一个带有可选输入的表单,但如果用户不填写表单,我无法使表单有效,因为它是外键,并且条令显示了一个错误 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`

我有一个带有可选输入的表单,但如果用户不填写表单,我无法使表单有效,因为它是外键,并且条令显示了一个错误

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中尝试了相同的请求,它工作正常:我可以将外键设置为null

那么,如何将外键设置为null并使表单有效,而不会出现错误

编辑

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

我不确定这里是否是这种情况,但对于一对一关系,如果要将其设置为Null,则需要使用doctor_Null object:

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

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

我已通过使用processValues()方法(通过save()调用)将属性设置为NULL来解决该问题。

您是否可以粘贴架构定义和表单?如果您的字段真的被定义为可选字段,那么您不应该收到这样的消息。错误来自数据库,因此看起来doctrine试图将“0”放在那里。我表单的小部件是“locataire2”。它不工作,但感谢我不知道的这个对象