I have a table Tasks_Users with id, task_id,user_id and sender_id. Problem is user_id and sender_id are both referring to facebook_id in Users table and I'm not sure how I can hook them together with two foreign keys. Is there a way we can get two foreign keys defined with HABTM relationship from one column?
UPDATE: Below is what I have currently set up in my Task model.
var $hasAndBelongsToMany = array(
'User' => array(
'className' => 'User',
'joinTable' => 'tasks_users',
'foreignKey' => 'task_id',
'associationForeignKey' => 'user_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
),
'Sender' => array(
'className' => 'User',
'joinTable' => 'tasks_users',
'foreignKey' => 'task_id',
'associationForeignKey' => 'sender_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
Below is for User model
var $hasAndBelongsToMany = array(
'Task' => array(
'className' => 'Task',
'joinTable' => 'tasks_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'task_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' =开发者_StackOverflow中文版> '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
Below is TasksUser model
var $belongsTo = array(
'Task' => array(
'className' => 'Task',
'foreignKey' => 'task_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Sender' => array(
'className' => 'User',
'foreignKey' => 'sender_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
I managed to get two multi-select boxes one for User_id and the other for Sender_id but the problem is I cannot save both at the same time for the same row...
The data arrays come as like this $this->data['User']['User'][] and $this->data['Sender']['Sender'][]
Theoretically it should save both but unfortunately it only saves Sender_id in the TasksUser table when you save using add action in scaffolding for Tasks controller and saves only User_id in the TasksUser table when you save using add action in scaffolding for Users controller.
The TasksUser tables are as below:
id task_id user_id sender_id
How can I make it so it saves both user_id and sender_id at the same row???
You have to put bellow code in your controller,
$this->TaskUser->bindModel(
array('belongsTo' => array(
'User1' => array(
'className' => 'User',
'foreignKey' => 'user_id'),
'User2' => array(
'className' => 'User',
'foreignKey' => 'sender_id'),
)
)
);
It should work normally, I mean without any other correction: If you have a table User which relates via HABTM to other tables, when you make a saveAll CakePhp should automatically update the Join table. For me it does work.
精彩评论