Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.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
Php Doctrine2在使用一对一和多对一关系时严格尝试插入t NULL_Php_Doctrine Orm - Fatal编程技术网

Php Doctrine2在使用一对一和多对一关系时严格尝试插入t NULL

Php Doctrine2在使用一对一和多对一关系时严格尝试插入t NULL,php,doctrine-orm,Php,Doctrine Orm,Places.yml manyToOne: excursions: targetEntity: Excursions inversedBy: places inverse: true cascade: ["ALL"] nullable: false joinColumn: name: idx referencedColumnName: place_id oneToOne: places: targetEnt

Places.yml

manyToOne:
  excursions:
    targetEntity: Excursions
    inversedBy: places
    inverse: true
    cascade: ["ALL"]
    nullable: false
    joinColumn:
      name: idx
      referencedColumnName: place_id
oneToOne:
  places:
    targetEntity: Places
    mappedBy: excursions
manyToOne:
  --remove--
oneToOne:
  places:
    targetEntity: Places
    joinColumn:
        name: place_id
        referencedColumnName: idx
游览。yml

manyToOne:
  excursions:
    targetEntity: Excursions
    inversedBy: places
    inverse: true
    cascade: ["ALL"]
    nullable: false
    joinColumn:
      name: idx
      referencedColumnName: place_id
oneToOne:
  places:
    targetEntity: Places
    mappedBy: excursions
manyToOne:
  --remove--
oneToOne:
  places:
    targetEntity: Places
    joinColumn:
        name: place_id
        referencedColumnName: idx
游览只有一个地点id。因此
$Excursion->getPlaces()
只返回一行

现在,当我尝试向
Places
表插入一个值时,Doctrine2说
非空冲突:7错误:“idx”列中的空值违反了非空约束

我不知道为什么会这样。如果我删除了
manyToOne
,则插入工作。但是这次
getPlaces()
不起作用

更新

问题是,我对数据库使用mappedBy。我只应该这样做:

Places.yml

manyToOne:
  excursions:
    targetEntity: Excursions
    inversedBy: places
    inverse: true
    cascade: ["ALL"]
    nullable: false
    joinColumn:
      name: idx
      referencedColumnName: place_id
oneToOne:
  places:
    targetEntity: Places
    mappedBy: excursions
manyToOne:
  --remove--
oneToOne:
  places:
    targetEntity: Places
    joinColumn:
        name: place_id
        referencedColumnName: idx
游览。yml

manyToOne:
  excursions:
    targetEntity: Excursions
    inversedBy: places
    inverse: true
    cascade: ["ALL"]
    nullable: false
    joinColumn:
      name: idx
      referencedColumnName: place_id
oneToOne:
  places:
    targetEntity: Places
    mappedBy: excursions
manyToOne:
  --remove--
oneToOne:
  places:
    targetEntity: Places
    joinColumn:
        name: place_id
        referencedColumnName: idx

这就解决了问题。但我是通过随机尝试/玩发现这一点的。所以,我不知道为什么:)任何人都知道,请解释我。

首先,映射是不正确的。您不应该在
Places.yml
中有
ManyToOne
,而在
tourings.yml
中,将反转侧映射为
OneToOne
。这很可能就是您遇到这些错误的原因。
您还希望一个
游览
有一个
地点
对象链接到它,但是
地点
可以有更多的
游览
对象,如果我正确理解您的问题的话。因此,映射应如下所示:

# Places.yml
OneToMany:
    excursions:
        tagertEntity: Excursions
        mappedBy: places

请注意:

  • manytone
    关系应该位于只有一个其他关系的实体内部
  • OneToMany
    关系应该位于具有另一个的多个对象的实体内部
  • joinColumn:name:
    应具有要在数据库中创建的列的名称
  • joinColumn:referencedColumnName:
    应该具有目标实体中的字段,您要使用该字段映射与之的关系(我建议您使用id,因为它是唯一的)
  • 创建此映射后,请运行以下命令:

    php app/console doctrine:generate:entities BUNDLE_PREFIX
    
    WARE
    BUNDLE\u前缀将是bundles名称的第一部分。这将生成所有正确的getter和setter。不要忘了更新数据库架构:

    php app/console doctrine:schema:update --force
    
    正如一些最后的建议,我建议(除非您打算这样做)您将实体名称更改为
    偏移
    地点
    ,因为每个对象应仅包含一个地点或偏移