Boa tarde,
Venho aqui mais uma vez, pedir ajudar porque já tentei vários métodos, até um que tem no fórum de programação, mas continuo com o mesmo erro, quando tento executar o arquivo "criar-turma.php", aparece para mim esse erro: PHP Fatal error: Uncaught Error: Call to a member function execute() on bool in C:\Users\felip\php-pdo-projeto-inicial\src\Infrastructure\Repository\PdoStudentRepository.php:66 Stack trace:
#0 C:\Users\felip\php-pdo-projeto-inicial\src\Infrastructure\Repository\PdoStudentRepository.php(56): Alura\Pdo\Infrastructure\Repository\PdoStudentRepository->insert()
#1 C:\Users\felip\php-pdo-projeto-inicial\criar-turma.php(20): Alura\Pdo\Infrastructure\Repository\PdoStudentRepository->save()
#2 {main} thrown in C:\Users\felip\php-pdo-projeto-inicial\src\Infrastructure\Repository\PdoStudentRepository.php on line 66
Os códigos dos arquivos abaixo: criar-turma.php
<?php
use Alura\Pdo\Domain\Model\Student;
use Alura\Pdo\infrastructure\Persistence\ConnectionCreator;
use Alura\Pdo\Infrastructure\Repository\PdoStudentRepository;
require_once 'vendor/autoload.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
<?php
namespace Alura\Pdo\Infrastructure\Repository;
use Alura\Pdo\Domain\Model\Student;
use Alura\Pdo\Domain\Repository\StudentRepository;
use DateTimeInterface;
use PDO;
class PdoStudentRepository implements StudentRepository
{
private PDO $connection;
public function __construct(PDO $connection)
{
$this->connection = $connection;
}
public function allStudent(): array
{
$sqlQuery = 'SELECT * FROM student;';
$stmt = $this->connection->query($sqlQuery);
return $this->hydrateStudentList($stmt);
}
public function studentBirthAt(DateTimeInterface $birthDate): array
{
$sqlQuery = 'SELECT * FROM student 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);
}
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());
}
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);
return $stmt->execute();
}
public function remove(Student $student): bool
{
$stmt = $this->connection->prepare('DELETE FROM student WHERE id = ?;');
$stmt->bindValue(1, $student->id(), PDO::PARAM_INT);
return $stmt->execute();
}
}