Skip to content

Commit

Permalink
Fix #6839: The getObjectModel() method should work for all ActiveReco…
Browse files Browse the repository at this point in the history
…rd classes (#6854)
  • Loading branch information
marc-farre committed Feb 13, 2024
1 parent 64e7614 commit c293d3c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 54 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ HumHub Changelog
- Enh #6838: Fix LDAP encryption labels and allow ignore also part of DNs
- Enh #6490: Update button style on force password form
- Enh #6847: Use prosemirror file handler flag
- Fix #6839: The getObjectModel() method should work for all ActiveRecord classes
31 changes: 27 additions & 4 deletions protected/humhub/components/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace humhub\components;

use Yii;
use humhub\modules\user\models\User;
use humhub\modules\file\components\FileManager;
use humhub\modules\user\models\User;
use Yii;
use yii\base\InvalidConfigException;
use yii\db\ColumnSchema;
use yii\db\Expression;
Expand Down Expand Up @@ -76,7 +76,7 @@ public function afterSave($insert, $changedAttributes)
$this->created_at = date('Y-m-d H:i:s');
}

if($this->hasAttribute('updated_at') && $this->updated_at instanceof Expression) {
if ($this->hasAttribute('updated_at') && $this->updated_at instanceof Expression) {
$this->updated_at = date('Y-m-d H:i:s');
}

Expand Down Expand Up @@ -136,9 +136,9 @@ public function getFileManager()
/**
* Returns the errors as string for all attribute or a single attribute.
*
* @since 1.2
* @param string $attribute attribute name. Use null to retrieve errors for all attributes.
* @return string the error message
* @since 1.2
*/
public function getErrorMessage($attribute = null)
{
Expand Down Expand Up @@ -257,4 +257,27 @@ private function columnValueCanBeNormalized(?ColumnSchema $column, $value): bool

return false;
}

/**
* Returns the class used in the polymorphic content relation.
* By default, this function will return the static class.
*
* Subclasses of existing content record classes may overwrite this function in order to remain the actual
* base type as follows:
*
* ```
* public static function getObjectModel(): string {
* return BaseType::class
* }
* ```
*
* This will force the usage of the `BaseType` class when creating, deleting or querying the content relation.
* This is used in cases in which a subclass extends the a base record class without implementing a custom content type.
*
* @return string
*/
public static function getObjectModel(): string
{
return static::class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
use Exception;
use humhub\components\ActiveRecord;
use humhub\helpers\DataTypeHelper;
use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\content\components\ContentAddonActiveRecord;
use ReflectionClass;
use ReflectionException;
use Yii;
Expand Down Expand Up @@ -119,7 +117,7 @@ public function setPolymorphicRelation(?object $object)

public static function getObjectModel(ActiveRecordInterface $object): string
{
return $object instanceof ContentActiveRecord || $object instanceof ContentAddonActiveRecord
return $object instanceof ActiveRecord
? $object::getObjectModel()
: get_class($object);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,29 +484,6 @@ public function afterStateChange(?int $newState, ?int $previousState): void
}
}

/**
* Returns the class used in the polymorphic content relation.
* By default this function will return the static class.
*
* Subclasses of existing content record classes may overwrite this function in order to remain the actual
* base type as follows:
*
* ```
* public static function getObjectModel(): string {
* return BaseType::class
* }
* ```
*
* This will force the usage of the `BaseType` class when creating, deleting or querying the content relation.
* This is used in cases in which a subclass extends the a base record class without implementing a custom content type.
*
* @return string
*/
public static function getObjectModel(): string
{
return static::class;
}

/**
* Marks this content for deletion (soft delete).
* Use `hardDelete()` method to delete record immediately.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,28 +281,4 @@ public function getUser()
{
return $this->hasOne(User::class, ['id' => 'created_by']);
}

/**
* Returns the class used in the polymorphic content relation.
* By default this function will return the static class.
*
* Subclasses of existing content record classes may overwrite this function in order to remain the actual
* base type as follows:
*
* ```
* public static function getObjectModel(): string {
* return BaseType::class
* }
* ```
*
* This will force the usage of the `BaseType` class when creating, deleting or querying the content relation.
* This is used in cases in which a subclass extends a base record class without implementing a custom content type.
*
* @return string
*/
public static function getObjectModel(): string
{
return static::class;
}

}

0 comments on commit c293d3c

Please sign in to comment.