nystudio107 / craft-similar

Similar for Craft lets you find elements, Entries, Categories, Commerce Products, etc, that are similar, based on... other related elements.
https://nystudio107.com/
MIT License
26 stars 5 forks source link

Cannot assign null to property nystudio107\similar\services\Similar::$preOrder of type array|string #49

Closed johnj24 closed 6 months ago

johnj24 commented 1 year ago

Question

At some point in the last few weeks, the code below stopped working and started to show this error:

Cannot assign null to property nystudio107\similar\services\Similar::$preOrder of type array|string

None of the variables set up below return null that I can tell. I recently started 'entrifying' category groups on this site but since I'm not asking for category ids here, I don't think it's an issue -- just adding for context. I haven't 'entryfied' tags yet.

{% set limitCriteria = craft.entries()
  .section('recipes')
  .limit(8) %}

{% set tagIds = entry.recipeTags.ids() %}

{% set contextIds = tagIds %}
{% set similarEntries = craft.similar.find({ element: entry, context: contextIds, criteria: limitCriteria }) %}

Stack Trace

TypeError: Cannot assign null to property nystudio107\similar\services\Similar::$preOrder of type array|string in /vendor/nystudio107/craft-similar/src/services/Similar.php:87
Stack trace:
#0 /vendor/nystudio107/craft-similar/src/variables/SimilarVariable.php(35): nystudio107\similar\services\Similar->find(Array)
#1 /vendor/twig/twig/src/Extension/CoreExtension.php(1607): nystudio107\similar\variables\SimilarVariable->find(Array)
#2 /vendor/craftcms/cms/src/helpers/Template.php(146): twig_get_attribute(Object(craft\web\twig\Environment), Object(Twig\Source), Object(nystudio107\similar\variables\SimilarVariable), 'find', Array, 'method', false, false)
#3 /storage/runtime/compiled_templates/36/36ff2ce91f108e73bdc1c7a638b82dd72f2fcf9a818e1a012512d218cc1e3530.php(257): craft\helpers\Template::attribute(Object(craft\web\twig\Environment), Object(Twig\Source), Object(nystudio107\similar\variables\SimilarVariable), 'find', Array, 'method')
#4 /vendor/twig/twig/src/Template.php(394): __TwigTemplate_42e9e8e5003d0c5963abbc8878526910fe78c4c3a59c08d06a5cbcc32eef8efa->doDisplay(Array, Array)
#5 /vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#6 /vendor/twig/twig/src/Template.php(379): Twig\Template->display(Array)
#7 /vendor/twig/twig/src/TemplateWrapper.php(40): Twig\Template->render(Array, Array)
#8 /vendor/twig/twig/src/Extension/CoreExtension.php(1323): Twig\TemplateWrapper->render(Array)
#9 /storage/runtime/compiled_templates/f1/f16473b511deeaee7b14f59d24763d20ca5eeace5090d9a170192ea33344e6af.php(44): twig_include(Object(craft\web\twig\Environment), Array, Array)
#10 /vendor/twig/twig/src/Template.php(394): __TwigTemplate_2ed01dc25998dd8d3f7d0d9c9595043b7c98e518e6e412c99c94bfc5efc636ab->doDisplay(Array, Array)
#11 /vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#12 /vendor/twig/twig/src/Template.php(379): Twig\Template->display(Array)
#13 /vendor/twig/twig/src/TemplateWrapper.php(40): Twig\Template->render(Array, Array)
#14 /vendor/twig/twig/src/Extension/CoreExtension.php(1323): Twig\TemplateWrapper->render(Array)
#15 /storage/runtime/compiled_templates/3a/3a11954bdb5cc49e14d2069c5267eabbb0267089c9b620e91ef82a74d85f5f8b.php(54): twig_include(Object(craft\web\twig\Environment), Array, '_routers/detail', Array)
#16 /vendor/twig/twig/src/Template.php(171): __TwigTemplate_8796a094a7a72c244ea1e7f067cc6d943c895f0478eeff8057e95b962e13635f->block_main(Array, Array)
#17 /storage/runtime/compiled_templates/45/45860eba8627918b8d7441f78cb552c20815b47e871cb4051195515f36140ac3.php(174): Twig\Template->displayBlock('main', Array, Array)
#18 /vendor/twig/twig/src/Template.php(171): __TwigTemplate_ecb24fb42481ae2f1820b20572f74a67db1b2e13525cb4f953f90ef3fc876fd9->block_body(Array, Array)
#19 /storage/runtime/compiled_templates/34/34b859337903191f3802062097c497cdc03518490b1e5f3790d24dda45d187ec.php(90): Twig\Template->displayBlock('body', Array, Array)
#20 /vendor/twig/twig/src/Template.php(394): __TwigTemplate_5f4efc1f9af3f702e8105cd852b3f95ee60151ea0ef47bcc6ed2997c541ccccf->doDisplay(Array, Array)
#21 /vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#22 /storage/runtime/compiled_templates/45/45860eba8627918b8d7441f78cb552c20815b47e871cb4051195515f36140ac3.php(49): Twig\Template->display(Array, Array)
#23 /vendor/twig/twig/src/Template.php(394): __TwigTemplate_ecb24fb42481ae2f1820b20572f74a67db1b2e13525cb4f953f90ef3fc876fd9->doDisplay(Array, Array)
#24 /vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#25 /storage/runtime/compiled_templates/3a/3a11954bdb5cc49e14d2069c5267eabbb0267089c9b620e91ef82a74d85f5f8b.php(43): Twig\Template->display(Array, Array)
#26 /vendor/twig/twig/src/Template.php(394): __TwigTemplate_8796a094a7a72c244ea1e7f067cc6d943c895f0478eeff8057e95b962e13635f->doDisplay(Array, Array)
#27 /vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling(Array, Array)
#28 /vendor/twig/twig/src/Template.php(379): Twig\Template->display(Array)
#29 /vendor/twig/twig/src/TemplateWrapper.php(40): Twig\Template->render(Array, Array)
#30 /vendor/twig/twig/src/Environment.php(277): Twig\TemplateWrapper->render(Array)
#31 /vendor/craftcms/cms/src/web/View.php(456): Twig\Environment->render('_routers/page.t...', Array)
#32 /vendor/craftcms/cms/src/web/View.php(509): craft\web\View->renderTemplate('_routers/page.t...', Array)
#33 /vendor/craftcms/cms/src/web/TemplateResponseFormatter.php(56): craft\web\View->renderPageTemplate('_routers/page.t...', Array, 'site')
#34 /vendor/yiisoft/yii2/web/Response.php(1098): craft\web\TemplateResponseFormatter->format(Object(craft\web\Response))
#35 /vendor/craftcms/cms/src/web/Response.php(286): yii\web\Response->prepare()
#36 /vendor/yiisoft/yii2/web/Response.php(339): craft\web\Response->prepare()
#37 /vendor/yiisoft/yii2/base/Application.php(390): yii\web\Response->send()
#38 /public/index.php(21): yii\base\Application->run()
#39 {main}

Additional context

Craft CMS: 4.4.9 Similar: 4.0.0

khalwat commented 1 year ago

If I had to guess, probably entry is null here at some point, for some reason.

johnj24 commented 1 year ago

The code snippet I shared above lives on an entry page, so I would be surprised if it were null.

Doesn't the following suggest nothing is null?

3. in /vendor/twig/twig/src/Extension/CoreExtension.php at line 1607– nystudio107\similar\variables\SimilarVariable::find(['element' => craft\elements\Entry, 'context' => ['49913', '22', '207', '75028'], 'criteria' => craft\elements\db\EntryQuery])
khalwat commented 1 year ago

mmmm yes it does... here's where it is going wrong (somehow):

        // Get an ElementQuery for this Element
        $elementClass = is_object($element) ? $element::class : $element;
        /** @var EntryQuery $query */
        $query = $this->getElementQuery($elementClass, $criteria);

        // Stash any orderBy directives from the $query for our anonymous function
        $this->preOrder = $query->orderBy;
khalwat commented 1 year ago

Somehow $query->orderBy is coming back as null

Here's what getElementQuery() does:

    protected function getElementQuery(string|ElementInterface $elementType, array $criteria): ElementQueryInterface
    {
        /** @var string|ElementInterface $elementType */
        $query = $elementType::find();
        Craft::configure($query, $criteria);

        return $query;
    }
khalwat commented 1 year ago

Try changing this:

        // Stash any orderBy directives from the $query for our anonymous function
        $this->preOrder = $query->orderBy;

to this:

        // Stash any orderBy directives from the $query for our anonymous function
        $this->preOrder = $query->orderBy ?? [];

...and see if that doesn't solve the issue for you?

johnj24 commented 1 year ago

That solved it. Thanks, Andrew.

khalwat commented 6 months ago

Addressed in the above commits