Get data from a ManyToMany relationship with Doctrine2

0

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;
    }
}
    
asked by anonymous 01.12.2014 / 20:46

1 answer

0

The problem was neither my Entities, nor the Annotations, nor the relationship. But the form I was trying to print, like this:

$professor->getTurmas()->getNome()

It was wrong, instead I had to make a foreach in $professor->getTurmas() , it was as follows in the Teacher Controller:

namespace App\Controllers;

class ProfessorController extends Controller
{
    public function index()
    {
        $this->professores = $this->model->getRepository()->findAll();

        foreach ($this->professores as $professor)
            if ($professor->getTurmas()->count() > 0)
                foreach ($professor->getTurmas() as $turma)
                     echo 'TurmaId: ' . $turma->getId() . ' -> TurmaNome: ' . $turma->getNome() . '<br>';
    }

    /* ... */

}
    
07.12.2014 / 00:59