Após criar o método fillPhonesOf e alterar o hydrateStudentList, o array está vindo vazio.
Arquivo lista-alunos.php:
Classe PdoStudenRepository:
<?php
namespace Alura\Pdo\Domain\Infrastructure\Repository;
use Alura\Pdo\Domain\Model\Repository\StudentRepository;
use Alura\Pdo\Domain\Model\Student;
use PDO;
class PdoStudentRepository implements StudentRepository
{
private PDO $connection;
public function __construct(PDO $connection)
{
$this->connection = $connection;
}
public function save(Student $student): bool
{
if ($student->id() === null){
return $this->insert($student);
}
return $this->update($student);
}
public function allStudents(): array
{
$sqlQuery = "SELECT * FROM students;";
$stmt = $this->connection->prepare($sqlQuery);
return $this->hydrateStudentList($stmt);
}
private function hydrateStudentList(\PDOStatement $stmt): array
{
$studentDataList = $stmt->fetchAll();
$studentList = [];
foreach ($studentDataList as $studentData) {
$student = new Student(
$studentData['id'],
$studentData['name'],
new \DateTimeImmutable($studentData['birth_date'])
);
$this->fillPhonesOf($student);
$studentList[] = $student;
}
return $studentList;
}
private function fillPhonesOf(Student $student): void
{
$sqlQuery = 'SELECT id, area_code, number FROM phones WHERE student_id = ?';
$stmt = $this->connection->prepare($sqlQuery);
$stmt->bindValue(1, $student->id(), PDO::PARAM_INT);
$stmt->execute();
$phoneDataList = $stmt->fetchAll();
foreach ($phoneDataList as $phoneData) {
$phone = new Phone(
$phoneData['id'],
$phoneData['area-code'],
$phoneData['number']
);
$student->addPhone($phone);
}
}
public function insert(Student $student): bool
{
$insertQuery = "INSERT INTO students (name, birth_date) VALUES (:name,:birth_date);";
$stmt = $this->connection->prepare($insertQuery);
$success = $stmt->execute([
':name' => $student->name(),
':birth_date' => $student->birthDate()->format('Y-m-d')
]);
if ($success) {
$student->defineId($this->connection->lastInsertId());
}
echo "Inserido com sucesso";
return $success;
}
public function update(Student $student): bool
{
$updateQuery = 'UPDATE students SET name = :name, birth_date = :birth_date WHERE id = :id;';
$stmt = $this->connection->prepare($updateQuery);
$stmt->bindValue(':name', $student->name());
$stmt->bindValue(':birth_date', $student->birthDate()->format('Y-m-d'));
$stmt->bindValue(':id', $student->id(), PDO::PARAM_INT);
echo "Atualizado com sucesso";
return $stmt->execute();
}
public function studentsBirthAt(\DateTimeInterface $birthDate): array
{
$sqlQuery = "SELECT * FROM students WHERE birth_date = ?;";
$stmt = $this->connection->prepare($sqlQuery);
$stmt->bindValue(1,$birthDate->format('Y-m-d'));
$stmt->execute();
return $this->hydrateStudentList($stmt);
}
public function remove(Student $student): bool
{
$stmt = $this->connection->prepare("DELETE FROM students WHERE id = ?;");
$stmt->bindValue(1, $student->id(), PDO::PARAM_INT);
return $stmt->execute();
}
}
Classe Phone: Classe Student:
Retorno do arquivo lista-alunos.php: