学说中的数据急切加载 mongodb

Data eager loading in doctrine mongodb

对不起我的英语。我似乎在 mongo 的教义工作中遗漏了一些东西。我有

/**
 * @MongoDB\MappedSuperclass
 * @MongoDB\DiscriminatorField("type")
 * @MongoDB\DiscriminatorMap({
 *   "hotel"="App\Document\Attributes\HotelAttributes",
 *   "apartment"="App\Document\Attributes\ApartmentAttributes",
*/
abstract class AbstractAttribute 
{
    /**
     * @MongoDB\Id
     * @MongoDB\UniqueIndex()
     */
    protected string $id;
    ...
}

还有几个 class 的后代,例如:

namespace App\Document\Attributes;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
 * @MongoDB\Document(collection="attributes")
 * @MongoDB\InheritanceType("SINGLE_COLLECTION")
 * @MongoDB\HasLifecycleCallbacks
 */
class HotelAttributes extends AbstractAttribute
{
    /**
     * @MongoDB\Field(
     *     type="int",
     *     nullable=false,
     *     name="bed_places",
     * )
     */
    protected int $bedPlaces;

以及引用属性

的资源class
class Resources
{
    /**
     * @MongoDB\ReferenceOne(
     *     targetDocument=AbstractAttribute::class,
     *     storeAs="dbRefWithDb",
     *     orphanRemoval=true,
     *     cascade={"all"}
     * )
     */
    protected AbstractAttribute $attributes;
    ...
}

资源是正常创建的,但是当通过id获取时,直到我尝试直接访问属性时,属性字段才被加载,即.:

$resource = $this->dm->getRepository(Resources::class)->find($id);
$resource->getAttributes() will return:

twig dump

$resource->getAttributes()->getCreated() - 将 return 创建日期和所有字段。

我有两个问题:

  1. 如何让学说自动加载所有数据?
  2. 如何在我的示例中为摘要 class 指定存储库?

感谢您的帮助。

ODM 中最接近 fetch="EAGER" 的是称为“启动引用”的过程。与 ORM 的 EAGER 相反,这将进行两个数据库查询(一个用于您正在查找的临时文档,另一个用于它们的原始引用),因为没有可行的方法来实现类似 JOIN 的提取。您可以在文档中阅读有关启动的更多信息:https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/2.2/reference/priming-references.html