Skip to content
Browse files

Enh #136: Added ability to select database connection in Gii model ge…

…nerator
  • Loading branch information...
1 parent f094f2c commit 5c46317820208d3ff1fa66fa5c37fbf83c9ccd12 @samdark samdark committed Mar 10, 2012
View
1 CHANGELOG
@@ -11,6 +11,7 @@ Version 1.1.11 work in progress
- Bug #417: CAttributeCollections::mergeWith() does not take into account the caseSensitive (dmtrs)
- Bug #433: Fixed the bug that Gii model name input autocomplete was not working sometimes (mdomba)
- Bug #454: Removed translation on CDbConnection exception as it was creating an endless loop if the application used CDbCache (mdomba)
+- Enh #136: Added ability to select database connection in Gii model generator (samdark)
- Enh #165: Allow CCacheDependency to be reusable across multiple cache calls (phpnode)
- Enh #171: Added support for PUT and DELETE request tunneled through POST via parameter named _method in POST body (musterknabe)
- Enh #191: Added ability to customize HTML classes of CLinkPager via its public properties (mashingan)
View
2 framework/gii/CCodeGenerator.php
@@ -76,6 +76,7 @@ public function actionIndex()
/**
* The code preview action.
* This action shows up the specified generated code.
+ * @throws CHttpException if unable to find code generated.
*/
public function actionCode()
{
@@ -93,6 +94,7 @@ public function actionCode()
/**
* The code diff action.
* This action shows up the difference between the newly generated code and the corresponding existing code.
+ * @throws CHttpException if unable to find code generated.
*/
public function actionDiff()
{
View
66 framework/gii/generators/model/ModelCode.php
@@ -2,6 +2,7 @@
class ModelCode extends CCodeModel
{
+ public $connectionId='db';
public $tablePrefix;
public $tableName;
public $modelClass;
@@ -19,14 +20,14 @@ public function rules()
{
return array_merge(parent::rules(), array(
array('tablePrefix, baseClass, tableName, modelClass, modelPath', 'filter', 'filter'=>'trim'),
- array('tableName, modelPath, baseClass', 'required'),
+ array('connectionId, tableName, modelPath, baseClass', 'required'),
array('tablePrefix, tableName, modelPath', 'match', 'pattern'=>'/^(\w+[\w\.]*|\*?|\w+\.\*)$/', 'message'=>'{attribute} should only contain word characters, dots, and an optional ending asterisk.'),
array('tableName', 'validateTableName', 'skipOnError'=>true),
array('tablePrefix, modelClass, baseClass', 'match', 'pattern'=>'/^[a-zA-Z_]\w*$/', 'message'=>'{attribute} should only contain word characters.'),
array('modelPath', 'validateModelPath', 'skipOnError'=>true),
array('baseClass, modelClass', 'validateReservedWord', 'skipOnError'=>true),
array('baseClass', 'validateBaseClass', 'skipOnError'=>true),
- array('tablePrefix, modelPath, baseClass, buildRelations', 'sticky'),
+ array('connectionId, tablePrefix, modelPath, baseClass, buildRelations', 'sticky'),
));
}
@@ -39,6 +40,7 @@ public function attributeLabels()
'modelClass'=>'Model Class',
'baseClass'=>'Base Class',
'buildRelations'=>'Build Relations',
+ 'connectionId'=>'Database Connection',
));
}
@@ -51,9 +53,9 @@ public function requiredTemplates()
public function init()
{
- if(Yii::app()->db===null)
- throw new CHttpException(500,'An active "db" connection is required to run this generator.');
- $this->tablePrefix=Yii::app()->db->tablePrefix;
+ if(Yii::app()->{$this->connectionId}===null)
+ throw new CHttpException(500,'An active "'.$this->connectionId.'" connection is required to run this generator.');
+ $this->tablePrefix=Yii::app()->{$this->connectionId}->tablePrefix;
parent::init();
}
@@ -71,7 +73,7 @@ public function prepare()
}
if($tableName[strlen($tableName)-1]==='*')
{
- $tables=Yii::app()->db->schema->getTables($schema);
+ $tables=Yii::app()->{$this->connectionId}->schema->getTables($schema);
if($this->tablePrefix!='')
{
foreach($tables as $i=>$table)
@@ -99,6 +101,7 @@ public function prepare()
'labels'=>$this->generateLabels($table),
'rules'=>$this->generateRules($table),
'relations'=>isset($this->relations[$className]) ? $this->relations[$className] : array(),
+ 'connectionId'=>$this->connectionId,
);
$this->files[]=new CCodeFile(
Yii::getPathOfAlias($this->modelPath).'/'.$className.'.php',
@@ -120,7 +123,7 @@ public function validateTableName($attribute,$params)
$schema='';
$this->modelClass='';
- $tables=Yii::app()->db->schema->getTables($schema);
+ $tables=Yii::app()->{$this->connectionId}->schema->getTables($schema);
foreach($tables as $table)
{
if($this->tablePrefix=='' || strpos($table->name,$this->tablePrefix)===0)
@@ -181,7 +184,7 @@ public function validateBaseClass($attribute,$params)
public function getTableSchema($tableName)
{
- return Yii::app()->db->getSchema()->getTable($tableName);
+ return Yii::app()->{$this->connectionId}->getSchema()->getTable($tableName);
}
public function generateLabels($table)
@@ -248,14 +251,14 @@ public function getRelations($className)
protected function removePrefix($tableName,$addBrackets=true)
{
- if($addBrackets && Yii::app()->db->tablePrefix=='')
+ if($addBrackets && Yii::app()->{$this->connectionId}->tablePrefix=='')
return $tableName;
- $prefix=$this->tablePrefix!='' ? $this->tablePrefix : Yii::app()->db->tablePrefix;
+ $prefix=$this->tablePrefix!='' ? $this->tablePrefix : Yii::app()->{$this->connectionId}->tablePrefix;
if($prefix!='')
{
- if($addBrackets && Yii::app()->db->tablePrefix!='')
+ if($addBrackets && Yii::app()->{$this->connectionId}->tablePrefix!='')
{
- $prefix=Yii::app()->db->tablePrefix;
+ $prefix=Yii::app()->{$this->connectionId}->tablePrefix;
$lb='{{';
$rb='}}';
}
@@ -279,7 +282,7 @@ protected function generateRelations()
if(!$this->buildRelations)
return array();
$relations=array();
- foreach(Yii::app()->db->schema->getTables() as $table)
+ foreach(Yii::app()->{$this->connectionId}->schema->getTables() as $table)
{
if($this->tablePrefix!='' && strpos($table->name,$this->tablePrefix)!==0)
continue;
@@ -385,11 +388,46 @@ protected function generateRelationName($tableName, $fkName, $multiple)
$name.=ucfirst($names[$i]);
$rawName=$name;
- $table=Yii::app()->db->schema->getTable($tableName);
+ $table=Yii::app()->{$this->connectionId}->schema->getTable($tableName);
$i=0;
while(isset($table->columns[$name]))
$name=$rawName.($i++);
return $name;
}
+
+ /**
+ * @return array List of DB connections ready to be displayed in dropdown
+ */
+ public function getConnectionList()
+ {
+ $list=array();
+ foreach(Yii::app()->getComponents(false) as $name=>$component)
+ {
+ if($this->isDbConnection($name,$component))
+ {
+ $connectionString = is_object($component) ? $component->connectionString : $component['connectionString'];
+ $list[$name]=$name.' ('.$connectionString.')';
+ }
+ }
+ return $list;
+ }
+
+ /**
+ * @param string $name component name
+ * @param mixed $component component config or component object
+ * @return bool if component is DB connection
+ */
+ private function isDbConnection($name,$component)
+ {
+ if(is_array($component))
+ {
+ if($component['class']=='CDbConnection')
+ return true;
+ else
+ $component=Yii::app()->getComponent($name);
+ }
+
+ return $component instanceof CDbConnection;
+ }
}
View
10 framework/gii/generators/model/templates/default/model.php
@@ -61,6 +61,16 @@ public static function model($className=__CLASS__)
{
return parent::model($className);
}
+ <?php if($connectionId!='db'):?>
+
+ /**
+ * @return CDbConnection database connection
+ */
+ public function getDbConnection()
+ {
+ return Yii::app()-><?php echo $connectionId ?>;
+ }
+ <?php endif?>
/**
* @return string the associated database table name
View
10 framework/gii/generators/model/views/index.php
@@ -39,6 +39,14 @@
<?php $form=$this->beginWidget('CCodeForm', array('model'=>$model)); ?>
<div class="row sticky">
+ <?php echo $form->labelEx($model, 'connectionId')?>
+ <?php echo $form->dropDownList($model, 'connectionId', $model->getConnectionList())?>
+ <div class="tooltip">
+ This refers to the database connection used.
+ </div>
+ <?php echo $form->error($model,'connectionId'); ?>
+ </div>
+ <div class="row sticky">
<?php echo $form->labelEx($model,'tablePrefix'); ?>
<?php echo $form->textField($model,'tablePrefix', array('size'=>65)); ?>
<div class="tooltip">
@@ -57,7 +65,7 @@
'model'=>$model,
'attribute'=>'tableName',
'name'=>'tableName',
- 'source'=>array_keys(Yii::app()->db->schema->getTables()),
+ 'source'=>array_keys(Yii::app()->{$model->connectionId}->schema->getTables()),
'options'=>array(
'minLength'=>'0',
'focus' => 'js:function(event,ui) {

0 comments on commit 5c46317

Please sign in to comment.
Something went wrong with that request. Please try again.