Dave
Dave

Reputation: 2586

ActiveJDBC Object with Multiple Parents that reference the same Class

I have a fairly simple model with two objects: Team and Game. The Team object has the name of the team, and the Game object looks-up to the Team object twice. One through the "away_team_id" and one through the "home_team_id" field.

  Game ----home_team_id----> Team
    |
    +------away_team_id----> Team

I'm fairly new to ActiveJDBC, but have used PHP ActiveRecord for quite some time. I cannot for the life of me figure out how to reference each team through the Game object. I do have these annotations in my Game object:

@BelongsToParents({ 
@BelongsTo(foreignKeyName="home_team_id",parent=Team.class), 
@BelongsTo(foreignKeyName="away_team_id",parent=Team.class)
}) 

public class Game extends Model {
}

In my test code, I have:

Team homeTeam = game.parent (Team.class);

But obviously that's only one of them, and I'm not even sure how it figures out which one it is! Any help would be greatly appreciated.

Thanks in advance!

Upvotes: 0

Views: 354

Answers (1)

ipolevoy
ipolevoy

Reputation: 5518

This is an unusual case. I would suggest something like this:

public class Game extends Model{
  public Team getHomeTeam(){
    return Team.findFirst("id = ?", get("home_team_id"));
  }
    public Team getAwayTeam(){
    return Team.findFirst("id = ?", get("home_away_id"));
  }
}

I would suggest wrapping ActiveJDBC in semantic methods like this to help potential refactoring in the future.

tx

Upvotes: 1

Related Questions