I have some trouble with two associations to one Model.. A little pic makes my problem hopefully a bit clearer..
http://imgur.com/hxL06u2 (sorry i cant post pictures here)
so i have users who can write/own articles.. one user can write multiple articles and one article can only be owned by one user (has many)..
now the users can like articles from other users... this is an has and belongs to many relationship .. my problem is, when i create an article sql dumps:
Cannot add or update a child row: a foreign key constraint fails (`xxx`.`articles_users`,...
so cake uses the wrong relationship (habtm instead of the has many) and therefore the wrong table (article_users instead of articles)
i could write my own save query with the right tables (i've tried that and its works) but i think thats not the best solution..
does someone knows an better approach?? thanks!
Edit:
Article Model
Ownes:
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
Likes:
public $hasAndBelongsToMany = array('User' => array(
'className' => 'User',
'joinTable' => 'articles_users',
'foreignKey' => 'article_id',
'associationForeignKey' => 'user_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
User Model
public $hasMany = array(
'Article' => array(
'className' => 'Article',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
public $hasAndBelongsToMany = array(
'Article' => array(
'className' => 'Article',
'joinTable' => 'articles_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'article_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
));
In your User model, your relationship names/ aliases are the same: Article. Make them different. Per the documentation: However, aliases for each model must be unique across the app.