Quando executo meu arquivo "criar-turma.php", o erro abaixo ocorre:
PHP Fatal error: Uncaught DomainException: ID é null in /home/alanfs/Documentos/projetos-dev/php-pdo/src/Domain/Model/Student.php:29
Stack trace:
#0 /home/alanfs/Documentos/projetos-dev/php-pdo/src/Infraestructure/Repository/PdoStudentRepository.php(56): Alura\Pdo\Domain\Model\Student->id()
#1 /home/alanfs/Documentos/projetos-dev/php-pdo/criar-turma.php(19): Alura\Pdo\Infraestructure\Repository\PdoStudentRepository->save()
#2 {main}
thrown in /home/alanfs/Documentos/projetos-dev/php-pdo/src/Domain/Model/Student.php on line 29
Percebi que o problema é devido ao ID ser "null" quando tenta inserir pela exception que coloquei no código, mas não sei como resolver. Meus arquivos:
Student.php
<?php
namespace Alura\Pdo\Domain\Model;
class Student
{
private ?int $id;
private string $name;
private \DateTimeInterface $birthDate;
public function __construct(?int $id, string $name, \DateTimeInterface $birthDate)
{
$this->id = $id;
$this->name = $name;
$this->birthDate = $birthDate;
}
public function defineId(int $id): void
{
if (!is_null($this->id)) {
throw new \DomainException('Você só pode definir o ID uma vez');
}
$this->id = $id;
}
public function id(): int
{
if (is_null($this->id)) {
throw new \DomainException('ID é null');
}
return $this->id;
}
public function name(): string
{
return $this->name;
}
// Outros métodos
}
PdoStudentRepository.php
<?php
namespace Alura\Pdo\Infraestructure\Repository;
use PDO;
use Alura\Pdo\Domain\Model\Student;
use Alura\Pdo\Domain\Repository\StudentRepository;
class PdoStudentRepository implements StudentRepository
{
private PDO $connection;
public function __construct(PDO $connection)
{
$this->connection = $connection;
}
public function insert(Student $student): bool
{
var_dump($student);
$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());
}
return $success;
}
public function save(Student $student): bool
{
if ($student->id() === null) {
return $this->insert($student);
}
return $this->update($student);
}
private function update(Student $student): bool
{
$updateQuery = 'UPDATE students SET name = :name, birth_date = :birth WHERE id = :id;';
$stmt = $this->connection->prepare($updateQuery);
$stmt->bindValue(':name', $student->name());
$stmt->bindValue(':birth', $student->birthDate()->format('Y-m-d'));
$stmt->bindValue(':id', $student->id());
return $stmt->execute();
}
// outras funções
private function hydrateStudentList(\PDOStatement $stmt): array
{
$studentDataList = $stmt->fetchAll(PDO::FETCH_ASSOC);
$studentList = [];
foreach ($studentDataList as $studentData) {
$studentList[] = new Student(
$studentData['id'],
$studentData['name'],
new \DateTimeImmutable($studentData['birth_date'])
);
}
return $studentList;
}
}
criar-turma.php
<?php
use Alura\Pdo\Domain\Model\Student;
use Alura\Pdo\Infraestructure\Repository\PdoStudentRepository;
use Alura\Pdo\Infraestructure\Persistence\ConnectionCreator;
require_once 'vendor/autoload.php';
$connection = ConnectionCreator::createConnection();
$studentRepository = new PdoStudentRepository($connection);
// Inserir os alunos da turma
$connection->beginTransaction();
$studentA = new Student(
null,
'Nico Outback',
new DateTimeImmutable('1987-07-07'),
);
$studentRepository->save($studentA);
$studentB = new Student(null, 'Alan Proist', new DateTimeImmutable('1985-05-31'));
$studentRepository->save($studentB);
$connection->commit();
Poderiam me ajudar a resolver?