如何在Symfony中使用Doctrine MongoDB ODM急切加载?

时间:2019-03-24 11:53:03

标签: php symfony doctrine doctrine-odm doctrine-mongodb

我有2个课程,我想使用急切的加载方式:

/**
 * @MongoDB\Document(collection="wallet")
 */
class Wallet
{
    /**
     * @MongoDB\ReferenceMany(targetDocument="WalletEntry", mappedBy="wallet")
     */
    protected $entries;
}    

/**
 * @MongoDB\Document(collection="wallet_entry")
 */
class WalletEntry
{
    /**
     * @MongoDB\ReferenceOne(targetDocument="Wallet", storeAs="id")
     */
    protected $wallet;
}
  1. 此代码使用延迟加载并生成n + 1个查询:
$wallets = $doctrine->getRepository('App:Wallet')->findAll();

foreach ($wallets as $wallet) {
    foreach ($wallet->entries as $entry) {
        // ...
    }
}
  1. 此代码生成错误Field "entries" is not the owning side of a reference relationship in class "App\Document\Wallet"
$wallets = $doctrine->createQueryBuilder('App:Wallet')
    ->field('entries')->prime(true)
    ->getQuery()
    ->execute();

如何使用紧急加载和仅2个查询将钱包及其条目一起加载?我没有在Wallet文档中保留任何引用,因为相应的WalletEntry文档的数量可能很大。

0 个答案:

没有答案