Php Doctrine2在使用一对一和多对一关系时严格尝试插入t NULL
Places.ymlPhp 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
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
WAREBUNDLE\u前缀将是bundles名称的第一部分。这将生成所有正确的getter和setter。不要忘了更新数据库架构:
php app/console doctrine:schema:update --force
正如一些最后的建议,我建议(除非您打算这样做)您将实体名称更改为偏移
和地点
,因为每个对象应仅包含一个地点或偏移