Commit 1d9e6b6f by horizons2

use tableprefix on gii model generator

use tableprefix on gii model generator
parent cbcc7b6a
...@@ -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 $useTablePefix=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'],
[['useTablePefix'], 'boolean'],
[['messageCategory'], 'validateMessageCategory', 'skipOnEmpty' => false], [['messageCategory'], 'validateMessageCategory', 'skipOnEmpty' => false],
]); ]);
} }
...@@ -534,6 +536,27 @@ class Generator extends \yii\gii\Generator ...@@ -534,6 +536,27 @@ 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 useTablePefix == true return with {{%}} depending of the position of the prefix
*/
public function generateTablename($tableName)
{
if (!$this->useTablePefix) {
return $tableName;
} else {
$db = $this->getDbConnection();
$patterns = [];
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, 'useTablePefix')->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