开发者

CakePHP - Custom hasOne Object Association - Using a Join Table instead of in-table foreign keys?

开发者 https://www.devze.com 2023-01-17 08:17 出处:网络
In CakePHP, I\'m trying to build a $hasOne relationship, but I can\'t add a foreign key to the tables (for legacy reasons).Is there any way I can create a join table (object1_object2) and link them to

In CakePHP, I'm trying to build a $hasOne relationship, but I can't add a foreign key to the tables (for legacy reasons). Is there any way I can create a join table (object1_object2) and link them together like this? I want to leverage Cake's powerful object relationship functionality, so if there's a way I can make this custom association permanent throughout the model, without always building a custom $options array for each find(), that'd be great. (Maybe a bindModel in the 开发者_Go百科Model's initalize()?)

Thanks!


If you want to use a join table, I believe you will have to use a hasAndBelongsToMany (HABTM) relationship, even though you will never have 'many'. The data will obviously behave differently to a standard hasOne relationship, but at least the relationship will exist.

There might be a way around it using the 'conditions' field of the hasOne relationship, but I suspect that you still have to specify a foreign key (or it will default to 'object2_id' anyway) and it will probably still attempt to do a join based on that foreign key.


I believe this is what you are looking for. http://www.packtpub.com/article/working-with-simple-associations-using-cakephp here is another one http://debuggable.com/posts/modeling-relationships-in-cakephp-faking-rails-throughassociation:480f4dd6-b990-485e-abe4-4baccbdd56cb

0

精彩评论

暂无评论...
验证码 换一张
取 消