Skip to content

Commit

Permalink
Fixes #327: Fixed bug in Model generator when $baseClass is an abstra…
Browse files Browse the repository at this point in the history
…ct class
  • Loading branch information
rhertogh authored and samdark committed Jun 12, 2018
1 parent ab8ba3b commit ee4d072
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Yii Framework 2 gii extension Change Log
2.0.8 under development
-----------------------

- Bug #327: Fixed bug in Model generator when $baseClass is an abstract class (rhertogh)
- Enh #366: Better class and file names for uppercase tables (slinstj)


Expand Down
17 changes: 16 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,23 @@ 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()) {
$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 ee4d072

Please sign in to comment.