Mesmo colocando a anotação na Classe Aluno o erro continuou: PHP Fatal error: Uncaught BadMethodCallException: Undefined method 'buscaPorAluno'. The method name must start with either findBy, findOneBy or countBy!
<?php
namespace Alura\Doctrine\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Alura\Doctrine\Repository\AlunoRepository;
/**
*
@Entity(repositoryClass="Alura\Doctrine\Repository\AlunoRepository")
*/
class Aluno
{
/**
* @Id
* @GeneratedValue
* @Column(type="integer")
*/
private $id;
/**
* @Column(type="string")
*/
private $nome;
/**
* @OneToMany(targetEntity="Telefone", mappedBy="aluno", cascade={"remove", "persist"}, fetch="EAGER")
*/
private $telefones;
/**
* @ManyToMany(targetEntity="Curso", mappedBy="alunos")
*/
private $cursos;
public function __construct()
{
$this->telefones = new ArrayCollection();
$this->cursos = 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;
}
public function addCurso(Curso $curso)
{
if ($this->cursos->contains($curso)) {
return $this;
}
$this->cursos->add($curso);
$curso->addAluno($this);
return $this;
}
public function getCursos(): Collection
{
return $this->cursos;
}
}
<?php
use Alura\Doctrine\Entity\Aluno;
use Alura\Doctrine\Entity\Telefone;
use Alura\Doctrine\Helper\EntityManagerFactory;
use Doctrine\DBAL\Logging\DebugStack;
use Alura\Doctrine\Repository\AlunoRepository;
require_once __DIR__ . '/../vendor/autoload.php';
$entityManagerFactory = new EntityManagerFactory();
$entityManager = $entityManagerFactory->getEntityManager();
$alunosRepository = $entityManager->getRepository(Aluno::class);
$debugStack = new DebugStack();
$entityManager->getConfiguration()->setSQLLogger($debugStack);
/**@var Aluno[] $alunos */
$alunos = $alunosRepository->buscaPorAluno();
/** @var Aluno [] $alunos */
$alunos = $alunosRepository->findAll();
foreach ($alunos as $aluno) {
$telefones = $aluno->getTelefones()
->map(function (Telefone $telefone) {
return $telefone->getNumero();
})
->toArray();
echo "ID: {$aluno->getId()}\n";
echo "Nome: {$aluno->getNome()}\n";
echo "Telefones: " . implode(",", $telefones) . "\n";
$cursos = $aluno->getCursos();
foreach ($cursos as $curso) {
echo "ID Curso: {$curso->getId()}\n";
echo "\tCurso: {$curso->getNome()}";
echo "\n";
}
echo "\n";
}
print_r($debugStack);;