Skip to content

Commit

Permalink
Fixed Model Generator to support abstract baseClass
Browse files Browse the repository at this point in the history
Fixes yiisoft#327
Checks if baseClass is an abstract class, and if so wrap it in a regular class before instantiating it.
  • Loading branch information
rhertogh committed May 23, 2018
1 parent c544316 commit d619c05
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/generators/model/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace yii\gii\generators\model;

use Yii;
use yii\base\InvalidConfigException;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\db\Connection;
Expand Down Expand Up @@ -689,9 +690,27 @@ protected function generateRelationName($relations, $table, $key, $multiple)
/* @var $baseModel \yii\db\ActiveRecord */
if ($baseModel === null) {
$baseClass = $this->baseClass;
$baseModel = new $baseClass();
$baseClassReflector = new \ReflectionClass($baseClass);
if ($baseClassReflector->isAbstract()) {
//Extra check for security to validate that $baseClass is indeed a class since this variable is used in eval
if (!class_exists($baseClass)) {
throw new InvalidConfigException("Class '$class' does not exist or has syntax error.");
}
$baseClassWrapper =
'namespace ' . __NAMESPACE__ . ';'.
'class GiiBaseClassWrapper extends \\' . $baseClass . ' {' .
'public static function tableName(){' .
'return "' . addslashes($table->fullName) . '";' .
'}' .
'};' .
'return new GiiBaseClassWrapper();';
$baseModel = eval($baseClassWrapper);
} else {
$baseModel = new $baseClass();
}
$baseModel->setAttributes([]);
}

if (!empty($key) && strcasecmp($key, 'id')) {
if (substr_compare($key, 'id', -2, 2, true) === 0) {
$key = rtrim(substr($key, 0, -2), '_');
Expand Down

0 comments on commit d619c05

Please sign in to comment.