Cms: Querying for relatedTo category causes Yii error when entry's category isn't set

Created on 18 Nov 2019  Â·  4Comments  Â·  Source: craftcms/cms

Description

Current entry's search for other entries with related categories fails if the current entry doesn't have anything selected for those categories.

Steps to reproduce

My page has this query that looks for logos related to the page:

{% set logoEntries = craft.entries
  .with([
    ['featuredImage', { withTransforms: ['thumbFit'] }],
  ])
  .site(entry.site.handle)
  .section('logos')
  .relatedTo([
    entry.categoryIndustry,
    entry.categorySolution,
    entry.categoryProduct
  ])
  .limit(20)
  .all()
%}

If the page's entry has all its categories set (categoryIndustry, categorySolution, categoryProduct), then the page will render fine. If the entry has no selected value for one or more of those categories, then it gives this error:

PHP Warning – yii\base\ErrorException
array_push() expects at least 2 parameters, 1 given
1. in /var/www/usi-craftcms/vendor/craftcms/cms/src/elements/db/ElementRelationParamParser.php at line 216

                    $relElementIds[] = $element->getSourceId();
                } else {
                    $relElementIds[] = $element->id;
                    if ($elementParam === 'element') {
                        $relSourceElementIds[] = $element->getSourceId();
                    }
                }
            } else if ($element instanceof ElementQueryInterface) {
                $ids = $element->ids();
                array_push($relElementIds, ...$ids);
                if ($elementParam === 'element') {
                    array_push($relSourceElementIds, ...$ids);
                }
            }
        }

        break;
    }
}

Additional info

This worked fine in Craft 3.3.13. The error appeared when I updated to 3.3.15. (And reverting to 3.3.13 makes everything work again.)

  • Craft version: 3.3.15
  • PHP version: 7.2.21
  • Database driver & version: MySQL 5.7.27
  • Plugins & versions:
    Element Map 1.2.1
    Feed Me 4.1.2
    Field Reveal 1.0.1
    Fractal 1.0.8
    Logs 3.0.2
    Matrix Toolbar 1.0.6
    Recaptcha Verify 1.0.0
    Redactor 2.4.0
    Redirect plugin for Craft 3 1.0.23
    SEOmatic 3.2.29
    Smith 1.0.0
    Snitch 3.0.0
    Workflow 1.2.3

Most helpful comment

Thanks for reporting that. It’s now fixed for the next release.

To get the fix early, change your craftcms/cms requirement in composer.json to:

"require": {
  "craftcms/cms": "dev-develop#a4c5a329b5cc0667ba236c15ea67afc091a7aa37 as 3.3.15",
  "...": "..."
}

Then run composer update.

All 4 comments

I've ran into this same issue a couple of times over the past weeks and I've found that this does work in PHP 7.3 (see changelog here).

I checked the server requirements page, which says PHP 7.0+. Should it say PHP 7.3+ now?
And regardless of the PHP version, it was working fine in Craft 3.3.13 (I ended up not upgrading to 3.3.15 until I got more guidance here).

Looks like this got introduced in this commit https://github.com/craftcms/cms/commit/67e2880f35fb08fe565631c6040bc5f9e7c272a0, which got released with 3.3.14

Thanks for reporting that. It’s now fixed for the next release.

To get the fix early, change your craftcms/cms requirement in composer.json to:

"require": {
  "craftcms/cms": "dev-develop#a4c5a329b5cc0667ba236c15ea67afc091a7aa37 as 3.3.15",
  "...": "..."
}

Then run composer update.

Was this page helpful?
0 / 5 - 0 ratings