简体   繁体   中英

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

$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.

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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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