1

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 Model's initalize()?)

Thanks!

Harry
  • 863
  • 3
  • 10
  • 26

2 Answers2

2

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.

death_au
  • 1,282
  • 2
  • 20
  • 41
0

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

Bot
  • 11,868
  • 11
  • 75
  • 131
  • That looks like it's more standard object relations - I'm needing to build a custom relationship without using the foreign key that Cake requires. I'd like to use a Join Table for a hasOne relationship. – Harry Sep 22 '10 at 17:06