0
I have the following relationship N:N between teacher tables and classes, where there is a third teacher table.
I want you to list the teachers and bring me all the classes this teacher teaches (that is related), but when I return to the teachers' consultation, $professor->getTurma()
empty.
Follows my code:
Controller Professor:
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();
}
}
}
Entidade Professor:
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;
}
}
Entity Turma:
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;
}
}
I had answered that the problem was the missing parameter
inversedBy
in the attribute$professores
classTurma
, but I tested it here and that’s not it. I inserted some lines in the database, both in the tableprofessores
how much in the tableturmas
and in the relational table, and I was able to return the lines normally. You tried to do this?– Rodrigo Rigotti
Opa +Rodrigo Rigotti, sorry for the delay in answering... How did you manage? Did you take getRepository->findAll() all teachers with classes related to the $this->attribute classes of the Teacher Entity and have access to these classes from the getTurmas() method of the Teacher Entity??? How did you do it? You can pass the code?
– LeoFelipe
that! played inside a Symfony2 project, but the process is basically the same. I’ll put in Github for you.
– Rodrigo Rigotti
The following: https://github.com/rodrigorigotti/lala.git
– Rodrigo Rigotti
The problem wasn’t even my entities, but rather the way I was trying to print, like this:
$professor->getTurmas()->getNome()
. Instead of making aforeach
in$professor->getTurmas()
. Thank you very much.– LeoFelipe
How to approve your help?
– LeoFelipe
No need. If you find it necessary, add a response explaining what you did wrong and validate your answer after a few days. :)
– Rodrigo Rigotti
Blz. Thank you again!
– LeoFelipe