dbh = $dbh; } private function mapRowToTeam(array $row): Team { $team = new Team(); $team->id = $row['LagID']; $team->setName($row['LagNavn']); $team->setCompany($row['Bedrift']); $team->setCardnumber($row['Kortnummer']); $team->setLeader($row['Lagleder']); $team->setPhone($row['Telefon']); $team->setParticipants($row['Deltagere']); $team->setRounds($row['Runder']); $team->bestTime = $row['Bestetid']; return $team; } /** * Returns an array of all teams */ public function getAll(): array { $sth = $this->dbh->query('SELECT * FROM lagtabell'); $assoc_array = $sth->fetchAll(PDO::FETCH_ASSOC); $teams = []; foreach ($assoc_array as $key => $row) { array_push($teams, $this->mapRowToTeam($row)); } return $teams; #while ($assoc_array) #{ # array_push($teams, $this->mapRowToTeam($assoc_array[0])); # array_pop($assoc_array); #} #return $teams; } public function getByCardnumber(string $cardnumber): ?Team { $sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE Kortnummer = ?'); $sth->execute([$cardnumber]); $row = $sth->fetch(PDO::FETCH_ASSOC); if ($row) { return $this->mapRowToTeam($row); } return NULL; } public function get(int $id): ?Team { $sth = $this->dbh->prepare('SELECT * FROM lagtabell WHERE LagID = ?'); $sth->execute([$id]); $row = $sth->fetch(PDO::FETCH_ASSOC); if ($row) { return $this->mapRowToTeam($row); } return NULL; } public function create(Team $team): Team { $sth = $this->dbh->prepare( 'INSERT INTO lagtabell (LagNavn, Bedrift, Kortnummer, Lagleder, Telefon, Deltagere, Runder, Bestetid) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' ); $sth->execute([ $team->name, $team->company, $team->cardnumber, $team->leader, $team->phone, $team->participants, $team->rounds, $team->bestTime ]); $lastId = $this->dbh->lastInsertId(); return $this->get($lastId); } public function update(Team $team): Team { $sth = $this->dbh->prepare( 'UPDATE lagtabell SET LagNavn = ?, Bedrift = ?, Kortnummer = ?, Lagleder = ?, Telefon = ?, Deltagere = ?, Runder = ?, Bestetid = ? WHERE LagID = ?' ); $sth->execute([ $team->name, $team->company, $team->cardnumber, $team->leader, $team->phone, $team->participants, $team->rounds, $team->bestTime, $team->id ]); return $this->get($team->id); } public function delete(int $id): void { $sth = $this->dbh->prepare('DELETE FROM lagtabell WHERE LagID = ?'); $sth->execute([$id]); } }