I have following table I am using for a messages function:
CREATE TABLE `messages` (
`id` int(11) NOT NULL PRIMARY AUTO_INCREMENT,
`to_user` int(11) NOT NULL,
`from_user` int(11) NOT NULL,
`message` text,
`seen` tinyint(1) NOT NULL DEFAULT '0',
`created` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I want now to create a new message the standard cakephp-way:
$message = $this->Messages->newEmptyEntity();
$message = $this->Messages->patchEntity($message, $this->request->getData());
$this->Messages->save($message);
This is my data that needs to be saved:
[
'to_user' => (int) 2,
'message' => 'test',
'from_user' => (int) 1,
'seen' => false
]
But it only saves the fields message
and seen
, even if all fields are accessible. I debugged the patched entity and got following result:
object(App\Model\Entity\Message) {
'message' => 'test',
'seen' => false,
'[new]' => true,
'[accessible]' => [
'to_user' => true,
'from_user' => true,
'message' => true,
'seen' => true,
'created' => true
],
'[dirty]' => [
'message' => true,
'seen' => true
],
'[original]' => [],
'[virtual]' => [],
'[hasErrors]' => false,
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Messages'
}
When I remove the belongsTo
relation, it works:
$this->belongsTo('ToUser', [
'foreignKey' => 'to_user',
'className' => 'Users',
'propertyName' => 'to_user'
]);
$this->belongsTo('FromUser', [
'foreignKey' => 'from_user',
'className' => 'Users',
'propertyName' => 'from_user'
]);
Now I don’t know how to debug or fix this issue. Any ideas?