php - Get data from a ManyToMany relationship with Doctrine2 -
i have following relationship n:n between teachers table , groups table, there third teachers_groups table.
i want list teachers bring me groups teacher teaches (** related **), when return of teachers, $teacher->getclasses()
empty.
here code:
teacher controller:
namespace app\controllers; class teachercontroller extends controller { public function index() { $this->teachers = $this->model->getrepository()->findall(); // bring teachers, not bring groups // $teachers->getgroups() foreach ($this->teachers $teachers) { var_dump($teachers->getgroups()); die(); } } }
teacher entity:
namespace app\entities; use doctrine\common\collections\arraycollection arraycollection; /** * @entity * @table(name="teachers") */ class teacher { /** * @id @column(type="integer") * @generatedvalue(strategy="auto") **/ private $id; /** * @manytomany(targetentity="group", mappedby="teachers") **/ private $groups; public function __construct() { $this->groups = new arraycollection(); } public function setgroups($groups) { $this->groups = $groups; } public function getgroups() { return $this->groups; } }
group entity:
namespace app\entities; use doctrine\common\collections\arraycollection arraycollection; /** * @entity * @table(name="groups") */ class group { /** * @id @column(type="integer") * @generatedvalue(strategy="auto") **/ private $id; /** * @manytomany(targetentity="teacher") * @jointable(name="teachers_groups", * joincolumns={@joincolumn(name="id_group",referencedcolumnname="id")}, * inversejoincolumns={@joincolumn(name="id_teacher", referencedcolumnname="id")} * ) **/ private $teachers; public function __construct() { $this->teachers = new arraycollection(); } public function setteachers($teachers) { $this->teachers = $teachers; } public function getteachers() { return $this->teachers; } }
seems me you're not referencing namespaces correctly in doctrine annotations.
solution 1:
use doctrine\orm\mapping orm; /** * @orm\manytomany(targetentity="teacher") * @orm\jointable(name="teachers_groups", ...
or solution 2:
use doctrine\orm\mapping\manytomany; use doctrine\orm\mapping\jointable; ... /** * @manytomany(targetentity="teacher") * @jointable(name="teachers_groups", ...
in other words, annotations ignored. i'm surprised teacher repository recognizes teacher class entity.
Comments
Post a Comment