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
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
beforeDeletefunction, specificallygetAncestors(1)returning acraft\elements\ElementCollectionand not acraft\elements\db\CategoryQuerywhen eager-loaded.For example:
Runs as expected with no errors. I've added a debug statement to help clarify the issue, rather then get into
deleteElement()with agetAncestors(1)call, which happens indeleteElement()anyway.In this scenario, the ancestor is a
craft\elements\db\CategoryQueryobject.Now, adding eager-loading for the ancestor throws an error.
This returns a
craft\elements\ElementCollectionobject, 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