Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Erro ao listar telefones

Olá pessoal, não estou conseguindo listar os telefones conforme o professor fez na Aula 4 vídeo 6. Até fiz Control + C e Control + V no código dele e continuou não funcionando. Estou usando a IDE Eclipse para PHP.

Ocorre o seguinte erro:

PHP Notice: Undefined index: Aluno in C:\Users\watan\eclipse-workspace\doctrine-alura\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php on line 1778 PHP Notice: Trying to access array offset on value of type null in C:\Users\watan\eclipse-workspace\doctrine-alura\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php on line 1782 PHP Warning: Invalid argument supplied for foreach() in C:\Users\watan\eclipse-workspace\doctrine-alura\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php on line 1782 PHP Notice: Undefined index: Aluno in C:\Users\watan\eclipse-workspace\doctrine-alura\vendor\doctrine\orm\lib\Doctrine\ORM\PersistentCollection.php on line 168 PHP Fatal error: Uncaught Error: Call to a member function setValue() on null in C:\Users\watan\eclipse-workspace\doctrine-alura\vendor\doctrine\orm\lib\Doctrine\ORM\PersistentCollection.php:168 Stack trace:

#0 C:\Users\watan\eclipse-workspace\doctrine-alura\vendor\doctrine\orm\lib\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php(511): Doctrine\ORM\PersistentCollection->hydrateAdd()

#1 C:\Users\watan\eclipse-workspace\doctrine-alura\vendor\doctrine\orm\lib\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php(162): Doctrine\ORM\Internal\Hydration\ObjectHydrator->hydrateRowData()

#2 C:\Users\watan\eclipse-workspace\doctrine-alura\vendor\doctrine\orm\lib\Doctrine\ORM\Internal\Hydration\AbstractHydrator.php(153): Doctrine\ORM\Internal\Hydration\ObjectHydrator->hydrateAllData()

#3 C:\Users\watan\eclipse-workspace\doctrine-alura\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php(951): Doctrine\ORM\Internal\Hydration\AbstractHydrator->hydrateAll()

#4 C:\Users\watan\eclipse-workspace\doctrine-alura\vendor in C:\Users\watan\eclipse-workspace\doctrine-alura\vendor\doctrine\orm\lib\Doctrine\ORM\PersistentCollection.php on line 168

Abaixo está o código do arquivo buscar-alunos.php

buscar-alunos.php

<?php

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

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

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

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

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

foreach ($alunoList as $aluno) {
    $telefones = $aluno->getTelefones()->map(function (Telefone $telefone) {
        $telefone->getNumero();
    })->toArray();
    echo "ID: {$aluno->getId()}\nNome: {$aluno->getNome()}\n\n";

}

Desde já, agradeço!

1 resposta
solução!

Consegui pessoal, erro resolvido!

O erro em si nem estava neste arquivo e sim na classe Aluno, onde no mappedBy coloquei aluno de uma forma diferente do atributo da classe Telefone.