Commit 0480fcc2 by Qiang Xue

Merge pull request #3222 from horizons2/patch-1

use tableprefix on gii model generator
parents 78e3eee2 b68f5b61
...@@ -30,6 +30,7 @@ class Generator extends \yii\gii\Generator ...@@ -30,6 +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;
/** /**
* @inheritdoc * @inheritdoc
...@@ -65,6 +66,7 @@ class Generator extends \yii\gii\Generator ...@@ -65,6 +66,7 @@ class Generator extends \yii\gii\Generator
[['baseClass'], 'validateClass', 'params' => ['extends' => ActiveRecord::className()]], [['baseClass'], 'validateClass', 'params' => ['extends' => ActiveRecord::className()]],
[['generateRelations', 'generateLabelsFromComments'], 'boolean'], [['generateRelations', 'generateLabelsFromComments'], 'boolean'],
[['enableI18N'], 'boolean'], [['enableI18N'], 'boolean'],
[['useTablePrefix'], 'boolean'],
[['messageCategory'], 'validateMessageCategory', 'skipOnEmpty' => false], [['messageCategory'], 'validateMessageCategory', 'skipOnEmpty' => false],
]); ]);
} }
...@@ -534,6 +536,26 @@ class Generator extends \yii\gii\Generator ...@@ -534,6 +536,26 @@ class Generator extends \yii\gii\Generator
} }
/** /**
* Generates a the tablename with tableprefix usage .
* @param string $tableName the table name (which may contain schema prefix)
* @return string the generated table name if useTablePrefix == true return with {{%}} depending of the position of the prefix
*/
public function generateTablename($tableName)
{
if (!$this->useTablePrefix) {
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;
}
}
/**
* Generates a class name from the specified table name. * Generates a class name from the specified table name.
* @param string $tableName the table name (which may contain schema prefix) * @param string $tableName the table name (which may contain schema prefix)
* @return string the generated class name * @return string the generated class name
......
...@@ -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 '<?= $tableName ?>'; return '<?= $generator->generateTablename($tableName) ?>';
} }
<?php if ($generator->db !== 'db'): ?> <?php if ($generator->db !== 'db'): ?>
......
...@@ -10,6 +10,7 @@ echo $form->field($generator, 'modelClass'); ...@@ -10,6 +10,7 @@ echo $form->field($generator, 'modelClass');
echo $form->field($generator, 'ns'); echo $form->field($generator, 'ns');
echo $form->field($generator, 'baseClass'); echo $form->field($generator, 'baseClass');
echo $form->field($generator, 'db'); echo $form->field($generator, 'db');
echo $form->field($generator, 'useTablePrefix')->checkbox();
echo $form->field($generator, 'generateRelations')->checkbox(); echo $form->field($generator, 'generateRelations')->checkbox();
echo $form->field($generator, 'generateLabelsFromComments')->checkbox(); echo $form->field($generator, 'generateLabelsFromComments')->checkbox();
echo $form->field($generator, 'enableI18N')->checkbox(); echo $form->field($generator, 'enableI18N')->checkbox();
......
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