Skip to content

Commit 5c46317

Browse files
committedMar 10, 2012
Enh yiisoft#136: Added ability to select database connection in Gii model generator
1 parent f094f2c commit 5c46317

File tree

5 files changed

+74
-15
lines changed

5 files changed

+74
-15
lines changed
 

‎CHANGELOG

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Version 1.1.11 work in progress
1111
- Bug #417: CAttributeCollections::mergeWith() does not take into account the caseSensitive (dmtrs)
1212
- Bug #433: Fixed the bug that Gii model name input autocomplete was not working sometimes (mdomba)
1313
- Bug #454: Removed translation on CDbConnection exception as it was creating an endless loop if the application used CDbCache (mdomba)
14+
- Enh #136: Added ability to select database connection in Gii model generator (samdark)
1415
- Enh #165: Allow CCacheDependency to be reusable across multiple cache calls (phpnode)
1516
- Enh #171: Added support for PUT and DELETE request tunneled through POST via parameter named _method in POST body (musterknabe)
1617
- Enh #191: Added ability to customize HTML classes of CLinkPager via its public properties (mashingan)

‎framework/gii/CCodeGenerator.php

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public function actionIndex()
7676
/**
7777
* The code preview action.
7878
* This action shows up the specified generated code.
79+
* @throws CHttpException if unable to find code generated.
7980
*/
8081
public function actionCode()
8182
{
@@ -93,6 +94,7 @@ public function actionCode()
9394
/**
9495
* The code diff action.
9596
* This action shows up the difference between the newly generated code and the corresponding existing code.
97+
* @throws CHttpException if unable to find code generated.
9698
*/
9799
public function actionDiff()
98100
{

‎framework/gii/generators/model/ModelCode.php

+52-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
class ModelCode extends CCodeModel
44
{
5+
public $connectionId='db';
56
public $tablePrefix;
67
public $tableName;
78
public $modelClass;
@@ -19,14 +20,14 @@ public function rules()
1920
{
2021
return array_merge(parent::rules(), array(
2122
array('tablePrefix, baseClass, tableName, modelClass, modelPath', 'filter', 'filter'=>'trim'),
22-
array('tableName, modelPath, baseClass', 'required'),
23+
array('connectionId, tableName, modelPath, baseClass', 'required'),
2324
array('tablePrefix, tableName, modelPath', 'match', 'pattern'=>'/^(\w+[\w\.]*|\*?|\w+\.\*)$/', 'message'=>'{attribute} should only contain word characters, dots, and an optional ending asterisk.'),
2425
array('tableName', 'validateTableName', 'skipOnError'=>true),
2526
array('tablePrefix, modelClass, baseClass', 'match', 'pattern'=>'/^[a-zA-Z_]\w*$/', 'message'=>'{attribute} should only contain word characters.'),
2627
array('modelPath', 'validateModelPath', 'skipOnError'=>true),
2728
array('baseClass, modelClass', 'validateReservedWord', 'skipOnError'=>true),
2829
array('baseClass', 'validateBaseClass', 'skipOnError'=>true),
29-
array('tablePrefix, modelPath, baseClass, buildRelations', 'sticky'),
30+
array('connectionId, tablePrefix, modelPath, baseClass, buildRelations', 'sticky'),
3031
));
3132
}
3233

@@ -39,6 +40,7 @@ public function attributeLabels()
3940
'modelClass'=>'Model Class',
4041
'baseClass'=>'Base Class',
4142
'buildRelations'=>'Build Relations',
43+
'connectionId'=>'Database Connection',
4244
));
4345
}
4446

@@ -51,9 +53,9 @@ public function requiredTemplates()
5153

5254
public function init()
5355
{
54-
if(Yii::app()->db===null)
55-
throw new CHttpException(500,'An active "db" connection is required to run this generator.');
56-
$this->tablePrefix=Yii::app()->db->tablePrefix;
56+
if(Yii::app()->{$this->connectionId}===null)
57+
throw new CHttpException(500,'An active "'.$this->connectionId.'" connection is required to run this generator.');
58+
$this->tablePrefix=Yii::app()->{$this->connectionId}->tablePrefix;
5759
parent::init();
5860
}
5961

@@ -71,7 +73,7 @@ public function prepare()
7173
}
7274
if($tableName[strlen($tableName)-1]==='*')
7375
{
74-
$tables=Yii::app()->db->schema->getTables($schema);
76+
$tables=Yii::app()->{$this->connectionId}->schema->getTables($schema);
7577
if($this->tablePrefix!='')
7678
{
7779
foreach($tables as $i=>$table)
@@ -99,6 +101,7 @@ public function prepare()
99101
'labels'=>$this->generateLabels($table),
100102
'rules'=>$this->generateRules($table),
101103
'relations'=>isset($this->relations[$className]) ? $this->relations[$className] : array(),
104+
'connectionId'=>$this->connectionId,
102105
);
103106
$this->files[]=new CCodeFile(
104107
Yii::getPathOfAlias($this->modelPath).'/'.$className.'.php',
@@ -120,7 +123,7 @@ public function validateTableName($attribute,$params)
120123
$schema='';
121124

122125
$this->modelClass='';
123-
$tables=Yii::app()->db->schema->getTables($schema);
126+
$tables=Yii::app()->{$this->connectionId}->schema->getTables($schema);
124127
foreach($tables as $table)
125128
{
126129
if($this->tablePrefix=='' || strpos($table->name,$this->tablePrefix)===0)
@@ -181,7 +184,7 @@ public function validateBaseClass($attribute,$params)
181184

182185
public function getTableSchema($tableName)
183186
{
184-
return Yii::app()->db->getSchema()->getTable($tableName);
187+
return Yii::app()->{$this->connectionId}->getSchema()->getTable($tableName);
185188
}
186189

187190
public function generateLabels($table)
@@ -248,14 +251,14 @@ public function getRelations($className)
248251

249252
protected function removePrefix($tableName,$addBrackets=true)
250253
{
251-
if($addBrackets && Yii::app()->db->tablePrefix=='')
254+
if($addBrackets && Yii::app()->{$this->connectionId}->tablePrefix=='')
252255
return $tableName;
253-
$prefix=$this->tablePrefix!='' ? $this->tablePrefix : Yii::app()->db->tablePrefix;
256+
$prefix=$this->tablePrefix!='' ? $this->tablePrefix : Yii::app()->{$this->connectionId}->tablePrefix;
254257
if($prefix!='')
255258
{
256-
if($addBrackets && Yii::app()->db->tablePrefix!='')
259+
if($addBrackets && Yii::app()->{$this->connectionId}->tablePrefix!='')
257260
{
258-
$prefix=Yii::app()->db->tablePrefix;
261+
$prefix=Yii::app()->{$this->connectionId}->tablePrefix;
259262
$lb='{{';
260263
$rb='}}';
261264
}
@@ -279,7 +282,7 @@ protected function generateRelations()
279282
if(!$this->buildRelations)
280283
return array();
281284
$relations=array();
282-
foreach(Yii::app()->db->schema->getTables() as $table)
285+
foreach(Yii::app()->{$this->connectionId}->schema->getTables() as $table)
283286
{
284287
if($this->tablePrefix!='' && strpos($table->name,$this->tablePrefix)!==0)
285288
continue;
@@ -385,11 +388,46 @@ protected function generateRelationName($tableName, $fkName, $multiple)
385388
$name.=ucfirst($names[$i]);
386389

387390
$rawName=$name;
388-
$table=Yii::app()->db->schema->getTable($tableName);
391+
$table=Yii::app()->{$this->connectionId}->schema->getTable($tableName);
389392
$i=0;
390393
while(isset($table->columns[$name]))
391394
$name=$rawName.($i++);
392395

393396
return $name;
394397
}
398+
399+
/**
400+
* @return array List of DB connections ready to be displayed in dropdown
401+
*/
402+
public function getConnectionList()
403+
{
404+
$list=array();
405+
foreach(Yii::app()->getComponents(false) as $name=>$component)
406+
{
407+
if($this->isDbConnection($name,$component))
408+
{
409+
$connectionString = is_object($component) ? $component->connectionString : $component['connectionString'];
410+
$list[$name]=$name.' ('.$connectionString.')';
411+
}
412+
}
413+
return $list;
414+
}
415+
416+
/**
417+
* @param string $name component name
418+
* @param mixed $component component config or component object
419+
* @return bool if component is DB connection
420+
*/
421+
private function isDbConnection($name,$component)
422+
{
423+
if(is_array($component))
424+
{
425+
if($component['class']=='CDbConnection')
426+
return true;
427+
else
428+
$component=Yii::app()->getComponent($name);
429+
}
430+
431+
return $component instanceof CDbConnection;
432+
}
395433
}

‎framework/gii/generators/model/templates/default/model.php

+10
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ public static function model($className=__CLASS__)
6161
{
6262
return parent::model($className);
6363
}
64+
<?php if($connectionId!='db'):?>
65+
66+
/**
67+
* @return CDbConnection database connection
68+
*/
69+
public function getDbConnection()
70+
{
71+
return Yii::app()-><?php echo $connectionId ?>;
72+
}
73+
<?php endif?>
6474

6575
/**
6676
* @return string the associated database table name

‎framework/gii/generators/model/views/index.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@
3838

3939
<?php $form=$this->beginWidget('CCodeForm', array('model'=>$model)); ?>
4040

41+
<div class="row sticky">
42+
<?php echo $form->labelEx($model, 'connectionId')?>
43+
<?php echo $form->dropDownList($model, 'connectionId', $model->getConnectionList())?>
44+
<div class="tooltip">
45+
This refers to the database connection used.
46+
</div>
47+
<?php echo $form->error($model,'connectionId'); ?>
48+
</div>
4149
<div class="row sticky">
4250
<?php echo $form->labelEx($model,'tablePrefix'); ?>
4351
<?php echo $form->textField($model,'tablePrefix', array('size'=>65)); ?>
@@ -57,7 +65,7 @@
5765
'model'=>$model,
5866
'attribute'=>'tableName',
5967
'name'=>'tableName',
60-
'source'=>array_keys(Yii::app()->db->schema->getTables()),
68+
'source'=>array_keys(Yii::app()->{$model->connectionId}->schema->getTables()),
6169
'options'=>array(
6270
'minLength'=>'0',
6371
'focus' => 'js:function(event,ui) {

0 commit comments

Comments
 (0)
Please sign in to comment.