Solucionado (ver solução)
Solucionado
(ver solução)
6
respostas

buscar-alunos.php

os códigos do buscar-aluno.php esta retornando PHP Fatal Error.

<?php

use Alura\Doctrine\Entity\Aluno;
use Alura\Doctrine\Helper\EntityManagerFactory;

require_once __DIR__ . '/../vendor/autoload.php';

$entityManagerFactory = new EntityManagerFactory();
$entityManager = $entityManagerFactory->getEntityManager();

$alunoRepository = $entityManager->getRepository(Aluno::class);

/** @var Aluno[] $alunoList */
$alunoList = $alunoRepository->findAll();

foreach ($alunoList as $aluno) {
    echo "ID: {$aluno->getId()}\nNome: {$aluno->getNome()}\n\n";
}

Mensagem....

gustavo@gustavo-Spin-SP314-51:/Projetos/PHP$ php "/Projetos/PHP/13-DoctrineAlura/commands/buscar-alunos.php"
PHP Fatal error:  Uncaught TypeError: Return value of Alura\Doctrine\Entity\Aluno::getNome() must be of the type string, null returned in /Projetos/PHP/13-DoctrineAlura/src/Entity/Aluno.php:28
Stack trace:
#0 /Projetos/PHP/13-DoctrineAlura/commands/buscar-alunos.php(17): Alura\Doctrine\Entity\Aluno->getNome()
#1 {main}
  thrown in /Projetos/PHP/13-DoctrineAlura/src/Entity/Aluno.php on line 28
6 respostas

Olá, Gustavo. Como está o código da sua entidade de Aluno?

O método getNome deveria retornar uma string mas está retornando nulo.

Ola Vinicius.

Acredito que o getNome esteja retornando uma string.

<?php

namespace Alura\Doctrine\Entity;

/**
 * @Entity
 */
class Aluno
{
    /**
     * @Id
     * @GeneratedValue
     * @Column(type="integer")
     */
    private $id;
    /**
     * #Column(type="string")
     */
    private $nome;

    public function getid(): int
    {
        return $this->id;
    }

    public function getNome(): string
    {
        return $this->nome;
    }

    public function setNome(string $nome): self
    {
        $this->nome = $nome;
        return $this;
    }
}

O erro tá aqui, Gustavo:

/**
  * #Column(type="string")
  */

Não é com # que definimos docblocks. :-)

Putz... Que coisa heim, a tecla ao lado...

Agora executando o busca-alunos.php a lista de PHP Error aumentou.

gustavo@gustavo-Spin-SP314-51:/Projetos/PHP$ php "/Projetos/PHP/13-DoctrineAlura/commands/buscar-alunos.php"
PHP Fatal error:  Uncaught PDOException: SQLSTATE[HY000]: General error: 1 no such column: t0.nome in /Projetos/PHP/13-DoctrineAlura/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:125
Stack trace:
#0 /Projetos/PHP/13-DoctrineAlura/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php(125): PDO->query()
#1 /Projetos/PHP/13-DoctrineAlura/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOQueryImplementation.php(36): Doctrine\DBAL\Driver\PDOConnection->doQuery()
#2 /Projetos/PHP/13-DoctrineAlura/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(1260): Doctrine\DBAL\Driver\PDOConnection->query()
#3 /Projetos/PHP/13-DoctrineAlura/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php(901): Doctrine\DBAL\Connection->executeQuery()
#4 /Projetos/PHP/13-DoctrineAlura/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php(199): Doctrine\ORM\Persisters\Entity\BasicEntityPersister->loadAll()
#5 /Projetos/PHP/13-DoctrineAlura/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php( in /Projetos/PHP/13-DoctrineAlura/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractSQLiteDriver.php on line 86

Agora deu certo Vinicius.

Exclui o banco.sqlite. Criei novamente com o doctrine orm:schema-tool:create Cadastrei novos alunos.

Agora a consulta esta dando certo.

Provavelmente tinha relação com a # ao inves do @

O procecimento é realmente esse de excluir o bd e criar novamente, ou há outra forma de corrigir?

Tks. Abs.

solução!

Que bom que conseguiu resolver, Gustavo. Com Migrations você conseguiria atualizar a tabela. Vamos falar disso no treinamento. :-D

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