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

[5.x]: Invalid owner id on isElementDraftOrRevision() call #16359

Closed
zsavajji opened this issue Dec 27, 2024 · 4 comments
Closed

[5.x]: Invalid owner id on isElementDraftOrRevision() call #16359

zsavajji opened this issue Dec 27, 2024 · 4 comments
Labels

Comments

@zsavajji
Copy link

What happened?

Hi, we are building a multi-site website with Craft, we're in development since beta, and we're now trying to propagate entries to a new site instance, since we're using sites as per country variations.
I wrote a custom module, which i successfully used in Craft 4.x to propagate entries from a given site to a target one, it works ok for a lot of entries, but some Matrix fields throw "Invalid owner ID" when invoking ElementHelper::isDraftOrRevision().

The behavior is unrelated to the element save phase.

I also got for one particular entry the Call to a member function getFieldById() on null error, and it seems related to the missing owner id error, since it comes from NestedElementTrait.php:243 class in getField(), which errors on $this->getOwner()?->getFieldLayout()->getFieldById($this->fieldId)

Do you have any idea on what could be going on and on how to correct this behavior?
The customer has already done a big part of the data entry and database corruption is really suboptimal at this stage.

Thanks in advance!

yii\base\InvalidConfigException: Invalid owner ID: 253 in /Users/andreafaggin/projects/lnproject-be/vendor/craftcms/cms/src/base/NestedElementTrait.php:166
Stack trace:
#0 /Users/andreafaggin/projects/lnproject-be/vendor/craftcms/cms/src/base/NestedElementTrait.php(211): craft\elements\Entry->getPrimaryOwner()
#1 /Users/andreafaggin/projects/lnproject-be/vendor/craftcms/cms/src/helpers/ElementHelper.php(553): craft\elements\Entry->getOwner()
#2 /Users/andreafaggin/projects/lnproject-be/vendor/craftcms/cms/src/helpers/ElementHelper.php(555): craft\helpers\ElementHelper::isDraftOrRevision(Object(craft\elements\Entry))
#3 /Users/andreafaggin/projects/lnproject-be/modules/sitepropagations/console/controllers/PropagateController.php(90): craft\helpers\ElementHelper::isDraftOrRevision(Object(craft\elements\Entry))
#4 [internal function]: modules\sitepropagations\console\controllers\PropagateController->actionCopyLocales()
#5 /Users/andreafaggin/projects/lnproject-be/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#6 /Users/andreafaggin/projects/lnproject-be/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#7 /Users/andreafaggin/projects/lnproject-be/vendor/yiisoft/yii2/console/Controller.php(180): yii\base\Controller->runAction('copy-locales', Array)
#8 /Users/andreafaggin/projects/lnproject-be/vendor/craftcms/cms/src/console/ControllerTrait.php(88): yii\console\Controller->runAction('copy-locales', Array)
#9 /Users/andreafaggin/projects/lnproject-be/vendor/craftcms/cms/src/console/Controller.php(216): craft\console\Controller->traitRunAction('copy-locales', Array)
#10 /Users/andreafaggin/projects/lnproject-be/vendor/yiisoft/yii2/base/Module.php(552): craft\console\Controller->runAction('copy-locales', Array)
#11 /Users/andreafaggin/projects/lnproject-be/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('site-propagatio...', Array)
#12 /Users/andreafaggin/projects/lnproject-be/vendor/craftcms/cms/src/console/Application.php(91): yii\console\Application->runAction('site-propagatio...', Array)
#13 /Users/andreafaggin/projects/lnproject-be/vendor/yiisoft/yii2/console/Application.php(147): craft\console\Application->runAction('site-propagatio...', Array)
#14 /Users/andreafaggin/projects/lnproject-be/vendor/craftcms/cms/src/console/Application.php(122): yii\console\Application->handleRequest(Object(craft\console\Request))
#15 /Users/andreafaggin/projects/lnproject-be/vendor/yiisoft/yii2/base/Application.php(384): craft\console\Application->handleRequest(Object(craft\console\Request))
#16 /Users/andreafaggin/projects/lnproject-be/craft(13): yii\base\Application->run()

Craft CMS version

5.x, currently latest 5.5.7

PHP version

8.3

Operating system and version

Staging: Debian 12 - dev: macOS 15.2

Database type and version

Staging: MariaDB 10.6 - dev MariaDB: 11.6

Image driver and version

No response

Installed plugins and versions

No response

@zsavajji zsavajji added the bug label Dec 27, 2024
@zsavajji
Copy link
Author

zsavajji commented Dec 27, 2024

A little debug later and I see that some of these entries with supposedly invalid ownership have really no primaryOwnerId, some others have a primaryOwnerId which points to another site.

Image

This happens before running any of the custom module logics.

Some other instead have no parent at all, in no available sites

Image

@brandonkelly
Copy link
Member

Just updated craft\base\NestedElementTrait::getOwner() and getPrimaryOwner() to return the owner element even if it doesn’t exist in the same site, for the next release.

You can test by changing your craftcms/cms requirement in composer.json to:

"craftcms/cms": "5.x-dev as 5.5.7",

Then run composer update.

Let me know if that helps.

@zsavajji
Copy link
Author

Thank you for your quick response!
This seems to have fixed the issue about the ownership, but i now get some more errors

craft\errors\UnsupportedSiteException: Attempting to save an element in an unsupported site. in /Users/andreafaggin/projects/lnproject-be/vendor/craftcms/cms/src/services/Elements.php:3518
Stack trace:
#0 /Users/andreafaggin/projects/lnproject-be/vendor/craftcms/cms/src/services/Elements.php(1303): craft\services\Elements->_saveElementInternal(Object(craft\elements\Entry), true, false, true, Array, false, false, false)
#1 /Users/andreafaggin/projects/lnproject-be/modules/sitepropagations/console/controllers/PropagateController.php(198): craft\services\Elements->saveElement(Object(craft\elements\Entry), true, false, true)
...
...
#19 {main}

A lot of this errors get printed on some Matrix entries, and then the process dies with

Exception 'Error' with message 'Call to a member function getFieldById() on null'

in /Users/andreafaggin/projects/lnproject-be/vendor/craftcms/cms/src/base/NestedElementTrait.php:271

Stack trace:
#0 /Users/andreafaggin/projects/lnproject-be/vendor/craftcms/cms/src/elements/Entry.php(1017): craft\elements\Entry->getField()
#1 /Users/andreafaggin/projects/lnproject-be/vendor/craftcms/cms/src/helpers/ElementHelper.php(315): craft\elements\Entry->getSupportedSites()
#2 /Users/andreafaggin/projects/lnproject-be/vendor/craftcms/cms/src/elements/NestedElementManager.php(309): craft\helpers\ElementHelper::supportedSitesForElement(Object(craft\elements\Entry))
#3 /Users/andreafaggin/projects/lnproject-be/vendor/craftcms/cms/src/fields/Matrix.php(548): craft\elements\NestedElementManager->getSupportedSiteIds(Object(craft\elements\Entry))
#4 /Users/andreafaggin/projects/lnproject-be/vendor/craftcms/cms/src/elements/Entry.php(1017): craft\fields\Matrix->getSupportedSitesForElement(Object(craft\elements\Entry))
#5 /Users/andreafaggin/projects/lnproject-be/vendor/craftcms/cms/src/helpers/ElementHelper.php(315): craft\elements\Entry->getSupportedSites()
#6 /Users/andreafaggin/projects/lnproject-be/vendor/craftcms/cms/src/services/Elements.php(3511): craft\helpers\ElementHelper::supportedSitesForElement(Object(craft\elements\Entry))
#7 /Users/andreafaggin/projects/lnproject-be/vendor/craftcms/cms/src/services/Elements.php(1303): craft\services\Elements->_saveElementInternal(Object(craft\elements\Entry), true, false, true, NULL, false, false, false)
#8 /Users/andreafaggin/projects/lnproject-be/modules/sitepropagations/console/controllers/PropagateController.php(198): craft\services\Elements->saveElement(Object(craft\elements\Entry), true, false, true)
...
...
#26 {main}

I am not sure though if this can be tracked back to the same leading cause, I have some more debugging to do.

Do you have any recommendation?

Thanks

@brandonkelly
Copy link
Member

That UnsupportedSiteException is expected if you’re trying to save a nested Matrix entry, for a site that the owner element doesn’t exist on.

The getFieldById() error is a weird one. Here’s the line it’s occurring on:

$field = $this->getOwner()?->getFieldLayout()->getFieldById($this->fieldId)

The only way you’d get that error is if getOwner() returned an element, but that element doesn’t have a field layout. Which should never be the case. All built-in element types have field layouts, and even if there were a plugin-provided element type that didn’t, then it wouldn’t have any Matrix fields. So I’m not really sure what to make of that one.

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

No branches or pull requests

2 participants