Browse Source

model generator WIP

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
48e3739b21
  1. 34
      framework/yii/gii/generators/model/Generator.php
  2. 2
      framework/yii/gii/generators/model/form.php

34
framework/yii/gii/generators/model/Generator.php

@ -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
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
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
'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
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
{ {
$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);

2
framework/yii/gii/generators/model/form.php

@ -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();

Loading…
Cancel
Save