1
resposta

Erro ao tentar listar alunos

Olá pessoa. Estou seguindo as aulas, porém encontrei um erro ao tentar executar a listagem dos alunos cadastrados. Ao executar o arquivo lista-alunos.php, o sistema apresenta um erro, informando que um dos parâmetros deveria ser uma string, mas está como null. Tentei achar o problema mas infelizmente não consegui. Segue abaixo a mensagem de erro bem como o código, para análise.

ERRO

D:\php-7.4.9-nts-Win32-vc15-x64\php.exe D:\Alura\php-pdo-projeto-inicial\lista-alunos.php
PHP Fatal error:  Uncaught TypeError: Argument 2 passed to Alura\Pdo\Domain\Model\Student::__construct() must be of the type string, null given, called in D:\Alura\php-pdo-projeto-inicial\lista-alunos.php on line 18 and defined in D:\Alura\php-pdo-projeto-inicial\src\Domain\Model\Student.php:11
Stack trace:
#0 D:\Alura\php-pdo-projeto-inicial\lista-alunos.php(18): Alura\Pdo\Domain\Model\Student->__construct(2, NULL, Object(DateTimeImmutable))
#1 {main}
  thrown in D:\Alura\php-pdo-projeto-inicial\src\Domain\Model\Student.php on line 11

Fatal error: Uncaught TypeError: Argument 2 passed to Alura\Pdo\Domain\Model\Student::__construct() must be of the type string, null given, called in D:\Alura\php-pdo-projeto-inicial\lista-alunos.php on line 18 and defined in D:\Alura\php-pdo-projeto-inicial\src\Domain\Model\Student.php:11
Stack trace:
#0 D:\Alura\php-pdo-projeto-inicial\lista-alunos.php(18): Alura\Pdo\Domain\Model\Student->__construct(2, NULL, Object(DateTimeImmutable))
#1 {main}
  thrown in D:\Alura\php-pdo-projeto-inicial\src\Domain\Model\Student.php on line 11

Process finished with exit code 255

inserir-aluno.php

<?php

use Alura\Pdo\Domain\Model\Student;

require_once 'vendor/autoload.php';

$databasePath = __DIR__ . '/banco.sqlite';
$pdo = new PDO('sqlite:' . $databasePath);

$student = new Student(
    null,
    "João Passareli",
    new \DateTimeImmutable( '1991-08-22')
);

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

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

$pdo->exec($sqlInsert);

lista-aluno.php

<?php

use Alura\Pdo\Domain\Model\Student;

require_once 'vendor/autoload.php';

$databasePath = __DIR__ . '/banco.sqlite';
$pdo = new PDO('sqlite:' . $databasePath);

$statement = $pdo->query('SELECT * FROM students;');
$studentDataList = $statement->fetchAll(PDO::FETCH_ASSOC);
$studentList = [];

foreach ($studentDataList as $studentData) {
    $studentList[] = new Student(
        $studentData['id'],
        $studentData['name'],
        new \DateTimeImmutable($studentData['birth_date'])
    );
}
1 resposta

Olá João,

Acho que esse tópico acabou ficando duplicado, vamos continuar a discução pelo outro tópico:

https://cursos.alura.com.br/forum/topico-erro-ao-tentar-listar-alunos-130798

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software