I would like to use hasMany through relationship to connect two users through a model invitations
I currently have the following controller:
class User extends AppModel {
var $name = 'User';
var $displayField = 'username';
var $hasMany = array(
'Invitation' => array(
'className' => 'Invitation',
'foreignKey' => 'sender_user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Invitation' => array(
'className' => 'Invitation',
'foreignKey' => 'receiver_user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
)
}
However, when i get error:
Error: Database table sender_users for model SenderUser was not found.
Which is a bit annoying, because I already thought I specified that I want to use Invitation.
What is the issue, what code do I change?
First: you can’t create the same association twice. Even from a PHP syntax perspective that’s not possible. You’ll have to give each association a unique name:
Second, the problem is probably that you don’t have an Invitation model. Cake creates a model on-the-fly according to naming conventions. Since you have a column
sender_user_id, according to naming conventions you should therefore also have asender_userstable. Create the Invitation model and set it up without that association, then Cake shouldn’t be looking for it.