kem*_*ica 0 php symfony doctrine-orm
这是我以前做过的事情,所以我很困惑为什么它不起作用。
我有两个实体Question和Qresponse。Question是拥有方,Qresponse是相反方。当我使用原则查找所有问题时,qresponses 属性始终为空。
//$questions is populated, but the qresponses property is always empty
$questions = $this->getDoctrine()->getManager()->getRepository(Question::class)->findAll();
Run Code Online (Sandbox Code Playgroud)
为什么是空的?我究竟做错了什么?
拥有方片段:问题
/**
* Class Question
* @package Entity
*
* @ORM\Entity
*/
class Question
{
public function __construct()
{
$this->qresponses = new ArrayCollection();
}
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/** @var ArrayCollection $responses
* @ORM\ManyToMany(targetEntity="Qresponse", mappedBy="questions", cascade={"persist"})
*/
private $qresponses;
}
Run Code Online (Sandbox Code Playgroud)
反面片段:Qresponse
/**
* Class Response
* @package Entity
*
* @ORM\Entity
*/
class Qresponse
{
public function __construct()
{
$this->questions = new ArrayCollection();
}
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var ArrayCollection $question
* @ORM\ManyToMany(targetEntity="Question", inversedBy="qresponses", cascade={"persist"})
* @ORM\JoinTable(name="qresponse_question")
*/
private $questions;
}
Run Code Online (Sandbox Code Playgroud)
已填充的数据库的图像。
来自 symfony 中的探查器的图像显示 qresponses 为空...
你没有做错什么,这只是一个典型的学说水合作用问题。
Doctrine 默认使用延迟加载,这意味着关联仅在需要时(例如,被$question->getQResponses()->first()->getId()调用时)加载。您可以通过fetch在协会中将学说选项设置为 EAGER 来轻松更改它:
/**
* @var ArrayCollection $responses
* @ORM\ManyToMany(targetEntity="Qresponse", mappedBy="questions", cascade={"persist"}, fetch="EAGER")
*/
private $qresponses;
Run Code Online (Sandbox Code Playgroud)