I have the following N: N relationship between the teacher and class tables, where there is a third table teachers_turtle.
I want to list all the classes that this teacher teaches ( that is related ), but when I return to the teacher's query, $professor->getTurma()
is empty.
Follow my code:
Controller Teacher:
namespace App\Controllers;
class ProfessorController extends Controller
{
public function index()
{
$this->professores = $this->model->getRepository()->findAll();
// Traz todos os professores, porém não traz suas turmas
// com $professor->getTurmas()
foreach ($this->professores as $professor) {
var_dump($professor->getTurmas()); die();
}
}
}
Teacher Entity:
namespace App\Entities;
use Doctrine\Common\Collections\ArrayCollection as ArrayCollection;
/**
* @Entity
* @Table(name="professores")
*/
class Professor
{
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
**/
private $id;
/**
* @ManyToMany(targetEntity="Turma", mappedBy="professores")
**/
private $turmas;
public function __construct()
{
$this->turmas = new ArrayCollection();
}
public function setTurmas($turmas)
{
$this->turmas = $turmas;
}
public function getTurmas()
{
return $this->turmas;
}
}
Class Entity:
namespace App\Entities;
use Doctrine\Common\Collections\ArrayCollection as ArrayCollection;
/**
* @Entity
* @Table(name="turmas")
*/
class Turma
{
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
**/
private $id;
/**
* @ManyToMany(targetEntity="Professor")
* @JoinTable(name="professores_turma",
* joinColumns={@JoinColumn(name="id_turma",referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="id_professor", referencedColumnName="id")}
* )
**/
private $professores;
public function __construct()
{
$this->professores = new ArrayCollection();
}
public function setProfessores($professores)
{
$this->professores = $professores;
}
public function getProfessores()
{
return $this->professores;
}
}