I have the following HABTM relationship:
//Message.php
class Message extends AppModel {
public $hasAndBelongsToMany = array(
'User' => array(
'className' => 'User',
'joinTable' => 'messages_users',
'foreignKey' => 'message_id',
'associationForeignKey' => 'user_id',
'unique' => true));
}
//User.php
[...]
public $hasAndBelongsToMany = array(
'Message' => array(
'className' => 'Message',
'joinTable' => 'messages_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'message_id',
'unique' => true));
//[...]
}
In my database I have these tables: users
, messages
and messages_users
(with id
, user_id
and message_id
fields).
I'm saving my data with:
//MessagesController.php
public function new_message() {
$users = $this->Message->User->find('list', array(
'fields' => array(
'username')));
$this->set(compact('users'));
if ($this->request->is('post')) {
//debug($this->request->data);
if ($this->Message->saveAll($this->request->data, array(
'deep' => true))) {
$this->Session->setFlash('Message sent.');
$this->redirect(array('action' => 'index'));
}
}
}
And debug info shows:
array(
'Message' => array(
'users' => '1',
'subject' => 'Subject',
'text' => 'Text'
)
)
The data is saving without problems in the table messages
, but the join table messages_users
is empty. What am I doing wrong?
Thanks in advance!