Commit 48e3739b by Qiang Xue

model generator WIP

parent a6248b4a
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
namespace yii\gii\generators\model; namespace yii\gii\generators\model;
use Yii; use Yii;
use yii\base\InvalidConfigException;
use yii\db\Connection; use yii\db\Connection;
use yii\gii\CodeFile; use yii\gii\CodeFile;
use yii\helpers\Inflector; use yii\helpers\Inflector;
...@@ -26,7 +25,7 @@ class Generator extends \yii\gii\Generator ...@@ -26,7 +25,7 @@ class Generator extends \yii\gii\Generator
public $modelClass; public $modelClass;
public $baseClass = '\yii\db\ActiveRecord'; public $baseClass = '\yii\db\ActiveRecord';
public $generateRelations = true; public $generateRelations = true;
public $commentsAsLabels = false; public $generateLabelsFromComments = false;
public function getName() public function getName()
...@@ -46,13 +45,13 @@ class Generator extends \yii\gii\Generator ...@@ -46,13 +45,13 @@ class Generator extends \yii\gii\Generator
array('db, ns, tableName, baseClass', 'required'), array('db, ns, tableName, baseClass', 'required'),
array('db, modelClass', 'match', 'pattern' => '/^\w+$/', 'message' => 'Only word characters are allowed.'), array('db, modelClass', 'match', 'pattern' => '/^\w+$/', 'message' => 'Only word characters are allowed.'),
array('ns, baseClass', 'match', 'pattern' => '/^[\w\\\\]+$/', 'message' => 'Only word characters and backslashes are allowed.'), array('ns, baseClass', 'match', 'pattern' => '/^[\w\\\\]+$/', 'message' => 'Only word characters and backslashes are allowed.'),
array('tableName', 'match', 'pattern' => '/^(\w+\.)?[\w\.\*]+$/', 'message' => 'Only word characters, asterisks and dot are allowed.'), array('tableName', 'match', 'pattern' => '/^(\w+\.)?\w+\*?$/', 'message' => 'Only word characters, and optionally an asterisk and/or a dot are allowed.'),
array('db', 'validateDb'), array('db', 'validateDb'),
array('ns', 'validateNamespace'), array('ns', 'validateNamespace'),
array('tableName', 'validateTableName'), array('tableName', 'validateTableName'),
array('modelClass', 'validateModelClass'), array('modelClass', 'validateModelClass'),
array('baseClass', 'validateBaseClass'), array('baseClass', 'validateBaseClass'),
array('generateRelations, commentsAsLabels', 'boolean'), array('generateRelations, generateLabelsFromComments', 'boolean'),
)); ));
} }
...@@ -65,7 +64,28 @@ class Generator extends \yii\gii\Generator ...@@ -65,7 +64,28 @@ class Generator extends \yii\gii\Generator
'modelClass' => 'Model Class', 'modelClass' => 'Model Class',
'baseClass' => 'Base Class', 'baseClass' => 'Base Class',
'generateRelations' => 'Generate Relations', 'generateRelations' => 'Generate Relations',
'commentsAsLabels' => 'Use Column Comments as Attribute Labels', 'generateLabelsFromComments' => 'Generate Labels from DB Comments',
);
}
public function hints()
{
return array(
'ns' => 'This is the namespace of the ActiveRecord class to be generated, e.g., <code>app\models</code>',
'db' => 'This is the ID of the DB application component.',
'tableName' => 'This is the name of the DB table that the new ActiveRecord class is associated with, e.g. <code>tbl_post</code>.
The table name may consist of the DB schema part if needed, e.g. <code>public.tbl_post</code>.
The table name may contain an asterisk at the end to match multiple table names, e.g. <code>tbl_*</code>.
In this case, multiple ActiveRecord classes will be generated, one for each matching table name.',
'modelClass' => 'This is the name of the ActiveRecord class to be generated. The class name should not contain
the namespace part as it is specified in "Namespace". You do not need to specify the class name
if "Table Name" contains an asterisk at the end, in which case multiple ActiveRecord classes will be generated.',
'baseClass' => 'This is the base class of the new ActiveRecord class. It should be a fully qualified namespaced class name.',
'generateRelations' => 'This indicates whether the generator should generate relations based on
foreign key constraints it detects in the database. Note that if your database contains too many tables,
you may want to uncheck this option to accelerate the code generation process.',
'generateLabelsFromComments' => 'This indicates whether the generator should generate attribute labels
by using the comments of the corresponding DB columns.',
); );
} }
...@@ -78,7 +98,7 @@ class Generator extends \yii\gii\Generator ...@@ -78,7 +98,7 @@ class Generator extends \yii\gii\Generator
public function stickyAttributes() public function stickyAttributes()
{ {
return array('ns', 'db', 'baseClass', 'generateRelations', 'commentsAsLabels'); return array('ns', 'db', 'baseClass', 'generateRelations', 'generateLabelsFromComments');
} }
public function getDbConnection() public function getDbConnection()
...@@ -147,7 +167,7 @@ class Generator extends \yii\gii\Generator ...@@ -147,7 +167,7 @@ class Generator extends \yii\gii\Generator
{ {
$labels = array(); $labels = array();
foreach ($table->columns as $column) { foreach ($table->columns as $column) {
if ($this->commentsAsLabels && !empty($column->comment)) { if ($this->generateLabelsFromComments && !empty($column->comment)) {
$labels[$column->name] = $column->comment; $labels[$column->name] = $column->comment;
} else { } else {
$label = Inflector::camel2words($column->name); $label = Inflector::camel2words($column->name);
......
...@@ -11,4 +11,4 @@ echo $form->field($generator, 'ns'); ...@@ -11,4 +11,4 @@ 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, 'generateRelations')->checkbox(); echo $form->field($generator, 'generateRelations')->checkbox();
echo $form->field($generator, 'commentsAsLabels')->checkbox(); echo $form->field($generator, 'generateLabelsFromComments')->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