Wole,

Understanding the differences between the relation types can be a little difficult at first. I still struggle with remembering, because it can make sense semantically either way! :)

My rule of thumb is this:
- If a table record "has" another record, the foreign record points to this record
- If a table record "belongs" to another record, this record points to the foreign record

I'll often say it to myself this way:
"If I _have_, then they point to me, but if I _belong_, I point to them"

Therefore, according to your schema, I would make the following assertions:

1. Every Team has many (home) Games
2. Every Team has many (away) Games
3. Every Game belongs to a Season
4. Every Game belongs to one home team
5. Every Game belongs to one away team

To associate the same model to different keys, do something along these lines:

class Team extends AppModel {
    $var name = "Team";

    $var hasMany = array(
        'HomeGames' => array( 'className' => 'Game', 'foreignKey' => 'home'),
        'AwayGames' => array( 'className' => 'Game', 'foreignKey' => 'visitor')
    );
}

class Game extends AppModel {
    $var name = "Game";

    $var belongsTo = array(
       'Season'  => array( 'className' => 'Season' ),   // NOTE: You can leave season_id out, as it is assumed
       'Home'    => array( 'className' => 'Team', 'foreignKey' => 'home'),
       'Visitor' => array( 'className' => 'Team', 'foreignKey' => 'visitor')
    );
}


Then, when you do something like

    $this->Team->recursive = 1;  //(if it's not already set)
    $this->set('myTeam', $this->Team->find($conditions));

you will have access to the Teams' games in the view like this

$myTeam['HomeGames'];

Does that help?

Wole wrote:
Chris,

Thanks for your response. Can you please explain a little more? My
games database has two columns, "home" and  "visitor", that specify
the names of the teams that played the game. To do a

Game belongsTo Team

association I need one foreignKey but in this case I have two possible
foreignKeys, "home" and  "visitor". How will the above associations
work in my case? Do I need to change my database structure? Please see
database table listing below. Thanks.

GAMES
======

  `id` varchar(16) NOT NULL default '',
  `season_id` varchar(16) NOT NULL default '',
  `gamedatetime` datetime NOT NULL default '0000-00-00 00:00:00',
  `home` varchar(255) NOT NULL default '',
  `visitor` varchar(255) NOT NULL default '',
  `homescore` int(11) NOT NULL default '0',
  `visitorscore` int(11) NOT NULL default '0',
  `winner` varchar(16) NOT NULL default '',
  `created` datetime NOT NULL default '0000-00-00 00:00:00',
  `modified` datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`id`)

TEAMS
======

  `id` varchar(16) NOT NULL default '',
  `city_id` varchar(16) NOT NULL default '',
  `name` varchar(255) NOT NULL default '',
  `played` int(11) NOT NULL default '0',
  `wins` int(11) NOT NULL default '0',
  `losses` int(11) NOT NULL default '0',
  `draws` int(11) NOT NULL default '0',
  `description` text NOT NULL,
  `created` datetime NOT NULL default '0000-00-00 00:00:00',
  `modified` datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`id`)



  


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to