|
12 | 12 | use yii\base\UnknownPropertyException; |
13 | 13 | use yii\base\UnknownMethodException; |
14 | 14 | use yii\db\Exception; |
| 15 | +use yii\base\Exception as BaseException; |
15 | 16 | use Yii; |
16 | 17 |
|
17 | 18 | /** |
18 | 19 | * Trait to simulate inheritance between two ActiveRecordInterface classes. |
19 | | - * Child classes must use the trait and implement the extendsFrom method. |
| 20 | + * Child classes MUST use the trait and MUST implement the |
| 21 | + * ActiveRecordInheritanceInterface. |
| 22 | + * |
| 23 | + * @see ActiveRecordInheritanceInterface |
20 | 24 | * |
21 | 25 | * * Methods overwriten from ActiveRecord in this trait like save, validate, etc... |
22 | 26 | * should not be overwriten on the class that uses the trait or functionality |
@@ -78,6 +82,9 @@ trait ActiveRecordInheritanceTrait { |
78 | 82 | */ |
79 | 83 | private function _parent() { |
80 | 84 | if ($this->_parent === null) { |
| 85 | + if (($this instanceof ActiveRecordInheritanceInterface) === false) { |
| 86 | + throw new BaseException('Classes that use the \jlorente\db\ActiveRecordInheritanceTrait must implement \jlorente\db\ActiveRecordInheritanceInterface'); |
| 87 | + } |
81 | 88 | $pClass = static::extendsFrom(); |
82 | 89 | if ($this->id !== null) { |
83 | 90 | $this->_parent = $pClass::findOne($this->{$this->parentAttribute()}); |
@@ -267,11 +274,4 @@ public function parentPrimaryKey() { |
267 | 274 | $pClass = static::extendsFrom(); |
268 | 275 | return $pClass::primaryKey()[0]; |
269 | 276 | } |
270 | | - |
271 | | - /** |
272 | | - * Returns the fully qualified parent class name. |
273 | | - * |
274 | | - * @return string |
275 | | - */ |
276 | | - abstract public static function extendsFrom(); |
277 | 277 | } |
0 commit comments