【发布时间】:2014-12-01 23:37:52
【问题描述】:
我在教师表和组表之间有以下关系N:N,其中还有第三个教师组表。
我想列出老师给我带这个老师教的所有小组(**是相关的**),但是当我得到所有老师的返回时,$teacher->getClasses()是空的。
这是我的代码:
教师控制器:
namespace App\Controllers;
class TeacherController extends Controller
{
public function index()
{
$this->teachers = $this->model->getRepository()->findAll();
// Bring all teachers, but does not bring their groups
// with $teachers->getGroups()
foreach ($this->teachers as $teachers) {
var_dump($teachers->getGroups()); die();
}
}
}
教师实体:
namespace App\Entities;
use Doctrine\Common\Collections\ArrayCollection as 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;
}
}
集团实体:
namespace App\Entities;
use Doctrine\Common\Collections\ArrayCollection as 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;
}
}
【问题讨论】:
-
不知道是不是Group->teachers中缺少的
inversedBy="groups"。如果添加它没有帮助,请在BasicEntityPersister::getManyToManyStatement()中的return $this->conn->executeQuery($sql, $params, $types);之前打印出$sql以查看发生了什么。
标签: php orm doctrine-orm doctrine relationship