Symfony2:Doctrine 不会以多对多关系加载相关实体
Symfony2: Doctrine does not load related entities in many-to-many relation
我有一个多对多关系,当我加载一个位于该关系一侧的实体时,我希望在另一侧看到它的 属性 相关实体的 ArrayCollection。然而,这并没有发生——加载的 ArrayCollection 中没有任何元素,而在数据库中我可以看到相关条目。可能是什么原因?
这是我的代码:
关系的一侧,ConsolidatedReport class:
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="P24\Response", inversedBy="consolidatedReports")
* @ORM\JoinTable(name="con_rprt_responses")
*/
private $responses;
关系的另一面,响应class:
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="P24\ConsolidatedReport\ConsolidatedReport", mappedBy="responses")
*/
private $consolidatedReports;
这是我 运行 获取 ConsolidatedReport 实例的函数。此函数位于从容器调用的服务中:
/**
* Picks the consolidated report with given id.
*
* @param string $id
*
* @return ConsolidatedReport
*
* @throws NonExistentConsolidatedReportException if the survey doesn't exist
*/
public function pick($id)
{
$report = $this->repository->findOneBy(array('id' => $id));
if (!$report) {
throw new NonExistentConsolidatedReportException($id);
}
return $report;
}'
在数据库中,有 "con_rprt_responses" table 两列 "consolidated_reports_id" 和 "response_id"。但是,在探查器中,我没有看到对 table.
的任何查询
这里会出什么问题?
更新:
请在下面查看我对这个问题的回答,对我有用。
如果您指定 joinColumns,这是否解决了您的问题?
/**
* @ORM\ManyToMany(targetEntity="P24\Response", inversedBy="consolidatedReports")
* @ORM\JoinTable(name="con_rprt_responses",
* joinColumns={@ORM\JoinColumn(name="consolidated_reports_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="response_id", referencedColumnName="id")}
* )
*/
必须使用 ArrayCollection 初始化 *toMany 属性。
public function __construct() {
$this->responses = new \Doctrine\Common\Collections\ArrayCollection();
$this-> consolidatedReports = new \Doctrine\Common\Collections\ArrayCollection();
}
我将 fetch="EAGER"
添加到 ConsolidatedReport class 的 $responses 属性 中,它起作用了。
代码现在看起来像这样:
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="P24\Response", inversedBy="consolidatedReports", fetch="EAGER")
* @ORM\JoinTable(name="con_rprt_responses")
*/
private $responses;
更多信息在这里:
http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-objects.html#by-eager-loading
不过,如果有人知道为什么相关实体的集合在没有明确指定 EAGER 提取的情况下无法加载 - 请分享您的知识,我们将不胜感激!
如果您有 多个查询 来使用 Doctrine 获取相同的对象,请尝试使用:
$entityManager->clear();
在两者之间,修复 "missing" 个实体。这不是解决方案 "as is",但可以让您了解查询链中的错误。
我有一个多对多关系,当我加载一个位于该关系一侧的实体时,我希望在另一侧看到它的 属性 相关实体的 ArrayCollection。然而,这并没有发生——加载的 ArrayCollection 中没有任何元素,而在数据库中我可以看到相关条目。可能是什么原因?
这是我的代码:
关系的一侧,ConsolidatedReport class:
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="P24\Response", inversedBy="consolidatedReports")
* @ORM\JoinTable(name="con_rprt_responses")
*/
private $responses;
关系的另一面,响应class:
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="P24\ConsolidatedReport\ConsolidatedReport", mappedBy="responses")
*/
private $consolidatedReports;
这是我 运行 获取 ConsolidatedReport 实例的函数。此函数位于从容器调用的服务中:
/**
* Picks the consolidated report with given id.
*
* @param string $id
*
* @return ConsolidatedReport
*
* @throws NonExistentConsolidatedReportException if the survey doesn't exist
*/
public function pick($id)
{
$report = $this->repository->findOneBy(array('id' => $id));
if (!$report) {
throw new NonExistentConsolidatedReportException($id);
}
return $report;
}'
在数据库中,有 "con_rprt_responses" table 两列 "consolidated_reports_id" 和 "response_id"。但是,在探查器中,我没有看到对 table.
的任何查询这里会出什么问题?
更新: 请在下面查看我对这个问题的回答,对我有用。
如果您指定 joinColumns,这是否解决了您的问题?
/**
* @ORM\ManyToMany(targetEntity="P24\Response", inversedBy="consolidatedReports")
* @ORM\JoinTable(name="con_rprt_responses",
* joinColumns={@ORM\JoinColumn(name="consolidated_reports_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="response_id", referencedColumnName="id")}
* )
*/
必须使用 ArrayCollection 初始化 *toMany 属性。
public function __construct() {
$this->responses = new \Doctrine\Common\Collections\ArrayCollection();
$this-> consolidatedReports = new \Doctrine\Common\Collections\ArrayCollection();
}
我将 fetch="EAGER"
添加到 ConsolidatedReport class 的 $responses 属性 中,它起作用了。
代码现在看起来像这样:
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="P24\Response", inversedBy="consolidatedReports", fetch="EAGER")
* @ORM\JoinTable(name="con_rprt_responses")
*/
private $responses;
更多信息在这里: http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-objects.html#by-eager-loading
不过,如果有人知道为什么相关实体的集合在没有明确指定 EAGER 提取的情况下无法加载 - 请分享您的知识,我们将不胜感激!
如果您有 多个查询 来使用 Doctrine 获取相同的对象,请尝试使用:
$entityManager->clear();
在两者之间,修复 "missing" 个实体。这不是解决方案 "as is",但可以让您了解查询链中的错误。