Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested eager load throws error when nested tier is missing #2708

Closed
evanwarner opened this issue Apr 8, 2018 · 3 comments
Closed

Nested eager load throws error when nested tier is missing #2708

evanwarner opened this issue Apr 8, 2018 · 3 comments

Comments

@evanwarner
Copy link
Contributor

Description

I'm executing a nested eager load: querying for a list of entries, eager loading the user from a specific field, and also that user's profile photo.

{% set recentPosts = craft.entries({
  section: 'blogPosts',
  with: ['guestAuthor.photo'],
  limit: 5,
}) %}

If any of the users don't have a profile photo, I get the following error:
TypeError Argument 1 passed to craft\helpers\StringHelper::toLowerCase() must be of the type string, null given, called in [...]/vendor/craftcms/cms/src/helpers/Db.php on line 448

I'd expect to be able to just handle a missing photo in my template code after the query executes.

Steps to reproduce

  1. Create an entry
  2. Add a Users field to the entry and select a user that does not have a photo
  3. Query for that entry, and eager load the user and their photo

Additional info

  • Craft version: 3.0.1
  • PHP version: 7.1.12
  • Database driver & version: MySQL 5.6.38
  • Plugins & versions: CodeMirror 1.0.0, Element API 2.5.2, GatherContent 1.0.5, Redactor 1.1.0

Stack Trace

TypeError: Argument 1 passed to craft\helpers\StringHelper::toLowerCase() must be of the type string, null given, called in [...]/vendor/craftcms/cms/src/helpers/Db.php on line 448 and defined in [...]/vendor/craftcms/cms/src/helpers/StringHelper.php:742
Stack trace:
#0 [...]/vendor/craftcms/cms/src/helpers/Db.php(448): craft\helpers\StringHelper::toLowerCase(NULL)
#1 [...]/vendor/craftcms/cms/src/elements/db/ElementQuery.php(912): craft\helpers\Db::parseParam('elements.id', Array)
#2 [...]/vendor/yiisoft/yii2/db/QueryBuilder.php(227): craft\elements\db\ElementQuery->prepare(Object(craft\db\mysql\QueryBuilder))
#3 [...]/vendor/yiisoft/yii2/db/Query.php(146): yii\db\QueryBuilder->build(Object(craft\elements\db\AssetQuery))
#4 [...]/vendor/yiisoft/yii2/db/Query.php(237): yii\db\Query->createCommand(Object(craft\db\Connection))
#5 [...]/vendor/craftcms/cms/src/db/Query.php(155): yii\db\Query->all(NULL)
#6 [...]/vendor/craftcms/cms/src/elements/db/ElementQuery.php(1011): craft\db\Query->all(NULL)
#7 [...]/vendor/craftcms/cms/src/services/Elements.php(1232): craft\elements\db\ElementQuery->all()
#8 [...]/vendor/craftcms/cms/src/elements/db/ElementQuery.php(1969): craft\services\Elements->eagerLoadElements('craft\\elements\\...', Array, Array)
#9 [...]/vendor/craftcms/cms/src/elements/db/ElementQuery.php(985): craft\elements\db\ElementQuery->_createElements(Array)
#10 [...]/vendor/yiisoft/yii2/db/Query.php(238): craft\elements\db\ElementQuery->populate(Array)
#11 [...]/vendor/craftcms/cms/src/db/Query.php(155): yii\db\Query->all(NULL)
#12 [...]/vendor/craftcms/cms/src/elements/db/ElementQuery.php(1011): craft\db\Query->all(NULL)
#13 [...]/vendor/twig/twig/lib/Twig/Extension/Core.php(1605): craft\elements\db\ElementQuery->all()
#14 [...]/vendor/craftcms/cms/src/helpers/Template.php(73): twig_get_attribute(Object(craft\web\twig\Environment), Object(Twig_Source), Object(craft\elements\db\EntryQuery), 'all', Array, 'method', false, false)
#15 [...]/storage/runtime/compiled_templates/a0/a0c29669680e97ad9713d130dae9ed8ca4989d1a509f9c7dcbc2deca0d524e23.php(137): craft\helpers\Template::attribute(Object(craft\web\twig\Environment), Object(Twig_Source), Object(craft\elements\db\EntryQuery), 'all', Array, 'method')
#16 [...]/vendor/twig/twig/lib/Twig/Template.php(189): __TwigTemplate_678037ad78e0639e31fd7c649f320cdefaaaf12c29f3fb3fe2d69985fb630674->block_content(Array, Array)
#17 [...]/storage/runtime/compiled_templates/30/3061c03a1f5b2a1308a1fb6febe98702174428db178d73977d812ac0cd97d35c.php(113): Twig_Template->displayBlock('content', Array, Array)
#18 [...]/vendor/twig/twig/lib/Twig/Template.php(390): __TwigTemplate_9965976e6d9afcb496b44c889613bedb125f2eb34ed35ba3bf59d0827eb212f4->doDisplay(Array, Array)
#19 [...]/vendor/craftcms/cms/src/web/twig/Template.php(49): Twig_Template->displayWithErrorHandling(Array, Array)
#20 [...]/vendor/twig/twig/lib/Twig/Template.php(367): craft\web\twig\Template->displayWithErrorHandling(Array, Array)
#21 [...]/vendor/craftcms/cms/src/web/twig/Template.php(31): Twig_Template->display(Array, Array)
#22 [...]/storage/runtime/compiled_templates/a0/a0c29669680e97ad9713d130dae9ed8ca4989d1a509f9c7dcbc2deca0d524e23.php(32): craft\web\twig\Template->display(Array, Array)
#23 [...]/vendor/twig/twig/lib/Twig/Template.php(390): __TwigTemplate_678037ad78e0639e31fd7c649f320cdefaaaf12c29f3fb3fe2d69985fb630674->doDisplay(Array, Array)
#24 [...]/vendor/craftcms/cms/src/web/twig/Template.php(49): Twig_Template->displayWithErrorHandling(Array, Array)
#25 [...]/vendor/twig/twig/lib/Twig/Template.php(367): craft\web\twig\Template->displayWithErrorHandling(Array, Array)
#26 [...]/vendor/craftcms/cms/src/web/twig/Template.php(31): Twig_Template->display(Array, Array)
#27 [...]/vendor/twig/twig/lib/Twig/Template.php(375): craft\web\twig\Template->display(Array)
#28 [...]/vendor/twig/twig/lib/Twig/Environment.php(289): Twig_Template->render(Array)
#29 [...]/vendor/craftcms/cms/src/web/View.php(305): Twig_Environment->render('blog/_index', Array)
#30 [...]/vendor/craftcms/cms/src/web/View.php(352): craft\web\View->renderTemplate('blog/_index', Array)
#31 [...]/vendor/craftcms/cms/src/web/Controller.php(113): craft\web\View->renderPageTemplate('blog/_index', Array)
#32 [...]/vendor/craftcms/cms/src/controllers/TemplatesController.php(78): craft\web\Controller->renderTemplate('blog/_index', Array)
#33 [internal function]: craft\controllers\TemplatesController->actionRender('blog/_index', Array)
#34 [...]/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#35 [...]/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#36 [...]/vendor/craftcms/cms/src/web/Controller.php(74): yii\base\Controller->runAction('render', Array)
#37 [...]/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction('render', Array)
#38 [...]/vendor/craftcms/cms/src/web/Application.php(238): yii\base\Module->runAction('templates/rende...', Array)
#39 [...]/vendor/yiisoft/yii2/web/Application.php(103): craft\web\Application->runAction('templates/rende...', Array)
#40 [...]/vendor/craftcms/cms/src/web/Application.php(227): yii\web\Application->handleRequest(Object(craft\web\Request))
#41 [...]/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
#42 [...]/web/index.php(21): yii\base\Application->run()
#43 {main}
@evanwarner
Copy link
Contributor Author

I should note that the query is fine if any of the posts are missing an assigned user, so long as all of the users that are assigned have photos.

@evanwarner
Copy link
Contributor Author

With a little more experimentation, it seems like user photos might be a special field, not treated like a normal Asset field. So perhaps my assumption that I could eager load the image asset is incorrect.

@brandonkelly
Copy link
Member

Yep users do support eager-loading their photos. That error should be fixed for the next release. Thanks for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants