mirror of
https://github.com/humhub/humhub.git
synced 2025-01-16 21:58:17 +01:00
Enh: Added PolymorphicRelation::strict
to throw exceptions when accessing invalid polymorphic relations
This commit is contained in:
parent
efb7a5d41e
commit
0c777b63af
@ -15,6 +15,7 @@ use Yii;
|
||||
use yii\base\Behavior;
|
||||
use yii\db\ActiveRecord;
|
||||
use yii\db\BaseActiveRecord;
|
||||
use Yii\db\IntegrityException;
|
||||
|
||||
/**
|
||||
* PolymorphicRelations behavior provides simple support for polymorphic relations in ActiveRecords.
|
||||
@ -34,6 +35,11 @@ class PolymorphicRelation extends Behavior
|
||||
*/
|
||||
public $pkAttribute = 'object_id';
|
||||
|
||||
/**
|
||||
* @var boolean if set to true an exception is thrown if `object_model` and `object_id` is set but does not exist
|
||||
*/
|
||||
public $strict = false;
|
||||
|
||||
/**
|
||||
* @var array the related object needs to be a "instanceof" at least one of these given classnames
|
||||
*/
|
||||
@ -60,6 +66,10 @@ class PolymorphicRelation extends Behavior
|
||||
$this->owner->getAttribute($this->pkAttribute)
|
||||
);
|
||||
|
||||
if($this->strict && !$object && !empty($this->classAttribute) && !empty($this->pkAttribute)) {
|
||||
throw new IntegrityException('Call to an inconsistent polymorphic relation detected on '.get_class($this->owner).' ('.$this->classAttribute.' : '.$this->pkAttribute.')');
|
||||
}
|
||||
|
||||
if ($object !== null && $this->validateUnderlyingObjectType($object)) {
|
||||
$this->_cached = $object;
|
||||
return $object;
|
||||
|
@ -1,6 +1,11 @@
|
||||
HumHub Change Log
|
||||
=================
|
||||
|
||||
1.3.0
|
||||
-----------------------------
|
||||
|
||||
- Enh: Added `PolymorphicRelation::strict` to throw exceptions when accessing invalid polymorphic relations
|
||||
|
||||
1.3.0-beta.3 (July 30, 2018)
|
||||
-----------------------------
|
||||
|
||||
|
@ -54,6 +54,7 @@ class Activity extends ContentActiveRecord
|
||||
return [
|
||||
[
|
||||
'class' => PolymorphicRelation::class,
|
||||
'strict' => true,
|
||||
'mustBeInstanceOf' => [
|
||||
ActiveRecord::class,
|
||||
]
|
||||
|
@ -51,6 +51,7 @@ class Notification extends \humhub\components\ActiveRecord
|
||||
'class' => PolymorphicRelation::class,
|
||||
'classAttribute' => 'source_class',
|
||||
'pkAttribute' => 'source_pk',
|
||||
'strict' => true,
|
||||
'mustBeInstanceOf' => [
|
||||
\yii\db\ActiveRecord::class,
|
||||
],
|
||||
|
Loading…
x
Reference in New Issue
Block a user