Getting Data from a Manytomany Relationship with Doctrine2

Asked

Viewed 220 times

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;
    }
}
  • 1

    I had answered that the problem was the missing parameter inversedBy in the attribute $professores class Turma, but I tested it here and that’s not it. I inserted some lines in the database, both in the table professores how much in the table turmas and in the relational table, and I was able to return the lines normally. You tried to do this?

  • 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?

  • 1

    that! played inside a Symfony2 project, but the process is basically the same. I’ll put in Github for you.

  • 1

    The following: https://github.com/rodrigorigotti/lala.git

  • The problem wasn’t even my entities, but rather the way I was trying to print, like this: $professor->getTurmas()->getNome(). Instead of making a foreach in $professor->getTurmas(). Thank you very much.

  • How to approve your help?

  • 1

    No need. If you find it necessary, add a response explaining what you did wrong and validate your answer after a few days. :)

  • Blz. Thank you again!

Show 3 more comments

1 answer

0


The problem was neither my Entities, nor in the Annotations nor in the relationship. But rather the way I was trying to print, so:

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

I was wrong, instead I had to make a foreach in $professor->getTurmas(), 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>';
    }

    /* ... */

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.