/** * Return a collection of teams * */ public function findAll() { $orderBy = array('name' => 'ASC'); $queryBuilder = $this->db->createQueryBuilder(); $queryBuilder->select('t.*')->from('team', 't')->orderBy('t.' . key($orderBy), current($orderBy)); $statement = $queryBuilder->execute(); $teamsData = $statement->fetchAll(); $teams = array(); foreach ($teamsData as $teamData) { $teamId = $teamData['id']; $team = $this->hydrateTeam($teamData); $teammates = $this->db->fetchAll('SELECT * FROM team_has_teammate INNER JOIN teammate ON team_has_teammate.teammate_id = teammate.id WHERE team_id = ?', array($teamId)); $arrayTeammate = array(); foreach ($teammates as $teammateData) { $teammate = new Teammate(); $teammate->hydrate($teammateData); $arrayTeammate[] = $teammate; } $team->setTeammates($arrayTeammate); $teams[$teamId] = $team; } return $teams; }
/** * @param $teamData * @return Teammate */ public function hydrateTeammate($teamData) { $teammate = new Teammate(); $teammate->setId($teamData['id']); $teammate->setFirstname($teamData['firstname']); $teammate->setLastname($teamData['lastname']); $teammate->setEmail($teamData['email']); $teammate->setPhone($teamData['phone']); $teammate->setImage($teamData['image']); return $teammate; }