contao/calendar-bundle

Loading models eagerly

Closed this issue · 2 comments

Now that @fritzmg has debugged the memory issues caused by eagerly loaded models, I want to discuss eager loading in general. Take this code for example:

// Link to an article
case 'article':
if (($objArticle = \ArticleModel::findByPk($objEvent->articleId, array('eager'=>true))) !== null && ($objPid = $objArticle->getRelated('pid')) instanceof PageModel)
{
/** @var PageModel $objPid */
self::$arrUrlCache[$strCacheKey] = ampersand($objPid->getFrontendUrl('/articles/' . ($objArticle->alias ?: $objArticle->id)));
}
break;

With array('eager'=>true) we instruct Contao to also load all related models. Here's what we get:

  • ArticleModel
  • PageModel (pid)
  • UserModel (author)

However, we are not using the UserModel in this case, so we would not have needed to create it. On the other hand, using array('eager'=>true) has saved us one additional database query.

@contao/developers Would you prefer having more database queries and no unneeded models over having less database queries and potentially unneeded models?

As discussed in Mumble on July 5th, we should generally disable eager loading in DCAs ('load'=>'eager') and never load collections eagerly. In other cases like above, it might be helpful.

Changed where applicable (see commits above).