Commit 2a152cb5 by Qiang Xue

model generator WIP

parent 6f9ddcf4
......@@ -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 $attribute the attribute to be validated
* @param array $params validation parameters
* @param string $value the attribute to be validated
* @return boolean whether the value is a reserved PHP keyword.
*/
public function validateReservedWord($attribute, $params)
public function isReservedKeyword($value)
{
static $keywords = array(
'__class__',
......@@ -381,12 +380,6 @@ abstract class Generator extends Model
'while',
'xor',
);
$value = $this->$attribute;
foreach (explode('\\', strtolower($value)) as $name) {
if (in_array($name, $keywords)) {
$this->addError($attribute, $this->getAttributeLabel($attribute) . ' cannot take a reserved PHP keyword.');
return;
}
}
return in_array(strtolower($value), $keywords, true);
}
}
......@@ -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('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', 'validateReservedWord'),
array('ns', 'match', 'pattern' => '/^[\w\\\\]*$/', 'message' => 'Only word characters and backslashes are allowed.'),
));
}
......
......@@ -9,7 +9,9 @@ namespace yii\gii\generators\model;
use Yii;
use yii\base\InvalidConfigException;
use yii\db\Connection;
use yii\gii\CodeFile;
use yii\helpers\Inflector;
/**
*
......@@ -19,17 +21,13 @@ use yii\gii\CodeFile;
class Generator extends \yii\gii\Generator
{
public $db = 'db';
public $ns = 'app\models';
public $tableName;
public $modelClass;
public $baseClass = '\yii\db\ActiveRecord';
public $buildRelations = true;
public $generateRelations = true;
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()
{
......@@ -44,29 +42,30 @@ class Generator extends \yii\gii\Generator
public function rules()
{
return array_merge(parent::rules(), array(
array('tablePrefix, baseClass, tableName, modelClass, modelPath, connectionId', 'filter', 'filter' => 'trim'),
array('tableName, modelPath, 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, ns, tableName, modelClass, baseClass', 'filter', 'filter' => 'trim'),
array('db, ns, tableName, baseClass', 'required'),
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('tablePrefix, modelClass', 'match', 'pattern' => '/^[a-zA-Z_]\w*$/', 'message' => '{attribute} should only contain word characters.'),
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('modelClass', 'validateModelClass'),
array('baseClass', 'validateBaseClass'),
array('generateRelations, commentsAsLabels', 'boolean'),
));
}
public function attributeLabels()
{
return array(
'tablePrefix' => 'Table Prefix',
'ns' => 'Namespace',
'db' => 'Database Connection ID',
'tableName' => 'Table Name',
'modelPath' => 'Model Path',
'modelClass' => 'Model Class',
'baseClass' => 'Base Class',
'buildRelations' => 'Build Relations',
'generateRelations' => 'Generate Relations',
'commentsAsLabels' => 'Use Column Comments as Attribute Labels',
'connectionId' => 'Database Connection',
);
}
......@@ -79,15 +78,18 @@ class Generator extends \yii\gii\Generator
public function stickyAttributes()
{
return array('tablePrefix', 'modelPath', 'baseClass', 'buildRelations', 'commentsAsLabels');
return array('ns', 'db', 'baseClass', 'generateRelations', 'commentsAsLabels');
}
public function generate()
public function getDbConnection()
{
if (($db = Yii::$app->{$this->db}) === null) {
throw new InvalidConfigException('The "db" property must refer to a valid DB connection.');
return Yii::$app->{$this->db};
}
public function generate()
{
$db = $this->getDbConnection();
if (($pos = strrpos($this->tableName, '.')) !== false) {
$schema = substr($this->tableName, 0, $pos);
$tableName = substr($this->tableName, $pos + 1);
......@@ -96,23 +98,20 @@ class Generator extends \yii\gii\Generator
$tableName = $this->tableName;
}
if (strpos($tableName, '*') !== false) {
$tables = $db->getSchema()->getTableSchemas($schema);
$tables = $db->getSchema()->getTableNames($schema);
} else {
$tables = array($db->getTableSchema($this->tableName, true));
}
$files = array();
$relations = $this->generateRelations();
foreach ($tables as $table) {
$className = $this->generateClassName($table->name);
$params = array(
'tableName' => $schema === '' ? $tableName : $schema . '.' . $tableName,
'modelClass' => $className,
'className' => $className,
'columns' => $table->columns,
'labels' => $this->generateLabels($table),
'rules' => $this->generateRules($table),
'relations' => isset($this->relations[$className]) ? $this->relations[$className] : array(),
);
$files[] = new CodeFile(
Yii::getAlias($this->modelPath) . '/' . $className . '.php',
......@@ -123,55 +122,6 @@ class Generator extends \yii\gii\Generator
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
* 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
}
}
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)
{
$connection = Yii::$app->{$this->connectionId};
return $connection->getSchema()->getTable($tableName, $connection->schemaCachingDuration !== 0);
$db = $this->getDbConnection();
return $db->getSchema()->getTable($tableName, true);
}
public function generateLabels($table)
{
$labels = array();
foreach ($table->columns as $column) {
if ($this->commentsAsLabels && $column->comment) {
if ($this->commentsAsLabels && !empty($column->comment)) {
$labels[$column->name] = $column->comment;
} else {
$label = ucwords(trim(strtolower(str_replace(array('-', '_'), ' ', preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $column->name)))));
$label = preg_replace('/\s+/', ' ', $label);
$label = Inflector::camel2words($column->name);
if (strcasecmp(substr($label, -3), ' id') === 0) {
$label = substr($label, 0, -3);
}
if ($label === 'Id') {
$label = 'ID';
$label = substr($label, 0, -3) . ' ID';
}
$label = str_replace("'", "\\'", $label);
$labels[$column->name] = $label;
}
}
......@@ -306,7 +241,7 @@ class Generator extends \yii\gii\Generator
protected function generateRelations()
{
if (!$this->buildRelations) {
if (!$this->generateRelations) {
return array();
}
......@@ -445,10 +380,79 @@ class Generator extends \yii\gii\Generator
return $name;
}
public function validateConnectionId($attribute, $params)
public function validateDb()
{
if (Yii::$app->hasComponent($this->connectionId) === false || !(Yii::$app->getComponent($this->connectionId) instanceof CDbConnection)) {
$this->addError('connectionId', 'A valid database connection is required to run this generator.');
if (Yii::$app->hasComponent($this->db) === false || !(Yii::$app->getComponent($this->db) instanceof Connection)) {
$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 @@
echo $form->field($generator, 'tableName');
echo $form->field($generator, 'modelClass');
echo $form->field($generator, 'ns');
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();
<?php
/**
* 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)
* - $modelClass: the model class name
* - $columns: list of table columns (name=>CDbColumnSchema)
......@@ -9,51 +16,29 @@
* - $rules: list of validation rules
* - $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; ?>".
*
* The followings are the available columns in table '<?php echo $tableName; ?>':
<?php foreach($columns as $column): ?>
* @property <?php echo $column->type.' $'.$column->name."\n"; ?>
<?php endforeach; ?>
<?php if(!empty($relations)): ?>
* Attributes:
*
* The followings are the available model relations:
<?php foreach($relations as $name=>$relation): ?>
* @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 foreach ($columns as $column): ?>
* @property <?php echo "{$column->phpType} \${$column->name}\n"; ?>
<?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()
{
......@@ -61,103 +46,14 @@ class <?php echo $modelClass; ?> extends <?php echo $this->baseClass."\n"; ?>
}
/**
* @return array validation rules for model attributes.
*/
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)
* @inheritdoc
*/
public function attributeLabels()
{
return array(
<?php foreach($labels as $name=>$label): ?>
<?php echo "'$name' => '$label',\n"; ?>
<?php foreach ($labels as $name => $label): ?>
<?php echo "'$name' => '" . addslashes($label) . "',\n"; ?>
<?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