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

Fatal Erro ao rodar o comando "orm:info"

Fatal error: Cannot declare class Alura\Doctrine\Entity\Aluno, because the name is already in use

Classe Aluno:

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")
     */
    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($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;
    }
}

Classe Telefone:

namespace Alura\Doctrine\Entity;

/**
 * @Entity
 */
class Telefone
{
    /**
     * @Id
     * @GenerationValue
     * @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(int $id): self
    {
        $this->id = $id;
        return $this;
    }

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

    public function setNumero(string $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;
    }
}
7 respostas
solução!

Fala, Lucio. Beleza?

Pelo seu erro, você tá tentando criar a classe Aluno em mais de um arquivo. Consegue verificar isso ou disponibilizar todo o seu projeto no GitHub pra eu dar uma olhada?

Não estou conseguindo achar o erro, vou subir no GitHub. Está aqui (https://github.com/gabrielr1999/projeto-doctrine)

O erro é exatamente o que eu disse. Você está criando a classe Aluno 2 vezes.

  1. https://github.com/gabrielr1999/projeto-doctrine/blob/master/src/Entity/Aluno.php
  2. https://github.com/gabrielr1999/projeto-doctrine/blob/master/src/Helper/Entity/Aluno.php

Então eu tenho que apagar o arquivo na pasta "Helper/Entity/Aluno.php". É isso?

Oi Lúcio, é como o Vinicius falou, você está criando duas vezes a classe Aluno.

Pode apagar sem medo a pasta Entity dentro de Helper, você já tem ela em outro local apropriado.

Abraço.

Sim, Lucio. Você nem deveria tê-lo criado.

Se você já tem a classe Aluno definida em Entity, por que definí-la novamente em Helper/Entity?

Obrigado pela ajuda Vinicius, suas aulas são muito boas.

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