3

Cake has its conventions for automating some functionality interaction between it's models and the tables they reference. Foreign keys should be called people_id, if they reference a table called people. How do I handle a case where a row holds two people, and needs foreign keys for both people? It's obvious that I can't duplicate the column names, having two people_id columns.

Consider a competition where you have two competitors. Each competitor is represented by a row in the competitor's table. The competitions table needs to refer to both of those competitors. How can I do this wouldn't breaking the automatic functionality that Cake offers?

Adam
  • 913
  • 1
  • 9
  • 26
  • i don't think you can have 2 columns of the same name - `people_id` - why do you think obvious you can? – Aprillion Jul 08 '12 at 17:31
  • [By the looks of it](http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#belongsto) , it is somewhat impossible to do this with the ORM. ActiveRecord design pattern is way too limited for this. Hell .. IMHO, its not even good from making simple `JOIN` statement, not to mention more complicated cases. Basically, you will have to manually write the queries and retrieve the data. – tereško Jul 08 '12 at 17:37
  • I found this link that describes how this process is done:http://book.cakephp.org/1.2/view/851/Multiple-relations-to-the-same-model – Adam Jul 08 '12 at 17:43
  • Found this answer: http://stackoverflow.com/questions/11844998/multiple-relations-to-the-same-model-cakephp – daniel.auener Jul 28 '13 at 07:21

1 Answers1

2

I found a link that describes how to do this perfectly.

http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm

bicycle
  • 8,315
  • 9
  • 52
  • 72
Adam
  • 913
  • 1
  • 9
  • 26