我有一个User
具有自我引用一对多关系的实体 - 每个人都User
拥有一组学生(他们也是用户):
<?php
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\Common\Collections\ArrayCollection;
/**
* User
*/
class User extends BaseUser
{
/**
* @var int
*/
protected $id;
private $students;
// ....
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
public function __construct() {
$this->students = new ArrayCollection();
// ...
parent::__construct();
}
/**
* Remove student
*
* @return User
*/
public function removeStudent($student)
{
$this->students->removeElement($student);
return $this;
}
/**
* Add a student
*
* @param User $students
*
* @return User
*/
public function addStudent($student)
{
$this->students->add($student);
return $this;
}
/**
* Get students
*
* @return User
*/
public function getStudents()
{
$this->students;
}
/**
* Set students
*
* @param User $students
*
* @return User
*/
public function setStudents($students)
{
$this->students = $students;
return $this;
}
// ....
}
映射是作为具有连接表的一对多单向完成的
AppBundle\Entity\User:
type: entity
table: null
repositoryClass: AppBundle\Repository\UserRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
// ...
manyToMany:
students:
targetEntity: User
joinTable:
name: mentors_students
joinColumns:
mentor_id:
referencedColumnName: id
inverseJoinColumns:
student_id:
referencedColumnName: id
unique: true
lifecycleCallbacks: { }
现在,当我使用 EasyAdmin 捆绑软件添加/编辑用户时,我可以为该用户添加学生。但是,当我检索实体时, students 属性始终为null。例如,当我查看用户列表时:
这里用户“sagarl3232”应该是“sj”的学生,但上面的视图清楚地显示了检索时的属性为空。
该实体已正确保存在数据库中。也就是说,连接表具有正确的值:
为什么教义要这样对我?不是应该自动给学生数组补水吗?