简体   繁体   English

预装主义ODM参考文档

[英]Preloading Doctrine ODM referenced documents

I'm using Doctrine ODM (MongoDB) and I have something like the following 我正在使用Doctrine ODM(MongoDB),并且有类似以下内容的信息

  • Attendee document Attendee文件
  • Answer document Answer文件
  • each Answer references one Attendee . 每个Answer引用一个Attendee (one-to-many relation) (一对多关系)

I do something like the following 我做类似以下的事情

attendees = attendeeRepo.findby(whatever)

foreach(attendees as attendee)
    answers = attendee.getAnswers()

The problem here is that on each attendee.getAnswers() Doctrine ODM executes a query like that: 这里的问题是,在每个attendee.getAnswers() Doctrine ODM都会执行如下查询:

db.TicketAnswer.find({ 
    "attendee.$id": ObjectId("50ae80608ead0ea71e00008b") 
})

if I have 100 atendees, 100 queries like this one will get executed, so I was trying to preload all Answers before the loop. 如果我有100位参加者,那么将执行100个这样的查询,因此我试图在循环之前预加载所有Answers but it didn't work as I expected. 但是它没有按我预期的那样工作。

I guess it didn't work as I was expecting as this is a one-to-many relation and I asked the one side of the relation ( Attendee ) to load the many ( Answer ). 我想这不像我期望的那样有效,因为这是一对多的关系,因此我要求关系的一侧( Attendee )加载许多关系( Answer )。 in other cases when I'm doing the opposite, preloading works as expected. 在其他情况下,当我做相反的事情时,预加载可以按预期进行。

I'm not sure what's the best solution for a case like that. 我不确定哪种情况下最好的解决方案是什么。 wish if anyone has ideas that can help. 希望有人能提供帮助的想法。

Check this article out: http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/priming-references.html 查阅本文: http : //docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/priming-references.html

To quote: 报价:

/** @Document */
class User
{
    /** @ReferenceMany(targetDocument="Account") */
    private $accounts;
}

- --

$qb = $dm->createQueryBuilder('User')
    ->field('accounts')->prime(true)
    ->limit(100);
$query = $qb->getQuery();
$users = $query->execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM