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

Aula 4 atividade 7 - Erro ao buscar alunos

Estou com o erro abaixo quando tento buscar os alunos e seus telefones

PHP Notice:  Undefined index: Aluno in C:\xampp\htdocs\cursos_alura\php\doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.p
hp on line 1789
PHP Notice:  Trying to access array offset on value of type null in C:\xampp\htdocs\cursos_alura\php\doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\
Entity\BasicEntityPersister.php on line 1793
PHP Warning:  Invalid argument supplied for foreach() in C:\xampp\htdocs\cursos_alura\php\doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\Basi
cEntityPersister.php on line 1793
PHP Notice:  Undefined index: Aluno in C:\xampp\htdocs\cursos_alura\php\doctrine\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:\xampp\htdocs\cursos_alura\php\doctrine\vendor\doctrine\orm\lib\Doctrine\O
RM\PersistentCollection.php:168
Stack trace:
#0 C:\xampp\htdocs\cursos_alura\php\doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php(511): Doctrine\ORM\PersistentCollectio
n->hydrateAdd()
#1 C:\xampp\htdocs\cursos_alura\php\doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\Internal\Hydration\ObjectHydrator.php(162): Doctrine\ORM\Internal\Hydration\
ObjectHydrator->hydrateRowData()
#2 C:\xampp\htdocs\cursos_alura\php\doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\Internal\Hydration\AbstractHydrator.php(202): Doctrine\ORM\Internal\Hydratio
n\ObjectHydrator->hydrateAllData()
#3 C:\xampp\htdocs\cursos_alura\php\doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\Entity\BasicEntityPersister.php(960): Doctrine\ORM\Internal\Hydra
tion\AbstractHydrator->hydrateAll()
#4 C:\xampp\htdocs\cursos_alura\php\doctrine\vendor\doctrine\orm\lib\Doctrine\ORM\Persi in C:\xampp\htdocs\cursos_alura\php\doctrine\vendor\doctrine\orm\lib\D
octrine\ORM\PersistentCollection.php on line 168

Segue abaixo meus arquivos:

Telefone:

<?php

namespace Alura\Doctrine\Entity;

/**
 * @Entity
 */
class Telefone
{
   /**
    * @Id
    * @GeneratedValue
    * @Column (type = "integer")
    */
   private $id;

   /**
    * @Column (type = "string")
    */
   private $numero;

   /**
    * @ManyToOne (targetEntity="Aluno")
    */
   private $aluno;

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

   public function setId($id): self
   {
      $this->id = $id;
      return $this;
   }

   public function getNumero(): string
   {
      return $this->numero;
   }

   public function setNumero($numero): self
   {
      $this->numero = $numero;
      return $this;
   }

   public function getAluno(): Aluno
   {
      return $this->aluno;
   }

   public function setAluno(Aluno $aluno): self
   {
      $this->aluno = $aluno;
      return $this;
   }
}

Aluno:

<?php

namespace Alura\Doctrine\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

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

   /**
     * @Column(type="string")
     */
   private $nome;

   /**
     * @OneToMany(targetEntity="Telefone", mappedBy="Aluno", cascade={"remove", "persist"})
     */
   private $telefones;

   public function __construct()
   {
      $this->telefones = new ArrayCollection();
   }


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

   public function addTelefone(Telefone $telefone )
   {
      $this->telefones->add($telefone);
      $telefone->setAluno($this);
      return $this;
   }

   public function getTelefones(): Collection
   {
      return $this->telefones;
   }
}

Buscar aluno:

<?php

use Alura\Doctrine\Entity\Aluno;
use Alura\Doctrine\Entity\Telefone;
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();

//listando todos os alunos e seus telefones
foreach ($alunoList as $aluno) {
    $telefones = $aluno
        ->getTelefones()
        ->map(function (Telefone $telefone){
            return $telefone->getNumero();
        })
        ->toArray()
    ;
    echo "ID: {$aluno->getId()}\nNome: {$aluno->getNome()}\n";
    echo "Telefones: " . implode(', ', $telefones);
    echo "\n\n";
}

Consigo criar os alunos normalmente

1 resposta
solução!

Resolvido já galera, Obrigado

O Erro estava na classe Aluno quando no "mappedBy" coloquei "Aluno" e não "aluno" como havia definido na classe Telefone