diff --git a/framework/gii/assets/css/main.css b/framework/gii/assets/css/main.css index f64c83578..c7c2d19ec 100644 --- a/framework/gii/assets/css/main.css +++ b/framework/gii/assets/css/main.css @@ -359,6 +359,12 @@ div.form .tooltip ul padding: 10px 0 0 20px; } +div.form .tooltip code +{ + color: #CA0EE3; + font-size:0.9em; +} + div.form.login { border: 1px solid #C9E0ED; diff --git a/framework/gii/generators/controller/views/index.php b/framework/gii/generators/controller/views/index.php index d6bd6736d..962bd3bed 100644 --- a/framework/gii/generators/controller/views/index.php +++ b/framework/gii/generators/controller/views/index.php @@ -15,9 +15,9 @@ one or several controller actions and their corresponding views.

Controller ID is case-sensitive. Below are some examples:
error($model,'controller'); ?> diff --git a/framework/gii/generators/crud/views/index.php b/framework/gii/generators/crud/views/index.php index a14975a6e..9e662aa10 100644 --- a/framework/gii/generators/crud/views/index.php +++ b/framework/gii/generators/crud/views/index.php @@ -28,8 +28,8 @@ $('#CrudCode_model').bind('keyup change', function(){ labelEx($model,'model'); ?> textField($model,'model',array('size'=>65)); ?>
- Model class is case-sensitive. It can be either a class name (e.g. "Post") - or the path alias of the class file (e.g. "application.models.Post"). + Model class is case-sensitive. It can be either a class name (e.g. Post) + or the path alias of the class file (e.g. application.models.Post). Note that if the former, the class must be auto-loadable.
error($model,'model'); ?> @@ -42,9 +42,9 @@ $('#CrudCode_model').bind('keyup change', function(){ Controller ID is case-sensitive. CRUD controllers are often named after the model class name that they are dealing with. Below are some examples: error($model,'controller'); ?> diff --git a/framework/gii/generators/form/views/index.php b/framework/gii/generators/form/views/index.php index 361b1a8c0..e48c45e78 100644 --- a/framework/gii/generators/form/views/index.php +++ b/framework/gii/generators/form/views/index.php @@ -12,8 +12,8 @@ labelEx($model,'model'); ?> textField($model,'model', array('size'=>65)); ?>
- Model class is case-sensitive. It can be either a class name (e.g. "Post") - or the path alias of the class file (e.g. "application.models.LoginForm"). + Model class is case-sensitive. It can be either a class name (e.g. Post) + or the path alias of the class file (e.g. application.models.LoginForm). Note that if the former, the class must be auto-loadable.
error($model,'model'); ?> @@ -23,7 +23,7 @@ textField($model,'viewName', array('size'=>65)); ?>
This refers to the name of the view script to be generated, for example, - 'site/contact', 'user/login'. The actual view script file will be generated + site/contact, user/login. The actual view script file will be generated under the View Path specified below.
error($model,'viewName'); ?> @@ -33,8 +33,8 @@ textField($model,'viewPath', array('size'=>65)); ?>
This refers to the directory that the new view script file should be generated under. - It should be specified in the form of a path alias, for example, 'application.views', - 'mymodule.views'. + It should be specified in the form of a path alias, for example, application.views, + mymodule.views.
error($model,'viewPath'); ?> @@ -43,8 +43,8 @@ textField($model,'scenario', array('size'=>65)); ?>
This refers to the scenario in which the model should be used to collect user input. - For example, a 'User' model can be used in both 'login' and 'register' scenarios. - To create a form for the login purpose, the scenario should be specified as 'login'. + For example, a User model can be used in both login and register scenarios. + To create a form for the login purpose, the scenario should be specified as login. Leave this empty if the model does not need to differentiate scenarios.
error($model,'scenario'); ?> diff --git a/framework/gii/generators/model/ModelCode.php b/framework/gii/generators/model/ModelCode.php index 91a93d67e..8a9f736dc 100644 --- a/framework/gii/generators/model/ModelCode.php +++ b/framework/gii/generators/model/ModelCode.php @@ -13,11 +13,11 @@ class ModelCode extends CCodeModel return array_merge(parent::rules(), array( array('tablePrefix, baseClass, tableName, modelClass, modelPath', 'filter', 'filter'=>'trim'), array('tableName, modelPath, modelClass, baseClass', 'required'), - array('tablePrefix, tableName, modelPath', 'match', 'pattern'=>'/^\w+[\w\.]*$/', 'message'=>'{attribute} should only contain word characters and dots.'), + array('tablePrefix, tableName, modelPath', 'match', 'pattern'=>'/^(\w+[\w\.]*|\*?|\w+\.\*)$/', 'message'=>'{attribute} should only contain word characters, dots, and an optional ending star.'), array('tablePrefix, modelClass, baseClass', 'match', 'pattern'=>'/^\w+$/', 'message'=>'{attribute} should only contain word characters.'), - array('tableName', 'validateTableName'), - array('modelPath', 'validateModelPath'), - array('baseClass', 'validateBaseClass'), + array('tableName', 'validateTableName', 'skipOnError'=>true), + array('modelPath', 'validateModelPath', 'skipOnError'=>true), + array('baseClass', 'validateBaseClass', 'skipOnError'=>true), array('tablePrefix, modelPath, baseClass', 'sticky'), )); } @@ -53,41 +53,58 @@ class ModelCode extends CCodeModel $this->files=array(); $templatePath=$this->templatePath; - $params=array( - 'tableName'=>$this->removePrefix($this->tableName), - 'modelClass'=>$this->modelClass, - 'columns'=>$this->getColumns(), - 'labels'=>$this->getLabels(), - 'rules'=>$this->getRules(), - 'relations'=>$this->getRelations(), - ); + if(($pos=strrpos($this->tableName,'.'))!==false) + { + $schema=substr($this->tableName,0,$pos); + $tableName=substr($this->tableName,$pos+1); + } + else + { + $schema=''; + $tableName=$this->tableName; + } + if($tableName[strlen($tableName)-1]==='*') + $tables=Yii::app()->db->schema->getTables($schema); + else + $tables=array($this->getTableSchema($this->tableName)); - $this->files[]=new CCodeFile( - Yii::getPathOfAlias($this->modelPath).'/'.$this->modelClass.'.php', - $this->render($templatePath.'/model.php', $params) - ); + $relations=$this->generateRelations(); + + foreach($tables as $table) + { + $tableName=$this->removePrefix($table->name); + $className=$this->generateClassName($table->name); + $params=array( + 'tableName'=>$schema==='' ? $tableName : $schema.'.'.$tableName, + 'modelClass'=>$className, + 'columns'=>$table->columns, + 'labels'=>$this->generateLabels($table), + 'rules'=>$this->generateRules($table), + 'relations'=>isset($relations[$className]) ? $relations[$className] : array(), + ); + $this->files[]=new CCodeFile( + Yii::getPathOfAlias($this->modelPath).'/'.$className.'.php', + $this->render($templatePath.'/model.php', $params) + ); + } } public function validateTableName($attribute,$params) { - if($this->hasErrors('tableName')) - return; - if($this->getTableSchema()===null) + if($this->tableName[strlen($this->tableName)-1]==='*') + $this->modelClass=''; + else if($this->getTableSchema($this->tableName)===null) $this->addError('tableName',"Table '{$this->tableName}' does not exist."); } public function validateModelPath($attribute,$params) { - if($this->hasErrors('modelPath')) - return; if(Yii::getPathOfAlias($this->modelPath)===false) $this->addError('modelPath','Model Path must be a valid path alias.'); } public function validateBaseClass($attribute,$params) { - if($this->hasErrors('baseClass')) - return; $class=@Yii::import($this->baseClass,true); if(!is_string($class) || !class_exists($class,false)) $this->addError('baseClass', "Class '{$this->baseClass}' does not exist or has syntax error."); @@ -95,20 +112,15 @@ class ModelCode extends CCodeModel $this->addError('baseClass', "'{$this->model}' must extend from CActiveRecord."); } - public function getTableSchema() + public function getTableSchema($tableName) { - return Yii::app()->db->getSchema()->getTable($this->tableName); + return Yii::app()->db->getSchema()->getTable($tableName); } - public function getColumns() - { - return Yii::app()->db->schema->getTable($this->tableName)->columns; - } - - public function getLabels() + public function generateLabels($table) { $labels=array(); - foreach($this->tableSchema->columns as $column) + foreach($table->columns as $column) { $label=ucwords(trim(strtolower(str_replace(array('-','_'),' ',preg_replace('/(?name))))); $label=preg_replace('/\s+/',' ',$label); @@ -121,11 +133,9 @@ class ModelCode extends CCodeModel return $labels; } - public function getRules() + public function generateRules($table) { $rules=array(); - $table=$this->tableSchema; - $required=array(); $integers=array(); $numerical=array(); @@ -164,10 +174,10 @@ class ModelCode extends CCodeModel return $rules; } - public function getRelations() + public function getRelations($className) { $relations=$this->generateRelations(); - return isset($relations[$this->modelClass]) ? $relations[$this->modelClass] : array(); + return isset($relations[$className]) ? $relations[$className] : array(); } protected function removePrefix($tableName,$addBrackets=true) @@ -261,7 +271,7 @@ class ModelCode extends CCodeModel protected function generateClassName($tableName) { - if($this->tableSchema->name===$tableName) + if($this->getTableSchema($tableName)->name===$tableName) return $this->modelClass; $tableName=$this->removePrefix($tableName,false); diff --git a/framework/gii/generators/model/views/index.php b/framework/gii/generators/model/views/index.php index f8ba66b88..d5d6af401 100644 --- a/framework/gii/generators/model/views/index.php +++ b/framework/gii/generators/model/views/index.php @@ -5,8 +5,9 @@ $('#ModelCode_modelClass').change(function(){ }); $('#ModelCode_tableName').bind('keyup change', function(){ var model=$('#ModelCode_modelClass'); + var tableName=$(this).val(); + $('.form .row.model-class').toggle(tableName.substring(tableName.length-1)!='*'); if(!model.data('changed')) { - var tableName=$(this).val(); var i=tableName.lastIndexOf('.'); if(i>=0) tableName=tableName.substring(i+1); @@ -21,6 +22,7 @@ $('#ModelCode_tableName').bind('keyup change', function(){ model.val(modelClass); } }); +$('.form .row.model-class').toggle($('#ModelCode_tableName').val().match(/\*$/)); "); ?>

Model Generator

@@ -37,10 +39,12 @@ $('#ModelCode_tableName').bind('keyup change', function(){ labelEx($model,'tablePrefix'); ?> textField($model,'tablePrefix', array('size'=>65)); ?>
+

This refers to the prefix name that is shared by all database tables. Setting this property mainly affects how model classes are named based on - the table names. For example, a table prefix 'tbl_' with a table name 'tbl_post' - will generate a model class named 'Post'. + the table names. For example, a table prefix tbl_ with a table name tbl_post + will generate a model class named Post. +


Leave this field empty if your database tables do not use common prefix.
@@ -51,15 +55,17 @@ $('#ModelCode_tableName').bind('keyup change', function(){ textField($model,'tableName', array('size'=>65)); ?>
This refers to the table name that a new model class should be generated for - (e.g. 'tbl_user'). It can contain schema name, if needed (e.g. 'public.tbl_post'). + (e.g. tbl_user). It can contain schema name, if needed (e.g. public.tbl_post). + You may also enter * (or schemaName.* for a particular DB schema) + to generate a model class for EVERY table.
error($model,'tableName'); ?> -
+
labelEx($model,'modelClass'); ?> textField($model,'modelClass', array('size'=>65)); ?>
- This is the name of the model class to be generated (e.g. 'Post', 'Comment'). + This is the name of the model class to be generated (e.g. Post, Comment). It is case-sensitive.
error($model,'modelClass'); ?> @@ -78,7 +84,7 @@ $('#ModelCode_tableName').bind('keyup change', function(){ textField($model,'modelPath', array('size'=>65)); ?>
This refers to the directory that the new model class file should be generated under. - It should be specified in the form of a path alias, for example, 'application.models'. + It should be specified in the form of a path alias, for example, application.models.
error($model,'modelPath'); ?>
diff --git a/framework/gii/generators/module/views/index.php b/framework/gii/generators/module/views/index.php index 9b68982c8..e000242cb 100644 --- a/framework/gii/generators/module/views/index.php +++ b/framework/gii/generators/module/views/index.php @@ -14,8 +14,8 @@
Module ID is case-sensitive. It should only contain word characters. The generated module class will be named after the module ID. - For example, a module ID "forum" will generate the module class - "ForumModule". + For example, a module ID forum will generate the module class + ForumModule.
error($model,'moduleID'); ?>