Doctrine DQL,正确获取包含所有引用和字段的整个实体

Doctrine DQL, fetching an entire entity with all references and fields correctly

如何使用 DQL 获取对象并正确获取所有字段和引用? 当我使用以下语句获取实体列表时:

return $this->orm->createQuery('SELECT pp FROM PagePlugin pp where pp.page = :page')->setParameter('page', $page)->getResult();

我将获得一个 PagePlugins 列表,其中每个 PagePlugin 都有一个对 Plugin 的引用:

/**
 * @var \Plugin
 *
 * @ORM\ManyToOne(targetEntity="Plugin", inversedBy="pagePlugin", cascade={"persist"})
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="plugin_id", referencedColumnName="plugin_id", onDelete="CASCADE")
 * })
 */
private $plugin;

迭代结果时,链接到 PagePlugins 的 Plugin 实体没有名称:

$pagePlugin->getPlugin()
DoctrineProxies\__CG__\Plugin::__set_state(array(
   '__initializer__' => 
  Closure::__set_state(array(
  )),
   '__cloner__' => 
  Closure::__set_state(array(
  )),
   '__isInitialized__' => false,
   'pluginId' => 7,
   'pluginName' => '',
   'pagePlugin' => NULL,
   'pluginMappingValue' => '',
   'allowedPlugin' => 0,
   'mainEntity' => 0,
   'autoCompleteIgnorePlugin' => 0,
))

当我刷新对象时:

        $ref = $pagePlugin->getPlugin();
        $test = ORM::getDefaultOrm();
        $test->refresh($ref);

将提取姓名:

$ref = {DoctrineProxies\__CG__\Plugin} [11]
 lazyPropertiesDefaults = {array} [0]
 __initializer__ = {Closure} [3]
 __cloner__ = {Closure} [3]
 __isInitialized__ = true
 *Plugin*pluginId = 7
 *Plugin*pluginName = "Header"
 *Plugin*pagePlugin = {Doctrine\ORM\PersistentCollection} [9]
 *Plugin*pluginMappingValue = "Header"
 *Plugin*allowedPlugin = null
 *Plugin*mainEntity = null
 *Plugin*autoCompleteIgnorePlugin = null

是否可以在 DQL 中指定 Doctrine 必须完全获取插件以及为什么 Doctrine 只获取该对象的一部分?

如果您也想获取 Plugin,您需要执行所谓的获取连接。您可以阅读 fetch joining here in the Doctrine2 documentation chapter 14.2.2. Joins:

Fetch Joins: In addition to the uses of regular joins: Used to fetch related entities and include them in the hydrated result of a query.

在您的情况下,您应该使用查询本身来执行提取连接:

$this->orm->createQuery('SELECT pp, p FROM PagePlugin pp JOIN pp.plugin p WHERE pp.page = :page')->setParameter('page', $page)->getResult();