2
2
3
3
class ModelCode extends CCodeModel
4
4
{
5
+ public $ connectionId ='db ' ;
5
6
public $ tablePrefix ;
6
7
public $ tableName ;
7
8
public $ modelClass ;
@@ -19,14 +20,14 @@ public function rules()
19
20
{
20
21
return array_merge (parent ::rules (), array (
21
22
array ('tablePrefix, baseClass, tableName, modelClass, modelPath ' , 'filter ' , 'filter ' =>'trim ' ),
22
- array ('tableName, modelPath, baseClass ' , 'required ' ),
23
+ array ('connectionId, tableName, modelPath, baseClass ' , 'required ' ),
23
24
array ('tablePrefix, tableName, modelPath ' , 'match ' , 'pattern ' =>'/^(\w+[\w\.]*|\*?|\w+\.\*)$/ ' , 'message ' =>'{attribute} should only contain word characters, dots, and an optional ending asterisk. ' ),
24
25
array ('tableName ' , 'validateTableName ' , 'skipOnError ' =>true ),
25
26
array ('tablePrefix, modelClass, baseClass ' , 'match ' , 'pattern ' =>'/^[a-zA-Z_]\w*$/ ' , 'message ' =>'{attribute} should only contain word characters. ' ),
26
27
array ('modelPath ' , 'validateModelPath ' , 'skipOnError ' =>true ),
27
28
array ('baseClass, modelClass ' , 'validateReservedWord ' , 'skipOnError ' =>true ),
28
29
array ('baseClass ' , 'validateBaseClass ' , 'skipOnError ' =>true ),
29
- array ('tablePrefix, modelPath, baseClass, buildRelations ' , 'sticky ' ),
30
+ array ('connectionId, tablePrefix, modelPath, baseClass, buildRelations ' , 'sticky ' ),
30
31
));
31
32
}
32
33
@@ -39,6 +40,7 @@ public function attributeLabels()
39
40
'modelClass ' =>'Model Class ' ,
40
41
'baseClass ' =>'Base Class ' ,
41
42
'buildRelations ' =>'Build Relations ' ,
43
+ 'connectionId ' =>'Database Connection ' ,
42
44
));
43
45
}
44
46
@@ -51,9 +53,9 @@ public function requiredTemplates()
51
53
52
54
public function init ()
53
55
{
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 ;
57
59
parent ::init ();
58
60
}
59
61
@@ -71,7 +73,7 @@ public function prepare()
71
73
}
72
74
if ($ tableName [strlen ($ tableName )-1 ]==='* ' )
73
75
{
74
- $ tables =Yii::app ()->db ->schema ->getTables ($ schema );
76
+ $ tables =Yii::app ()->{ $ this -> connectionId } ->schema ->getTables ($ schema );
75
77
if ($ this ->tablePrefix !='' )
76
78
{
77
79
foreach ($ tables as $ i =>$ table )
@@ -99,6 +101,7 @@ public function prepare()
99
101
'labels ' =>$ this ->generateLabels ($ table ),
100
102
'rules ' =>$ this ->generateRules ($ table ),
101
103
'relations ' =>isset ($ this ->relations [$ className ]) ? $ this ->relations [$ className ] : array (),
104
+ 'connectionId ' =>$ this ->connectionId ,
102
105
);
103
106
$ this ->files []=new CCodeFile (
104
107
Yii::getPathOfAlias ($ this ->modelPath ).'/ ' .$ className .'.php ' ,
@@ -120,7 +123,7 @@ public function validateTableName($attribute,$params)
120
123
$ schema ='' ;
121
124
122
125
$ this ->modelClass ='' ;
123
- $ tables =Yii::app ()->db ->schema ->getTables ($ schema );
126
+ $ tables =Yii::app ()->{ $ this -> connectionId } ->schema ->getTables ($ schema );
124
127
foreach ($ tables as $ table )
125
128
{
126
129
if ($ this ->tablePrefix =='' || strpos ($ table ->name ,$ this ->tablePrefix )===0 )
@@ -181,7 +184,7 @@ public function validateBaseClass($attribute,$params)
181
184
182
185
public function getTableSchema ($ tableName )
183
186
{
184
- return Yii::app ()->db ->getSchema ()->getTable ($ tableName );
187
+ return Yii::app ()->{ $ this -> connectionId } ->getSchema ()->getTable ($ tableName );
185
188
}
186
189
187
190
public function generateLabels ($ table )
@@ -248,14 +251,14 @@ public function getRelations($className)
248
251
249
252
protected function removePrefix ($ tableName ,$ addBrackets =true )
250
253
{
251
- if ($ addBrackets && Yii::app ()->db ->tablePrefix =='' )
254
+ if ($ addBrackets && Yii::app ()->{ $ this -> connectionId } ->tablePrefix =='' )
252
255
return $ tableName ;
253
- $ prefix =$ this ->tablePrefix !='' ? $ this ->tablePrefix : Yii::app ()->db ->tablePrefix ;
256
+ $ prefix =$ this ->tablePrefix !='' ? $ this ->tablePrefix : Yii::app ()->{ $ this -> connectionId } ->tablePrefix ;
254
257
if ($ prefix !='' )
255
258
{
256
- if ($ addBrackets && Yii::app ()->db ->tablePrefix !='' )
259
+ if ($ addBrackets && Yii::app ()->{ $ this -> connectionId } ->tablePrefix !='' )
257
260
{
258
- $ prefix =Yii::app ()->db ->tablePrefix ;
261
+ $ prefix =Yii::app ()->{ $ this -> connectionId } ->tablePrefix ;
259
262
$ lb ='{{ ' ;
260
263
$ rb ='}} ' ;
261
264
}
@@ -279,7 +282,7 @@ protected function generateRelations()
279
282
if (!$ this ->buildRelations )
280
283
return array ();
281
284
$ relations =array ();
282
- foreach (Yii::app ()->db ->schema ->getTables () as $ table )
285
+ foreach (Yii::app ()->{ $ this -> connectionId } ->schema ->getTables () as $ table )
283
286
{
284
287
if ($ this ->tablePrefix !='' && strpos ($ table ->name ,$ this ->tablePrefix )!==0 )
285
288
continue ;
@@ -385,11 +388,46 @@ protected function generateRelationName($tableName, $fkName, $multiple)
385
388
$ name .=ucfirst ($ names [$ i ]);
386
389
387
390
$ rawName =$ name ;
388
- $ table =Yii::app ()->db ->schema ->getTable ($ tableName );
391
+ $ table =Yii::app ()->{ $ this -> connectionId } ->schema ->getTable ($ tableName );
389
392
$ i =0 ;
390
393
while (isset ($ table ->columns [$ name ]))
391
394
$ name =$ rawName .($ i ++);
392
395
393
396
return $ name ;
394
397
}
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
+ }
395
433
}
0 commit comments