我确信这有一个简单的解决方案,但我无法理解正在发生的事情.我有一个实体,称为Payment等两个实体叫User和Household.
A User有很多Payments而且Household有很多Payments.这全部映射为Payment使用YAML的单向manyToOne关系,如此(希望这符合http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/association上的指导)-mapping.html#many-to-one-unidirectional) -
Payment:
type: entity
table: payments
id:
_id:
type: integer
column: payment_id
generator:
strategy: AUTO
manyToOne:
_user:
targetEntity: Application_Model_User
joinColumn:
name: user_id
referencedColumnName: payment_id
_household:
targetEntity: Application_Model_Household
joinColumn:
name: household_id
referencedColumnName: payment_id
Run Code Online (Sandbox Code Playgroud)
数据库方面,我有三个表users,payments和households.该payments表具有(除了它的主键之外)两个名为user_id和的列household_id,它们对应于其他两个表的主键.
问题是,当我尝试坚持一个新的时,Payment我从mySql返回一个错误,告诉我该user_id列payments不能为null.一些挖掘已经确定了doctrine正在null为INSERT语句user_id和household_idINSERT语句提供.对象图肯定是正确的.
另外,我收到以下警告:
Notice: Undefined index: payment_id in [..]/library/Doctrine/ORM/Persisters/BasicEntityPersister.php on line 511
Notice: Undefined index: in [..]/library/Doctrine/ORM/Persisters/BasicEntityPersister.php on line 511
Notice: Undefined index: payment_id in [..]/library/Doctrine/ORM/Persisters/BasicEntityPersister.php on line 511
Notice: Undefined index: in [..]/library/Doctrine/ORM/Persisters/BasicEntityPersister.php on line 511
Run Code Online (Sandbox Code Playgroud)
我已经确定payment_id问题是来自referencedColumnName我的映射中的s,并且索引是未定义的,因为数组原则正在搜索包含来自关系另一侧的字段,即Household或者User.但肯定不应该这样吗?我想我已经按照指南,但我不能让它工作.
我正在使用Zend Framework,如果它有帮助,但我的代码没有什么特别之处.我很确定这是一个映射问题.
谢谢
我只使用过Doctrine + Symfony2,但我认为你的referencedColumnName应该只是"id",而不是payment_id,因为你需要解释外键关系.
payment.household_id--references - > household.id.
在Symfony2中,Doctrine有一个'inversedBy'属性,用于映射另一个表中代表payment_id的列.
来自Symfony2映射的一段代码:
/**
* @ORM\ManyToOne(targetEntity="kurtfunai\WalkthroughBundle\Entity\Group", inversedBy="members")
* @ORM\JoinColumn(name="fk_group_id", referencedColumnName="id")
*/
protected $group;
Run Code Online (Sandbox Code Playgroud)
在你的情况下,它需要改为这样的东西:
_user:
targetEntity: Application_Model_User
joinColumn:
name: user_id
referencedColumnName: <USERS_TABLE_PRIMARY_KEY_COLUMN_NAME>
_household:
targetEntity: Application_Model_Household
joinColumn:
name: household_id
referencedColumnName: <HOUSEHOLDS_TABLE_PRIMARY_KEY_COLUMN_NAME>
Run Code Online (Sandbox Code Playgroud)
(替换为相应的列名称)
| 归档时间: |
|
| 查看次数: |
10822 次 |
| 最近记录: |