tags:

views:

54

answers:

2

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 3.7.6.7 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 3.7.6.7, 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?

A: 

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

DavidYell
I suspect I misstated the problem or just provided not enough information. I'll mark this as a correct answer, as better table design in the first place would surely save me from this type of problem!
thesunneversets
A: 

"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.

Leo
I certainly didn't have a model defined for CoListedHomes, but then nor did I have a model defined for ListedHomes, which worked later on. I don't think you should need to define models here; though maybe Cake, having discovered a 'Homes' model as the first element of the $hasMany array, was then upset that 'CoListedHomes' wasn't a model too. (This may explain why it worked when I switched the order around, so the first element was a non-model.)I agree that a HABTM relationship was probably the way to go, instead of wrestling with inventing the hasAndBelongsToTwo category. Next time!
thesunneversets