Commit 0c8faedd by Alexander Makarov

Fixes #3760: Gii model generator now imports classes when necessary instead of…

Fixes #3760: Gii model generator now imports classes when necessary instead of using fully qualified class names
parent 84ab9b55
......@@ -4,6 +4,7 @@ Yii Framework 2 gii extension Change Log
2.0.1 under development
-----------------------
- Enh #3760: Gii model generator now imports classes when necessary instead of using fully qualified class names (umneeq, samdark)
- Enh #5613: Added `--overwrite` option to Gii console command to support overwriting all files (motin, qiangxue)
- Bug: Gii console command help information does not contain global options (qiangxue)
......
......@@ -165,9 +165,17 @@ class Generator extends \yii\gii\Generator
foreach ($this->getTableNames() as $tableName) {
$className = $this->generateClassName($tableName);
$tableSchema = $db->getTableSchema($tableName);
$baseClassReflection = new \ReflectionClass($this->baseClass);
$baseClassName = $baseClassReflection->getShortName();
$baseClassNamespace = '\\' . ltrim($baseClassReflection->getNamespaceName(), '\\');
$params = [
'tableName' => $tableName,
'className' => $className,
'classNamespace' => '\\' . ltrim($this->ns, '\\'),
'baseClassName' => $baseClassName,
'baseClassNamespace' => $baseClassNamespace,
'tableSchema' => $tableSchema,
'labels' => $this->generateLabels($tableSchema),
'rules' => $this->generateRules($tableSchema),
......
......@@ -7,6 +7,9 @@
/* @var $generator yii\gii\generators\model\Generator */
/* @var $tableName string full table name */
/* @var $className string class name */
/* @var $classNamespace string class fully qualified namespace */
/* @var $baseClassName string base class name */
/* @var $baseClassNamespace string base class fully qualified namespace */
/* @var $tableSchema yii\db\TableSchema */
/* @var $labels string[] list of attribute labels (name => label) */
/* @var $rules string[] list of validation rules */
......@@ -18,6 +21,9 @@ echo "<?php\n";
namespace <?= $generator->ns ?>;
use Yii;
<?php if ($classNamespace !== $baseClassNamespace): ?>
use <?= ltrim($generator->baseClass, '\\') ?><?= $className === $baseClassName ? ' as ' . $baseClassName . 'Base' : '' ?>;
<?php endif; ?>
/**
* This is the model class for table "<?= $generator->generateTableName($tableName) ?>".
......@@ -32,7 +38,7 @@ use Yii;
<?php endforeach; ?>
<?php endif; ?>
*/
class <?= $className ?> extends <?= '\\' . ltrim($generator->baseClass, '\\') . "\n" ?>
class <?= $className ?> extends <?= ($className === $baseClassName ? $baseClassName . 'Base' : $baseClassName) . "\n" ?>
{
/**
* @inheritdoc
......
......@@ -13,6 +13,7 @@ Yii Framework 2 Change Log
- Bug #5665: The `currentPage` meta data in the RESTful result should be 1-based, similar to that in HTTP headers (qiangxue)
- Bug #5682: The `asset` command would incorrectly combine CSS files when `UrlManager::linkAssets` is true (dmvslv)
- Bug: Gii console command help information does not contain global options (qiangxue)
- Enh #3760: Gii model generator now imports classes when necessary instead of using fully qualified class names (umneeq, samdark)
- Enh #5223: Query builder now supports selecting sub-queries as columns (qiangxue)
- Enh #5587: `json_encode` is now used with `JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE` where it makes sense, also
it is now default for `Json::encode()` (samdark)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment