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

Humhub 1.16.0 - Error with PolymorphicRelation::getPolymorphicRelation() #6772

Open
marc-farre opened this issue Dec 21, 2023 · 5 comments
Open

Comments

@marc-farre
Copy link
Collaborator

What steps will reproduce the problem?

I cannot tell, but on my Humhub instance, it occurred when viewing the notification list.

The problem is that $this->owner->getAttribute() can return null:

$this->owner->getAttribute($this->classAttribute),

But the first argument of static::loadActiveRecord() must be a string:
https://github.com/humhub/humhub/blob/1db793da1f0d680053713b0654bdd5f32a5339cd/protected/humhub/components/behaviors/PolymorphicRelation.php#L177C50-L177C50

Additional info

Log:

TypeError: humhub\components\behaviors\PolymorphicRelation::loadActiveRecord(): Argument #1 ($className) must be of type string, null given, called in /var/www/humhub/protected/humhub/components/behaviors/PolymorphicRelation.php on line 69 and defined in /var/www/humhub/protected/humhub/components/behaviors/PolymorphicRelation.php:177
Stack trace:
#0 /var/www/humhub/protected/humhub/components/behaviors/PolymorphicRelation.php(69): humhub\components\behaviors\PolymorphicRelation::loadActiveRecord(NULL, NULL)
#1 [internal function]: humhub\components\behaviors\PolymorphicRelation->getPolymorphicRelation()
#2 /var/www/humhub/protected/vendor/yiisoft/yii2/base/Component.php(297): call_user_func_array(Array, Array)
#3 /var/www/humhub/protected/humhub/modules/notification/models/Notification.php(130): yii\base\Component->__call('getPolymorphicR...', Array)
#4 /var/www/humhub/protected/humhub/modules/notification/controllers/ListController.php(46): humhub\modules\notification\models\Notification->getBaseModel()
#5 [internal function]: humhub\modules\notification\controllers\ListController->actionIndex()
#6 /var/www/humhub/protected/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#7 /var/www/humhub/protected/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#8 /var/www/humhub/protected/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('', Array)
#9 /var/www/humhub/protected/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('notification/li...', Array)
#10 /var/www/humhub/protected/vendor/yiisoft/yii2/base/Application.php(384): yii\web\Application->handleRequest(Object(humhub\components\Request))
#11 /var/www/humhub/index.php(28): yii\base\Application->run()
#12 {main}
Q A
HumHub version 1.16.0
PHP version 8.2
@marc-farre
Copy link
Collaborator Author

@martin-rueegg I think it's a code you've worked one. Maybe you know what would be the best approach to solve this problem. (allowing a null param or forcing null to become a string). Thanks!

@ArchBlood
Copy link
Contributor

I've not tested;

$classAttributeValue = $this->owner?->getAttribute($this->classAttribute);

if ($classAttributeValue !== null) {
    $record = static::loadActiveRecord($classAttributeValue);
} else {
    // Handle the case where $classAttributeValue is null
    $record = null;
}

@martin-rueegg
Copy link
Contributor

I guess if $this->owner->getAttribute($this->classAttribute) returns an empty value (no matter if null or '', we should not attempt to load the active record. Same applies for $this->owner->getAttribute($this->pkAttribute). Because without both of these information we cannot find the any ActiveRecord. We should set $record to null in such a case:

So yes, removing the type restriction is probably the easiest option:

@martin-rueegg
Copy link
Contributor

martin-rueegg commented Dec 21, 2023

I've not tested;

$classAttributeValue = $this->owner?->getAttribute($this->classAttribute);

if ($classAttributeValue !== null) {
    $record = static::loadActiveRecord($classAttributeValue);
} else {
    // Handle the case where $classAttributeValue is null
    $record = null;
}

Thanks, @ArchBlood, for your suggestion. According to the exception trace provided by @marc-farre it is not an issue that $this->owner is not set (must be set for a behaviour!), but that the getAttribute() returns a null value.

I'm not sure we need to account for the possibility, that for some reason the owner is set to null, should we? It's a public property and some idiot could have the idea to set it to null ... :-D

Also, the Nullsafe operator is only available since php 8.0. And while @luke- said he wants to silantly fade out v7.4, this would simply crash it ... :-)

@ArchBlood
Copy link
Contributor

Also, the Nullsafe operator is only available since php 8.0. And while @luke- said he wants to silantly fade out v7.4, this would simply crash it ... :-)

I can understand that, some of @GreenMeteor modules will not function unless PHP 8.0 are used, others still need updated for PHP 8.0, but I can understand the silent method of moving away from PHP 7.4. 🤔

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

No branches or pull requests

3 participants