I struggle with this question every time I make a new OOP app. Assuming this will grow to be a mid to large scale site that will keep growing, what is the proper way to implement selecting objects nested in another object (in this case what's the best way to implement querying for matchup info).
Let's say I have this:
Class SportsTeam {
protected $teamId;
protected $teamName;
protected $teamLocation;
}
Class Matchup {
protected $matchupId;
protected $date;
protected $time;
protected $homeTeamId;
protected $awayTeamId;
protected $homeTeam; //SportsTeam object
protected $awayTeam; //SportsTeam object
}
Class SportsTeamDao {
public function getSportsTeam($teamId) { //Get SportTeam info }
}
Class MatchupDao {
public function getMatchup($matchupId) { //Get Matchup info }
}
OPTION 1: Select the matchup object itself and within the matchup object have a call in the get method of homeTeam object and a call in the awayTeam object to get the entire SportsTeam object as necessary so that if I add properties to the SportsTeam object later on (e.g. teamLogo) they are accessible to where I use the matchup object.
Class Matchup {
protected $matchupId;
protected $date;
protected $time;
protected $homeTeamId;
protected $awayTeamId;
protected $homeTeam; //SportsTeam object
protected $awayTeam; //SportsTeam object
//Getters and setters for matchupId, date, time, homeTeamId, awayTeamId
public function getHomeTeam() {
$this->homeTeam = SportsTeamDao::getSportsTeam($this->homeTeamId);
return $homeTeam;
}
public function getAwayTeam() {
$this->awayTeam = SportsTeamDao::getSportsTeam($this->awayTeamId);
return $awayTeam;
}
}
Class SportsTeamDao {
public function getSportsTeam($teamId) { //Get SportTeam info }
}
Class MatchupDao {
public function getMatchup($matchupId) {
$query = "SELECT matchupId, date, time, hometeamid, awayteamid WHERE matchupId = $matchupId LIMIT 1"; //I'd parameterize it of course
}
}
OPTION 2: Select all the details possibly needed for matchup in a join so I only go to the database once. Drawback being if I add properties to SportsTeam later I'd have to remember to add it to my select statements for SportsTeam and Matchup and any other place that needs to use these new properties.
Class SportsTeam {
protected $teamId;
protected $teamName;
protected $teamLocation;
}
Class Matchup {
protected $matchupId;
protected $date;
protected $time;
protected $homeTeamId;
protected $awayTeamId;
protected $homeTeam; //SportsTeam object
protected $awayTeam; //SportsTeam object
}
Class SportsTeamDao {
public function getSportsTeam($teamId) { //Get SportTeam info }
}
Class MatchupDao {
public function getMatchup($matchupId) {
$query = "SELECT M.matchupid, M.date, M.time, M.homeTeamId, M.awayTeamId, HT.teamName AS homeN, HT.teamLocation AS homeL, AT.teamName AS awayN, AT.teamLocation AS awayL FROM matchups M LEFT JOIN sportsteam HT ON M.hometeamid = HT.teamid LEFT JOIN sportsteam AT ON M.awayteamid = AT.teamid WHERE matchupid = $matchupid LIMIT 1";
//Set matchup properties and hometeam object properties and awayteam object properties
}
}
My dilemma is option 1 is more DB intensive (think if I want to display a list of football players on a team, it's 1 call for team + 1 call to get all Id's of players on the team + 53 more calls one for each player object) but easier to manage, option 2 is less DB connections (1 call for team, 1 call for all players) but more tedious to maintain. So what's the proper way to implement this or is there a third better way?
SELECTstatement, I think option 1 is acceptable.