Boa noite, ao executar o arquivo criar-turma.php obtive o erro:
PHP Fatal error: Uncaught Error: Call to a member function execute() on bool in C:\xampp\htdocs\phppdo\src\Domain\Infrastructure\Repository\PdoStudentRepository.php:66
Stack trace:
#0 C:\xampp\htdocs\phppdo\src\Domain\Infrastructure\Repository\PdoStudentRepository.php(55): Alura\Pdo\Domain\Infrastructure\Repository\PdoStudentRepository->insert(Object(Alura\Pdo\Domain\Model\Student))
#1 C:\xampp\htdocs\phppdo\criar-turma.php(17): Alura\Pdo\Domain\Infrastructure\Repository\PdoStudentRepository->save(Object(Alura\Pdo\Domain\Model\Student))
Observação, minha tabela students já está criada, Se alguem puder dar uma olhada nos meus codigos e ver se bate o olho em algum possivel erro... muito obrigada.
criar-turma.php
<?php
.....
$connection = ConnectionCreator::createConnection();
$studentRepository = new PdoStudentRepository($connection);
$connection->beginTransaction();
$aStudent = new Student(null, 'Nico Steppat', new DateTimeImmutable('1985-05-01'));
$studentRepository->save($aStudent);
$anotherStudent = new Student(null, 'Sergio Lopes', new DateTimeImmutable('1985-05-01'));
$studentRepository->save($anotherStudent);
$connection->commit();
PdoStudentRepository
<?php
...
class PdoStudentRepository implements StudentRepository
{
private PDO $connection;
public function __construct(PDO $connection)
{
$this->connection = $connection;
}
public function allStudents(): array
{
$sqlQuery = 'SELECT * FROM students;';
$stmt = $this->connection->query($sqlQuery);
return $this->hydrateStudentList($stmt);
}
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);
}
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;
}
public function save(Student $student): bool
{
if ($student->id() === null) {
return $this->insert($student);
}
return $this->update($student);
}
private function insert(Student $student): bool
{
$insertQuery = 'INSERT INTO students (name, birth_date) VALUES (:name, :birth_date);';
$stmt = $this->connection->prepare($insertQuery);
$success = $stmt->execute([ ->> o erro aponta pra esta linha....
':name' => $student->name(),
':birth_date' => $student->birthDate()->format('Y-m-d'),
]);
if ($success) {
$student->defineId($this->connection->lastInsertId());
}
return $success;
}
private function update(Student $student): bool
{
omiti pra caber....
}
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();
}
}
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
{
return $this->id;
}
public function name(): string
{
return $this->name;
}
public function changeName(string $newName): void
{
$this->name = $newName;
}
public function birthDate(): \DateTimeInterface
{
return $this->birthDate;
}
public function age(): int
{
return $this->birthDate
->diff(new \DateTimeImmutable())
->y;
}
}