I'm sure there's a simple solution to this, but I can't fathom what's going on. I have an entity called a Payment
and two other entities called User
and Household
.
A User
has many Payment
s and and a Household
has many Payment
s. This is all mapped as unidirectional manyToOne relationships of the Payment
using YAML, like this (hopefully this is in line with the guidance at 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
Database wise, I have three tables, users
, payments
and households
. The payments
table has (in addition to it's primary key) two columns called user_id
and household_id
, which correspond the to primary keys of the other two tables.
The problem is that when I try to persist a new Payment
I get an error back from mySql telling me that the user_id
column of payments
cannot be null. Some digging has established that doctrine is supplying null
for both user_id
and household_id
in the INSERT statement. The object graph is definitely correct.
In addition, I get the following warnings:
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
I've established that the payment_id
in question is coming from the referencedColumnName
s in my mapping, and that the index is undefined because the array doctrine is searching contains the fields from the opposite side of the relationship, i.e. the Household
or the User
. But surely it shouldn't be? I think I've followed the guide, but I can't make it work.
I'm using Zend Framework, if it helps, but there's nothing special going on my code. I'm pretty sure it's a mapping problem.
Thanks
I've only worked with Doctrine + Symfony2, but I think your referencedColumnName should just be "id", not payment_id, because you need to explain the foreign key relationship.
payment.household_id
--references--> household.id
.
In Symfony2 there is an 'inversedBy' property for Doctrine, which is used to map the column in the other table that would represent the payment_id.
A snippet of code from a Symfony2 Mapping:
/**
* @ORM\ManyToOne(targetEntity="kurtfunai\WalkthroughBundle\Entity\Group", inversedBy="members")
* @ORM\JoinColumn(name="fk_group_id", referencedColumnName="id")
*/
protected $group;
In your case it would need altered to something like this:
_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>
(Replace with the appropriate column names)