Php 从与Doctrine2的多个关系中获取数据
我在教师表和组表之间有以下关系N:N,其中有第三个教师组表 我想列出该教师教给我的所有组(**相关**),但当我得到所有教师的返回时,Php 从与Doctrine2的多个关系中获取数据,php,orm,doctrine-orm,doctrine,relationship,Php,Orm,Doctrine Orm,Doctrine,Relationship,我在教师表和组表之间有以下关系N:N,其中有第三个教师组表 我想列出该教师教给我的所有组(**相关**),但当我得到所有教师的返回时,$teacher->getClasses()为空 这是我的代码: 教师管理员: namespace App\Controllers; class TeacherController extends Controller { public function index() { $this->teachers = $this-&
$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;
}
}
教师实体:
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;
}
}
集团实体:
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;
}
}
在我看来,您在条令注释中没有正确引用名称空间 解决方案1:
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\ManyToMany(targetEntity="Teacher")
* @ORM\JoinTable(name="teachers_groups",
...
或解决方案2:
use Doctrine\ORM\Mapping\ManyToMany;
use Doctrine\ORM\Mapping\JoinTable;
...
/**
* @ManyToMany(targetEntity="Teacher")
* @JoinTable(name="teachers_groups",
...
换句话说,您的注释当前被忽略。我很惊讶您的教师资源库甚至将教师类视为一个实体。我遵循了他们自己的文档示例:
http://doctrine-orm.readthedocs.org/en/latest/reference/advanced-configuration.html
。我的实体没有问题,包括其他crud学生和具有1:1关系的类。注释还可以。除非遗漏了一些关于关系的信息。我想知道是不是在小组->教师中遗漏了inversedBy=“groups”
。如果添加它不起作用,请在返回$this->conn->executeQuery($sql,$params,$types)之前,在BasicEntityPersister::getManyToManyStatement()
中打印出$sql
代码>以查看发生了什么。