Commit 3e3e4435 by Qiang Xue

Refactored the fix for #3222.

parent 0480fcc2
...@@ -6,6 +6,7 @@ Yii Framework 2 gii extension Change Log ...@@ -6,6 +6,7 @@ Yii Framework 2 gii extension Change Log
- Bug #1263: Fixed the issue that Gii and Debug modules might be affected by incompatible asset manager configuration (qiangxue) - Bug #1263: Fixed the issue that Gii and Debug modules might be affected by incompatible asset manager configuration (qiangxue)
- Enh #3088: The gii module will manage their own URL rules now (qiangxue) - Enh #3088: The gii module will manage their own URL rules now (qiangxue)
- Enh #3222: Added `useTablePrefix` option to the model generator for Gii (horizons2)
2.0.0-beta April 13, 2014 2.0.0-beta April 13, 2014
......
...@@ -30,7 +30,7 @@ class Generator extends \yii\gii\Generator ...@@ -30,7 +30,7 @@ class Generator extends \yii\gii\Generator
public $baseClass = 'yii\db\ActiveRecord'; public $baseClass = 'yii\db\ActiveRecord';
public $generateRelations = true; public $generateRelations = true;
public $generateLabelsFromComments = false; public $generateLabelsFromComments = false;
public $useTablePrefix=false; public $useTablePrefix = false;
/** /**
* @inheritdoc * @inheritdoc
...@@ -536,23 +536,24 @@ class Generator extends \yii\gii\Generator ...@@ -536,23 +536,24 @@ class Generator extends \yii\gii\Generator
} }
/** /**
* Generates a the tablename with tableprefix usage . * Generates the table name by considering table prefix.
* @param string $tableName the table name (which may contain schema prefix) * If [[useTablePrefix]] is false, the table name will be returned without change.
* @return string the generated table name if useTablePrefix == true return with {{%}} depending of the position of the prefix * @param string $tableName the table name (which may contain schema prefix)
* @return string the generated table name
*/ */
public function generateTablename($tableName) public function generateTableName($tableName)
{ {
if (!$this->useTablePrefix) { if (!$this->useTablePrefix) {
return $tableName; return $tableName;
} else {
$db = $this->getDbConnection();
if (preg_match("/^{$db->tablePrefix}(.*?)$/", $tableName, $matches)) {
$tableName = '{{%'.$matches[1].'}}';
} elseif (preg_match("/^(.*?){$db->tablePrefix}$/", $tableName, $matches)) {
$tableName = '{{'.$matches[1].'%}}';
}
return $tableName;
} }
$db = $this->getDbConnection();
if (preg_match("/^{$db->tablePrefix}(.*?)$/", $tableName, $matches)) {
$tableName = '{{%' . $matches[1] . '}}';
} elseif (preg_match("/^(.*?){$db->tablePrefix}$/", $tableName, $matches)) {
$tableName = '{{' . $matches[1] . '%}}';
}
return $tableName;
} }
/** /**
......
...@@ -39,7 +39,7 @@ class <?= $className ?> extends <?= '\\' . ltrim($generator->baseClass, '\\') . ...@@ -39,7 +39,7 @@ class <?= $className ?> extends <?= '\\' . ltrim($generator->baseClass, '\\') .
*/ */
public static function tableName() public static function tableName()
{ {
return '<?= $generator->generateTablename($tableName) ?>'; return '<?= $generator->generateTableName($tableName) ?>';
} }
<?php if ($generator->db !== 'db'): ?> <?php if ($generator->db !== 'db'): ?>
......
...@@ -28,6 +28,7 @@ Yii Framework 2 Change Log ...@@ -28,6 +28,7 @@ Yii Framework 2 Change Log
- Enh #3108: Added `yii\debug\Module::enableDebugLogs` to disable logging debug logs by default (qiangxue) - Enh #3108: Added `yii\debug\Module::enableDebugLogs` to disable logging debug logs by default (qiangxue)
- Enh #3132: `yii\rbac\PhpManager` now supports more compact data file format (qiangxue) - Enh #3132: `yii\rbac\PhpManager` now supports more compact data file format (qiangxue)
- Enh #3154: Added validation error display for `GridView` filters (ivan-kolmychek) - Enh #3154: Added validation error display for `GridView` filters (ivan-kolmychek)
- Enh #3222: Added `useTablePrefix` option to the model generator for Gii (horizons2)
- Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue) - Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue)
- Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue) - Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue)
- Enh: Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules (qiangxue) - Enh: Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules (qiangxue)
......
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