Olá.
Eu tentei resolver o desafio proposto em aula, da maneira que pensei, está correto? OBS: Os dois fragmentos de códigos abaixo pertencem a mesma classe
public function studentsWithPhones(): array
{
$sql = "SELECT
students.id,
students.name,
students.birth_date,
phones.id AS phone_id,
phones.area_code,
phones.number
FROM students
INNER JOIN phones ON phones.student_id = students.id";
$sql = $this->pdo->query($sql);
$result = $sql->fetchAll();
$studentList = [];
foreach ($result as $row) {
$this->insertPhoneInStudents($row, $studentList);
}
return $studentList;
}
private function insertPhoneInStudents($data, &$studentList): void
{
if(!array_key_exists($data['id'], $studentList)) {
$studentList[$data['id']] = new Student($data['id'], $data['name'], new \DateTimeImmutable($data['birth_date']));
}
$phone = new Phone($data['phone_id'], $data['area_code'], $data['number']);
$studentList[$data['id']]->addPhone($phone);
}