Tutorial :Cakephp Multiple Relations to the Same Model


I'm working on a real estate application, where one Home can be listed by either one or two Realtors. As such, I'm trying to follow the example given in section of the Cake Cookbook. Here's what's going on in my Realtors model:

class Realtor extends AppModel {      var $primaryKey = $num;      var $name = 'Realtor';      var $hasMany = array('Homes' =>           array('className' => 'Home',                'foreignKey' => 'realtor_num',          ...),                           'CoListedHomes' =>          array('className' => 'Home',                'foreignKey' => 'realtor2_num',          ...)       );  

This completely breaks the application, with the error message "Database table co_listed_homes for model CoListedHomes was not found."

Referring back to the Cookbook, second example, it seems clear that they shouldn't have or need separate "messages_received" and "messages_sent" tables in their database, so what is it that I'm doing wrong?

ETA: Weirdly (to me) I think I got the relationships to work by swapping around the order of the arrays: the first array, on foreignKey = realtor2_num, is called 'ListedHomes', the second array, on foreignKey = realtor_num is called 'Home'. I suppose my question then is, why should the order matter, and why should Cake started talking about unfound database tables in some circumstances?


Just in case, I'd have built this as a categories setup. Then created a join table between Category and Home.

Then you can have a hasAndBelongsToMany beween both categories and homes


"Database table co_listed_homes for model CoListedHomes was not found" probably refers to you not having a model defined for CoListedHomes that points to the homes table.

I would, however, code this as a n:m otherwise known as hasAndBelongsToMany, as stated by DavidYell.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »