简体   繁体   English

Propel populateRelation一对多关系不会填充关系

[英]Propel populateRelation one-to-many relation doesn't fill relation

I have a problem with populateRelation , lets explain, that we have some Article, this Article can have tags, so we do another table article_tag, this do ArticleTag object in propel, lets find the all Articles 我有一个问题, populateRelation ,让我们解释一下,我们有一些文章,这篇文章可以有标签,所以我们做另一个表article_tag,这个做ArticleTag对象的推进,让我们找到所有的文章

$Articles = ArticleQuery::create()->find();

okey so we have now all Articles, now we wanna have all of those tags for each Article, so here we go: 所以我们现在有了所有文章,现在我们想要为每篇文章提供所有这些标签,所以我们在这里:

$Articles->populateRelation('ArticleTag');

and then is some view we do: 然后是我们做的一些观点:

foreach($Articles as $Article) {
  // some stuff
  foreach($Article->getArticleTags() as $ArticleTag) { // this should not do query!
    // some additional stuff
  }
}

and when I have 1 200 articles in db, with populateRelation the queries going down up to 600, but should be only 2, for articles and for articles tags. 当我在db中有1 200篇文章时,使用populateRelation查询最多为600,但对于文章和文章标签应该只有2。

Where I do mistake? 我哪里弄错了?

I put an answer here about recursively populate related objects. 在这里给出一个关于递归填充相关对象的答案

You just have to explicit joinWith the table in your query: 您只需要在查询中显式joinWith

$articles = ArticleQuery::create()->joinWith('Article.ArticleTag')->find();

// this won't perform others queries
// not sure about getArticleTags or getArticleTag
foreach($articles->getArticleTags() as $ArticleTag) { }

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

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