Welcome Guest, Not a member yet? Register   Sign In
php-activerecord and 2 foreign keys referring to one table
#1

[eluser]thevenin[/eluser]
Hi.

In php-activerecord it is easy to create association with foreign table when we're using standard field names ("user_id", "post_id", etc.) and there is only one FK associated with one foreign table: user_id -> users table, post_id -> posts table, etc.

In my database I would like to retrieve teams' names for each match (table: matches).

Every match record has 2 foreign keys:

Code:
home_team_id // referring to "teams.name"
Code:
away_team_id // referring to "teams.name"

I know how to create query in plain SQL but I would like to use php-activerecord.

Do you have any idea how to create delegates/associations to retrieve teams' names in $match object?


"teams" table:

Code:
CREATE TABLE `teams` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(32) NOT NULL,
  `short_name` varchar(16) DEFAULT NULL,
  `code` varchar(3) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

"matches" table:

Code:
CREATE TABLE `matches` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `home_team_id` int(11) unsigned NOT NULL,
  `away_team_id` int(11) unsigned NOT NULL,
  `home_goals` enum('0','1','2','3','4','5','6','7','8','9','10') NOT NULL DEFAULT '0',
  `away_goals` enum('0','1','2','3','4','5','6','7','8','9','10') NOT NULL DEFAULT '0',
  `starting_at` datetime NOT NULL,
  `status` enum('not_started','afoot','finished') NOT NULL DEFAULT 'not_started',
  `stage` enum('group_a','group_b','group_c','group_d','quarter_final','semi_final','final') NOT NULL,
  PRIMARY KEY (`id`),
  KEY `home_team` (`home_team_id`),
  KEY `away_team` (`away_team_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;


Messages In This Thread
php-activerecord and 2 foreign keys referring to one table - by El Forum - 03-21-2012, 09:56 AM



Theme © iAndrew 2016 - Forum software by © MyBB