我有一個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;
}
// ....
}
映射爲one-to-many unidirectional with join table
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捆綁用戶,我可以爲用戶添加的學生。但是,當我檢索實體時,學生資產始終爲null。例如,當我查看用戶列表:
在這裏,用戶sagarl3232「被認爲是「SJ」,但鑑於學生上面清楚地顯示在檢索時的屬性爲null。
該實體是在數據庫中正確保存。也就是說,連接表有正確的價值觀:
爲什麼學說這樣對我?是不是應該自動補充學生陣列?
downvote的原因? – MrWarlock616
您是否檢查過(例如symfony調試工具欄)加載頁面時,教義是否發出正確的查詢?您可以在EasyAdmin之外重現它更孤立嗎? –
是的!實際上,doctrine確實發出了一個查詢來獲取所有關聯的學生,例如:SELECT ...,u0_.mentor_id AS mentor_id_17 FROM user u0_ WHERE u0_.id IN(?)ORDER BY u0_.id DESC 參數:[0 => [0 => 8,1 => 7,2 => 6,3 => 5,4 => 1]]' – MrWarlock616