我有一些麻烦与两个协会一个型号.. 小PIC使我的问题,希望更清楚一点..CakePHP的:一个型号多种关系
http://imgur.com/hxL06u2(对不起,我不能在这里发布图片)
所以我有用户谁可以写/自己的文章..一个用户可以写多篇文章和一篇文章只能由一个用户拥有(有很多)..
现在的用户可以喜欢从其他用户的文章。 ..这是一个拥有和属于很多关系..我的问题是,当我创建一个文件sql转储:
Cannot add or update a child row: a foreign key constraint fails (`xxx`.`articles_users`,...
所以蛋糕使用错误的关系(HABTM而不是有很多),因此错误的表(article_users而不是物品)
我可以写我自己保存的查询与右表(I”我试过这个和它的作品),但我认为这不是最好的解决方案..
确实有人知道更好的方法?谢谢!
编辑:
文章型号
Ownes:
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
喜欢:
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' => ''
)
);
用户模型
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' => ''
));
为您的用户和文章型号分享相关型号代码($ hasMany,$ belongsTo,HABTM) – AgRizzo 2014-09-25 20:05:11