Commit 2a152cb5 by Qiang Xue

model generator WIP

parent 6f9ddcf4
...@@ -294,11 +294,10 @@ abstract class Generator extends Model ...@@ -294,11 +294,10 @@ abstract class Generator extends Model
} }
/** /**
* Validates an attribute to make sure it is not taking a PHP reserved keyword. * @param string $value the attribute to be validated
* @param string $attribute the attribute to be validated * @return boolean whether the value is a reserved PHP keyword.
* @param array $params validation parameters
*/ */
public function validateReservedWord($attribute, $params) public function isReservedKeyword($value)
{ {
static $keywords = array( static $keywords = array(
'__class__', '__class__',
...@@ -381,12 +380,6 @@ abstract class Generator extends Model ...@@ -381,12 +380,6 @@ abstract class Generator extends Model
'while', 'while',
'xor', 'xor',
); );
$value = $this->$attribute; return in_array(strtolower($value), $keywords, true);
foreach (explode('\\', strtolower($value)) as $name) {
if (in_array($name, $keywords)) {
$this->addError($attribute, $this->getAttributeLabel($attribute) . ' cannot take a reserved PHP keyword.');
return;
}
}
} }
} }
...@@ -65,7 +65,6 @@ class Generator extends \yii\gii\Generator ...@@ -65,7 +65,6 @@ class Generator extends \yii\gii\Generator
array('controller', 'match', 'pattern' => '/^[a-z\\-\\/]*$/', 'message' => 'Only a-z, dashes (-) and slashes (/) are allowed.'), array('controller', 'match', 'pattern' => '/^[a-z\\-\\/]*$/', 'message' => 'Only a-z, dashes (-) and slashes (/) are allowed.'),
array('actions', 'match', 'pattern' => '/^[a-z\\-,\\s]*$/', 'message' => 'Only a-z, dashes (-), spaces and commas are allowed.'), array('actions', 'match', 'pattern' => '/^[a-z\\-,\\s]*$/', 'message' => 'Only a-z, dashes (-), spaces and commas are allowed.'),
array('baseClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'), array('baseClass', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'),
array('baseClass', 'validateReservedWord'),
array('ns', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'), array('ns', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'),
)); ));
} }
......
...@@ -9,7 +9,9 @@ namespace yii\gii\generators\model; ...@@ -9,7 +9,9 @@ namespace yii\gii\generators\model;
use Yii; use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\db\Connection;
use yii\gii\CodeFile; use yii\gii\CodeFile;
use yii\helpers\Inflector;
/** /**
* *
...@@ -19,17 +21,13 @@ use yii\gii\CodeFile; ...@@ -19,17 +21,13 @@ use yii\gii\CodeFile;
class Generator extends \yii\gii\Generator class Generator extends \yii\gii\Generator
{ {
public $db = 'db'; public $db = 'db';
public $ns = 'app\models';
public $tableName; public $tableName;
public $modelClass; public $modelClass;
public $baseClass = '\yii\db\ActiveRecord'; public $baseClass = '\yii\db\ActiveRecord';
public $buildRelations = true; public $generateRelations = true;
public $commentsAsLabels = false; public $commentsAsLabels = false;
/**
* @var array list of candidate relation code. The array are indexed by AR class names and relation names.
* Each element represents the code of the one relation in one AR class.
*/
protected $relations;
public function getName() public function getName()
{ {
...@@ -44,29 +42,30 @@ class Generator extends \yii\gii\Generator ...@@ -44,29 +42,30 @@ class Generator extends \yii\gii\Generator
public function rules() public function rules()
{ {
return array_merge(parent::rules(), array( return array_merge(parent::rules(), array(
array('tablePrefix, baseClass, tableName, modelClass, modelPath, connectionId', 'filter', 'filter' => 'trim'), array('db, ns, tableName, modelClass, baseClass', 'filter', 'filter' => 'trim'),
array('tableName, modelPath, baseClass', 'required'), array('db, ns, tableName, baseClass', 'required'),
array('tablePrefix, tableName, modelPath', 'match', 'pattern' => '/^(\w+[\w\.]*|\*?|\w+\.\*)$/', 'message' => '{attribute} should only contain word characters, dots, and an optional ending asterisk.'), 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('tableName', 'match', 'pattern' => '/^(\w+\.)?[\w\.\*]+$/', 'message' => 'Only word characters, asterisks and dot are allowed.'),
array('db', 'validateDb'),
array('ns', 'validateNamespace'),
array('tableName', 'validateTableName'), array('tableName', 'validateTableName'),
array('tablePrefix, modelClass', 'match', 'pattern' => '/^[a-zA-Z_]\w*$/', 'message' => '{attribute} should only contain word characters.'), array('modelClass', 'validateModelClass'),
array('baseClass', 'match', 'pattern' => '/^[a-zA-Z_][\w\\\\]*$/', 'message' => '{attribute} should only contain word characters and backslashes.'),
array('modelPath', 'validateModelPath'),
array('baseClass, modelClass', 'validateReservedWord'),
array('baseClass', 'validateBaseClass'), array('baseClass', 'validateBaseClass'),
array('generateRelations, commentsAsLabels', 'boolean'),
)); ));
} }
public function attributeLabels() public function attributeLabels()
{ {
return array( return array(
'tablePrefix' => 'Table Prefix', 'ns' => 'Namespace',
'db' => 'Database Connection ID',
'tableName' => 'Table Name', 'tableName' => 'Table Name',
'modelPath' => 'Model Path',
'modelClass' => 'Model Class', 'modelClass' => 'Model Class',
'baseClass' => 'Base Class', 'baseClass' => 'Base Class',
'buildRelations' => 'Build Relations', 'generateRelations' => 'Generate Relations',
'commentsAsLabels' => 'Use Column Comments as Attribute Labels', 'commentsAsLabels' => 'Use Column Comments as Attribute Labels',
'connectionId' => 'Database Connection',
); );
} }
...@@ -79,14 +78,17 @@ class Generator extends \yii\gii\Generator ...@@ -79,14 +78,17 @@ class Generator extends \yii\gii\Generator
public function stickyAttributes() public function stickyAttributes()
{ {
return array('tablePrefix', 'modelPath', 'baseClass', 'buildRelations', 'commentsAsLabels'); return array('ns', 'db', 'baseClass', 'generateRelations', 'commentsAsLabels');
}
public function getDbConnection()
{
return Yii::$app->{$this->db};
} }
public function generate() public function generate()
{ {
if (($db = Yii::$app->{$this->db}) === null) { $db = $this->getDbConnection();
throw new InvalidConfigException('The "db" property must refer to a valid DB connection.');
}
if (($pos = strrpos($this->tableName, '.')) !== false) { if (($pos = strrpos($this->tableName, '.')) !== false) {
$schema = substr($this->tableName, 0, $pos); $schema = substr($this->tableName, 0, $pos);
...@@ -96,23 +98,20 @@ class Generator extends \yii\gii\Generator ...@@ -96,23 +98,20 @@ class Generator extends \yii\gii\Generator
$tableName = $this->tableName; $tableName = $this->tableName;
} }
if (strpos($tableName, '*') !== false) { if (strpos($tableName, '*') !== false) {
$tables = $db->getSchema()->getTableSchemas($schema); $tables = $db->getSchema()->getTableNames($schema);
} else { } else {
$tables = array($db->getTableSchema($this->tableName, true)); $tables = array($db->getTableSchema($this->tableName, true));
} }
$files = array(); $files = array();
$relations = $this->generateRelations();
foreach ($tables as $table) { foreach ($tables as $table) {
$className = $this->generateClassName($table->name); $className = $this->generateClassName($table->name);
$params = array( $params = array(
'tableName' => $schema === '' ? $tableName : $schema . '.' . $tableName, 'tableName' => $schema === '' ? $tableName : $schema . '.' . $tableName,
'modelClass' => $className, 'className' => $className,
'columns' => $table->columns, 'columns' => $table->columns,
'labels' => $this->generateLabels($table), 'labels' => $this->generateLabels($table),
'rules' => $this->generateRules($table),
'relations' => isset($this->relations[$className]) ? $this->relations[$className] : array(),
); );
$files[] = new CodeFile( $files[] = new CodeFile(
Yii::getAlias($this->modelPath) . '/' . $className . '.php', Yii::getAlias($this->modelPath) . '/' . $className . '.php',
...@@ -123,55 +122,6 @@ class Generator extends \yii\gii\Generator ...@@ -123,55 +122,6 @@ class Generator extends \yii\gii\Generator
return $files; return $files;
} }
public function validateTableName($attribute, $params)
{
if ($this->hasErrors()) {
return;
}
$invalidTables = array();
$invalidColumns = array();
if ($this->tableName[strlen($this->tableName) - 1] === '*') {
if (($pos = strrpos($this->tableName, '.')) !== false) {
$schema = substr($this->tableName, 0, $pos);
} else {
$schema = '';
}
$this->modelClass = '';
$tables = Yii::$app->{$this->connectionId}->schema->getTables($schema);
foreach ($tables as $table) {
if ($this->tablePrefix == '' || strpos($table->name, $this->tablePrefix) === 0) {
if (in_array(strtolower($table->name), self::$keywords)) {
$invalidTables[] = $table->name;
}
if (($invalidColumn = $this->checkColumns($table)) !== null) {
$invalidColumns[] = $invalidColumn;
}
}
}
} else {
if (($table = $this->getTableSchema($this->tableName)) === null) {
$this->addError('tableName', "Table '{$this->tableName}' does not exist.");
}
if ($this->modelClass === '') {
$this->addError('modelClass', 'Model Class cannot be blank.');
}
if (!$this->hasErrors($attribute) && ($invalidColumn = $this->checkColumns($table)) !== null) {
$invalidColumns[] = $invalidColumn;
}
}
if ($invalidTables != array()) {
$this->addError('tableName', 'Model class cannot take a reserved PHP keyword! Table name: ' . implode(', ', $invalidTables) . ".");
}
if ($invalidColumns != array()) {
$this->addError('tableName', 'Column names that does not follow PHP variable naming convention: ' . implode(', ', $invalidColumns) . ".");
}
}
/* /*
* Check that all database field names conform to PHP variable naming rules * Check that all database field names conform to PHP variable naming rules
* For example mysql allows field name like "2011aa", but PHP does not allow variable like "$model->2011aa" * For example mysql allows field name like "2011aa", but PHP does not allow variable like "$model->2011aa"
...@@ -187,38 +137,23 @@ class Generator extends \yii\gii\Generator ...@@ -187,38 +137,23 @@ class Generator extends \yii\gii\Generator
} }
} }
public function validateBaseClass($attribute, $params)
{
$class = @Yii::import($this->baseClass, true);
if (!is_string($class) || !$this->classExists($class)) {
$this->addError('baseClass', "Class '{$this->baseClass}' does not exist or has syntax error.");
} elseif ($class !== 'CActiveRecord' && !is_subclass_of($class, 'CActiveRecord')) {
$this->addError('baseClass', "'{$this->model}' must extend from CActiveRecord.");
}
}
public function getTableSchema($tableName) public function getTableSchema($tableName)
{ {
$connection = Yii::$app->{$this->connectionId}; $db = $this->getDbConnection();
return $connection->getSchema()->getTable($tableName, $connection->schemaCachingDuration !== 0); return $db->getSchema()->getTable($tableName, true);
} }
public function generateLabels($table) public function generateLabels($table)
{ {
$labels = array(); $labels = array();
foreach ($table->columns as $column) { foreach ($table->columns as $column) {
if ($this->commentsAsLabels && $column->comment) { if ($this->commentsAsLabels && !empty($column->comment)) {
$labels[$column->name] = $column->comment; $labels[$column->name] = $column->comment;
} else { } else {
$label = ucwords(trim(strtolower(str_replace(array('-', '_'), ' ', preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $column->name))))); $label = Inflector::camel2words($column->name);
$label = preg_replace('/\s+/', ' ', $label);
if (strcasecmp(substr($label, -3), ' id') === 0) { if (strcasecmp(substr($label, -3), ' id') === 0) {
$label = substr($label, 0, -3); $label = substr($label, 0, -3) . ' ID';
}
if ($label === 'Id') {
$label = 'ID';
} }
$label = str_replace("'", "\\'", $label);
$labels[$column->name] = $label; $labels[$column->name] = $label;
} }
} }
...@@ -306,7 +241,7 @@ class Generator extends \yii\gii\Generator ...@@ -306,7 +241,7 @@ class Generator extends \yii\gii\Generator
protected function generateRelations() protected function generateRelations()
{ {
if (!$this->buildRelations) { if (!$this->generateRelations) {
return array(); return array();
} }
...@@ -445,10 +380,79 @@ class Generator extends \yii\gii\Generator ...@@ -445,10 +380,79 @@ class Generator extends \yii\gii\Generator
return $name; return $name;
} }
public function validateConnectionId($attribute, $params) public function validateDb()
{ {
if (Yii::$app->hasComponent($this->connectionId) === false || !(Yii::$app->getComponent($this->connectionId) instanceof CDbConnection)) { if (Yii::$app->hasComponent($this->db) === false || !(Yii::$app->getComponent($this->db) instanceof Connection)) {
$this->addError('connectionId', 'A valid database connection is required to run this generator.'); $this->addError('db', 'A valid database connection is required to run this generator.');
}
}
public function validateNamespace()
{
}
public function validateModelClass()
{
if ($this->isReservedKeyword($this->modelClass)) {
$this->addError('modelClass', 'The name is a reserved PHP keyword.');
}
if (strpos($this->tableName, '*') === false && $this->modelClass == '') {
$this->addError('modelClass', 'Model Class cannot be blank.');
}
}
public function validateTableName()
{
$invalidTables = array();
$invalidColumns = array();
if ($this->tableName[strlen($this->tableName) - 1] === '*') {
if (($pos = strrpos($this->tableName, '.')) !== false) {
$schema = substr($this->tableName, 0, $pos);
} else {
$schema = '';
}
$this->modelClass = '';
$tables = $this->getDbConnection()->schema->getTables($schema);
foreach ($tables as $table) {
if ($this->tablePrefix == '' || strpos($table->name, $this->tablePrefix) === 0) {
if (in_array(strtolower($table->name), self::$keywords)) {
$invalidTables[] = $table->name;
}
if (($invalidColumn = $this->checkColumns($table)) !== null) {
$invalidColumns[] = $invalidColumn;
}
}
}
} else {
if (($table = $this->getTableSchema($this->tableName)) === null) {
$this->addError('tableName', "Table '{$this->tableName}' does not exist.");
}
if ($this->modelClass === '') {
$this->addError('modelClass', 'Model Class cannot be blank.');
}
if (!$this->hasErrors($attribute) && ($invalidColumn = $this->checkColumns($table)) !== null) {
$invalidColumns[] = $invalidColumn;
}
}
if ($invalidTables != array()) {
$this->addError('tableName', 'Model class cannot take a reserved PHP keyword! Table name: ' . implode(', ', $invalidTables) . ".");
}
if ($invalidColumns != array()) {
$this->addError('tableName', 'Column names that does not follow PHP variable naming convention: ' . implode(', ', $invalidColumns) . ".");
}
}
public function validateBaseClass()
{
$class = @Yii::import($this->baseClass, true);
if (!is_string($class) || !$this->classExists($class)) {
$this->addError('baseClass', "Class '{$this->baseClass}' does not exist or has syntax error.");
} elseif ($class !== 'CActiveRecord' && !is_subclass_of($class, 'CActiveRecord')) {
$this->addError('baseClass', "'{$this->model}' must extend from CActiveRecord.");
} }
} }
} }
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
echo $form->field($generator, 'tableName'); echo $form->field($generator, 'tableName');
echo $form->field($generator, 'modelClass'); echo $form->field($generator, 'modelClass');
echo $form->field($generator, 'ns');
echo $form->field($generator, 'baseClass'); echo $form->field($generator, 'baseClass');
echo $form->field($generator, 'buildRelations')->checkbox(); echo $form->field($generator, 'db');
echo $form->field($generator, 'generateRelations')->checkbox();
echo $form->field($generator, 'commentsAsLabels')->checkbox(); echo $form->field($generator, 'commentsAsLabels')->checkbox();
<?php <?php
/** /**
* This is the template for generating the model class of a specified table. * This is the template for generating the model class of a specified table.
* - $this: the ModelCode object *
* @var yii\base\View $this
* @var yii\gii\generators\model\Generator $generator
* @var string $tableName
* @var string $className
* @var yii\db\ColumnSchema[] $columns
* @var string[] $labels
*
* - $tableName: the table name for this class (prefix is already removed if necessary) * - $tableName: the table name for this class (prefix is already removed if necessary)
* - $modelClass: the model class name * - $modelClass: the model class name
* - $columns: list of table columns (name=>CDbColumnSchema) * - $columns: list of table columns (name=>CDbColumnSchema)
...@@ -9,51 +16,29 @@ ...@@ -9,51 +16,29 @@
* - $rules: list of validation rules * - $rules: list of validation rules
* - $relations: list of relations (name=>relation declaration) * - $relations: list of relations (name=>relation declaration)
*/ */
$pos = strrpos($className, '\\');
$ns = ltrim(substr($className, 0, $pos), '\\');
$className = substr($className, $pos + 1);
echo "<?php\n";
?> ?>
<?php echo "<?php\n"; ?>
namespace <?php echo $ns; ?>;
/** /**
* This is the model class for table "<?php echo $tableName; ?>". * This is the model class for table "<?php echo $tableName; ?>".
* *
* The followings are the available columns in table '<?php echo $tableName; ?>': * Attributes:
<?php foreach($columns as $column): ?>
* @property <?php echo $column->type.' $'.$column->name."\n"; ?>
<?php endforeach; ?>
<?php if(!empty($relations)): ?>
* *
* The followings are the available model relations: <?php foreach ($columns as $column): ?>
<?php foreach($relations as $name=>$relation): ?> * @property <?php echo "{$column->phpType} \${$column->name}\n"; ?>
* @property <?php
if (preg_match("~^array\(self::([^,]+), '([^']+)', '([^']+)'\)$~", $relation, $matches))
{
$relationType = $matches[1];
$relationModel = $matches[2];
switch($relationType){
case 'HAS_ONE':
echo $relationModel.' $'.$name."\n";
break;
case 'BELONGS_TO':
echo $relationModel.' $'.$name."\n";
break;
case 'HAS_MANY':
echo $relationModel.'[] $'.$name."\n";
break;
case 'MANY_MANY':
echo $relationModel.'[] $'.$name."\n";
break;
default:
echo 'mixed $'.$name."\n";
}
}
?>
<?php endforeach; ?> <?php endforeach; ?>
<?php endif; ?>
*/ */
class <?php echo $modelClass; ?> extends <?php echo $this->baseClass."\n"; ?> class <?php echo $className; ?> extends <?php echo '\\' . ltrim($generator->baseClass, '\\') . "\n"; ?>
{ {
/** /**
* @return string the associated database table name * @inheritdoc
*/ */
public function tableName() public function tableName()
{ {
...@@ -61,103 +46,14 @@ class <?php echo $modelClass; ?> extends <?php echo $this->baseClass."\n"; ?> ...@@ -61,103 +46,14 @@ class <?php echo $modelClass; ?> extends <?php echo $this->baseClass."\n"; ?>
} }
/** /**
* @return array validation rules for model attributes. * @inheritdoc
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
<?php foreach($rules as $rule): ?>
<?php echo $rule.",\n"; ?>
<?php endforeach; ?>
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('<?php echo implode(', ', array_keys($columns)); ?>', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
<?php foreach($relations as $name=>$relation): ?>
<?php echo "'$name' => $relation,\n"; ?>
<?php endforeach; ?>
);
}
/**
* @return array customized attribute labels (name=>label)
*/ */
public function attributeLabels() public function attributeLabels()
{ {
return array( return array(
<?php foreach($labels as $name=>$label): ?> <?php foreach ($labels as $name => $label): ?>
<?php echo "'$name' => '$label',\n"; ?> <?php echo "'$name' => '" . addslashes($label) . "',\n"; ?>
<?php endforeach; ?> <?php endforeach; ?>
); );
} }
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
<?php
foreach($columns as $name=>$column)
{
if($column->type==='string')
{
echo "\t\t\$criteria->compare('$name',\$this->$name,true);\n";
}
else
{
echo "\t\t\$criteria->compare('$name',\$this->$name);\n";
}
}
?>
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
<?php if($connectionId!='db'):?>
/**
* @return CDbConnection the database connection used for this class
*/
public function getDbConnection()
{
return Yii::app()-><?php echo $connectionId ?>;
}
<?php endif?>
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return <?php echo $modelClass; ?> the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
} }
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