Skip to content

[4.x]: Method craft\elements\ElementCollection::status does not exist - eager-loading ancestors and deleting categories #16722

@engram-design

Description

@engram-design

What happened?

Description

Related to verbb/zen#22 there seems to be an issue with deleting a category (and likely other elements) when eager-loading their ancestors.

This seems to stem from the beforeDelete function, specifically getAncestors(1) returning a craft\elements\ElementCollection and not a craft\elements\db\CategoryQuery when eager-loaded.

For example:

foreach (Category::find()->all() as $category) {
    // For easy testing, we're checking what `beforeDelete()` would perform
    Craft::dd($this->getAncestors(1));

    // The actual error can be raised be calling `deleteElement()`
    Craft::$app->getElements()->deleteElement($category);
}

Runs as expected with no errors. I've added a debug statement to help clarify the issue, rather then get into deleteElement() with a getAncestors(1) call, which happens in deleteElement() anyway.

In this scenario, the ancestor is a craft\elements\db\CategoryQuery object.

Now, adding eager-loading for the ancestor throws an error.

foreach (Category::find()->with(['ancestors'])->all() as $category) {
    Craft::dd($this->getAncestors(1));
}

This returns a craft\elements\ElementCollection object, which when trying to query with, is throwing issues.

Is this expected behaviour?

Craft CMS version

4.14.5

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions