1
resposta

Uncaught Error: Class 'PDO\Infra\Persistence\ConnectionCreator' not found

Está dizendo que a classe ConnectionCreator não foi encontrada, sendo que dei um use nela usando o proprio autoload

inserirAluno.php

<?php

use Alura\Pdo\Domain\Model\Student;

use PDO\Infra\Persistence\ConnectionCreator;

require_once 'vendor/autoload.php';

$pdo = ConnectionCreator::createConnection();


$student = new Student(
    null,
    'joao carlo',
    new DateTimeImmutable('2003-10-03'));

$sqlInsert = "INSERT INTO students (name, birth_date) values (:name,:birth_date);";
$statement = $pdo->prepare($sqlInsert);
$statement->bindValue(':name',$student->name());
$statement->bindValue(':birth_date',$student->birthDate()->format('Y-m-d'));
$statement->execute();

if($statement->execute()){
    echo "Aluno cadastro com sucesso";
}

ConnectionCreator.php

<?php

namespace PDO\Infra\Persistence;

use PDO;

class ConnectionCreator
{
    public static function createConnection(): PDO
    {
        $databasePath = __DIR__ . '/../../../banco.sqlite';

        return new PDO('sqlite:' . $databasePath);

    }

}
1 resposta

Tentei incrementar a conexao dentro do proprio arquivi "inserirAluno.php" e ele não ta encontrado a minha Classe modelo Student

Fatal error: Uncaught Error: Class 'PDO\Domain\Model\Student' not found in

<?php

use PDO\Domain\Model\Student;

use Alura\PDO\Infra\Persistence\ConnectionCreator;

//$pdo = ConnectionCreator::createConnection();
$caminhoBanco = __DIR__ . '/banco.sqlite';
$pdo = new PDO('sqlite:' . $caminhoBanco);

$student = new Student(
    null,
    "Patricia Freitas",
    new \DateTimeImmutable('1986-10-25')
);


$sqlInsert = "INSERT INTO students (name, birth_date) VALUES (:name, :birth_date);";
$statement = $pdo->prepare($sqlInsert);
$statement->bindValue(':name', $student->name());
$statement->bindValue(':birth_date', $student->birthDate()->format('Y-m-d'));

if ($statement->execute()) {
    echo "Aluno incluído";
}

Codigo da minha Classe Student:

<?php

namespace 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;
    }
}

A organização de pastas estão ok