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

Quando a classe alvo "targetEntity" pertence a outro namespace

O doctrine parece não buscar a classe através do namespace especificado pelo "use" e tenho o seguinte erro quando executo por linha de comando o "orm:info":

 php vendor/bin/doctrine orm:info

 Found 3 mapped entities:

 [OK]   App\Aluno\Entity\Telefone
 [FAIL] App\Aluno\Entity\Aluno
 The target-entity App\Aluno\Entity\Curso cannot be found in 'App\Aluno\Entity\Aluno#cursos'.

 [FAIL] App\Curso\Entity\Curso
 The target-entity App\Curso\Entity\Aluno cannot be found in 'App\Curso\Entity\Curso#alunos'.

Por fins de especulação, resolvi separar os domínios de cursos e alunos. Portanto, minha estrutura de pastas é a seguinte:


|--> src
|------> Aluno
|-----------> Entity
|--------------------> Telefone
|--------------------> Aluno
|------> Cursos
|-----------> Entity
|--------------------> Curso

Segue o código da classe Curso:

<?php

namespace App\Curso\Entity;


use App\Aluno\Entity\Aluno;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

/**
 * @Entity
 */
class Curso {

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

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

    /**
     * @ManyToMany(targetEntity="Aluno", inversedBy="cursos")
     */
    private $alunos;

    public function __construct()
    {
        $this->alunos = 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 addAluno(Aluno $aluno): self
    {
        if($this->alunos->contains($aluno)){
            return $this;
        }

        $this->alunos->add($aluno);
        $aluno->addCursos($this);
        return $this;
    }
    public function getAlunos(): Collection
    {
        return $this->alunos;
    }
}

Segue o código da classe Aluno:

<?php
namespace App\Aluno\Entity;

use App\Curso\Entity\Curso;
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;

    /**
     * @ManyToMany(targetEntity="Curso", mappedBy="alunos")
     */
    private $cursos;

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

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

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

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

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

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


    public function addCursos(Curso $curso): self
    {
        if($this->cursos->contains($curso)){
            return $this;
        }
        $this->cursos->add($curso);
        $curso->addAluno($this);
        return $this;
    }
    public function getCursos(): Collection
    {
        return $this->cursos;
    }

}

Consegui resolver simplesmente especificando o "caminho" completo para a classe, conforme abaixo:

Classe Curso:


...

class Curso {

....

    /**
     * @ManyToMany(targetEntity="App\Aluno\Entity\Aluno", inversedBy="cursos")
     */
    private $alunos;

....

Classe Aluno:

...

class Aluno
{

...

    /**
     * @ManyToMany(targetEntity="App\Curso\Entity\Curso", mappedBy="alunos")
     */
    private $cursos;

...

Então ... estou fazendo algo errado? Este é o comportamento padrão do Doctrine? Existe alguma forma de forçar ele a entender o meu "use" ou devo mesmo especificar o caminho por completo da entidade quando houverem namespaces distintos?

Desde já agradeço! Valeu!

1 resposta
solução!

Fala, Alisson! Até onde eu sei, esse é o comportamento padrão sim. Não conheço nenhuma forma diferente de implementar não.

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