Hey there
I have a simple relation issue. Using a contacts
table and users
table.
Now on the contacts
table (beside a ton of fields), I have two fields, that should be related to the users
table:
partner (integer)
owner (integer)
As usual I created the relations in the table (on both sides ofc):
// src/Model/Table/ContactsTable.php
$this->belongsTo('Partner', [
'className' => 'Users',
'foreignKey' => 'partner',
'propertyName' => 'partner'
]);
$this->belongsTo('Owner', [
'className' => 'Users',
'foreignKey' => 'owner',
'propertyName' => 'owner'
]);
and the Users table:
// src/Model/Table/UsersTable.php
$this->hasMany('Partner', [
'className' => 'Contacts',
'foreignKey' => 'partner',
'propertyName' => 'partner'
]);
$this->hasMany('Owner', [
'className' => 'Contacts',
'foreignKey' => 'owner',
'propertyName' => 'owner'
]);
Already existing entries do output the related fields (of course, if added contain
in the controller). But I have an issue with patching (or say updating) or creating a new entity.
Before update:
'contact_partner' => (int) 2
'owner' => (int) 1
After $contact = $this->Contacts->patchEntity($contact, $this->request->getData());
// debug($this->request->getData())
'contact_partner' => '2',
'owner' => '1',
// debug($contact) -- after 'patchEntity()'
'contact_partner' => null
'owner' => null
What am I missing here? The relations work (on both sides, tested in UsersController
with contain, and working on the Contacts
side. But updating/creating results in null
.
Thanks in advance.
Oh. PS: I am using Cake 4.0.4