Commit c835f17d by Qiang Xue

finished rule generation for model generator.

parent 6978a680
...@@ -10,6 +10,7 @@ namespace yii\gii\generators\model; ...@@ -10,6 +10,7 @@ namespace yii\gii\generators\model;
use Yii; use Yii;
use yii\db\ActiveRecord; use yii\db\ActiveRecord;
use yii\db\Connection; use yii\db\Connection;
use yii\db\Schema;
use yii\gii\CodeFile; use yii\gii\CodeFile;
use yii\helpers\Inflector; use yii\helpers\Inflector;
...@@ -121,6 +122,7 @@ class Generator extends \yii\gii\Generator ...@@ -121,6 +122,7 @@ class Generator extends \yii\gii\Generator
'className' => $className, 'className' => $className,
'tableSchema' => $tableSchema, 'tableSchema' => $tableSchema,
'labels' => $this->generateLabels($tableSchema), 'labels' => $this->generateLabels($tableSchema),
'rules' => $this->generateRules($tableSchema),
); );
$files[] = new CodeFile( $files[] = new CodeFile(
Yii::getAlias('@' . str_replace('\\', '/', $this->ns)) . '/' . $className . '.php', Yii::getAlias('@' . str_replace('\\', '/', $this->ns)) . '/' . $className . '.php',
...@@ -155,48 +157,56 @@ class Generator extends \yii\gii\Generator ...@@ -155,48 +157,56 @@ class Generator extends \yii\gii\Generator
return $labels; return $labels;
} }
/**
* @param \yii\db\TableSchema $table
* @return array
*/
public function generateRules($table) public function generateRules($table)
{ {
$rules = array(); $types = array();
$required = array(); $lengths = array();
$integers = array();
$numerical = array();
$length = array();
$safe = array();
foreach ($table->columns as $column) { foreach ($table->columns as $column) {
if ($column->autoIncrement) { if ($column->autoIncrement) {
continue; continue;
} }
$r = !$column->allowNull && $column->defaultValue === null; if (!$column->allowNull && $column->defaultValue === null) {
if ($r) { $types['required'][] = $column->name;
$required[] = $column->name;
} }
if ($column->type === 'integer') { switch ($column->type) {
$integers[] = $column->name; case Schema::TYPE_SMALLINT:
} elseif ($column->type === 'double') { case Schema::TYPE_INTEGER:
$numerical[] = $column->name; case Schema::TYPE_BIGINT:
} elseif ($column->type === 'string' && $column->size > 0) { $types['integer'][] = $column->name;
$length[$column->size][] = $column->name; break;
} elseif (!$column->isPrimaryKey && !$r) { case Schema::TYPE_BOOLEAN:
$safe[] = $column->name; $types['boolean'][] = $column->name;
break;
case Schema::TYPE_FLOAT:
case Schema::TYPE_DECIMAL:
case Schema::TYPE_MONEY:
$types['number'][] = $column->name;
break;
case Schema::TYPE_DATE:
case Schema::TYPE_TIME:
case Schema::TYPE_DATETIME:
case Schema::TYPE_TIMESTAMP:
$types['safe'][] = $column->name;
break;
default: // strings
if ($column->size > 0) {
$lengths[$column->size][] = $column->name;
} else {
$types['string'][] = $column->name;
}
} }
} }
if ($required !== array()) {
$rules[] = "array('" . implode(', ', $required) . "', 'required')"; $rules = array();
} foreach ($types as $type => $columns) {
if ($integers !== array()) { $rules[] = "array('" . implode(', ', $columns) . "', '$type')";
$rules[] = "array('" . implode(', ', $integers) . "', 'numerical', 'integerOnly'=>true)";
}
if ($numerical !== array()) {
$rules[] = "array('" . implode(', ', $numerical) . "', 'numerical')";
}
if ($length !== array()) {
foreach ($length as $len => $cols) {
$rules[] = "array('" . implode(', ', $cols) . "', 'length', 'max'=>$len)";
}
} }
if ($safe !== array()) { foreach ($lengths as $length => $columns) {
$rules[] = "array('" . implode(', ', $safe) . "', 'safe')"; $rules[] = "array('" . implode(', ', $columns) . "', 'string', 'max' => $length)";
} }
return $rules; return $rules;
...@@ -317,34 +327,6 @@ class Generator extends \yii\gii\Generator ...@@ -317,34 +327,6 @@ class Generator extends \yii\gii\Generator
&& $table->foreignKeys[$pk[0]][0] !== $table->foreignKeys[$pk[1]][0]); // and the foreign keys point different tables && $table->foreignKeys[$pk[0]][0] !== $table->foreignKeys[$pk[1]][0]); // and the foreign keys point different tables
} }
protected function generateClassName($tableName)
{
if (($pos = strrpos($tableName, '.')) !== false) {
$tableName = substr($tableName, $pos + 1);
}
$db = $this->getDbConnection();
$patterns = array();
if (strpos($this->tableName, '*') !== false) {
$pattern = $this->tableName;
if (($pos = strrpos($pattern, '.')) !== false) {
$pattern = substr($pattern, $pos + 1);
}
$patterns[] = '/^' . str_replace('*', '(\w+)', $pattern) . '$/';
}
if (!empty($db->tablePrefix)) {
$patterns[] = "/^{$db->tablePrefix}(.*?)|(.*?){$db->tablePrefix}$/";
}
$className = $tableName;
foreach ($patterns as $pattern) {
if (preg_match($pattern, $tableName, $matches)) {
$className = $matches[1];
}
}
return Inflector::id2camel($className, '_');
}
/** /**
* Generate a name for use as a relation name (inside relations() function in a model). * Generate a name for use as a relation name (inside relations() function in a model).
* @param string the name of the table to hold the relation * @param string the name of the table to hold the relation
...@@ -431,8 +413,14 @@ class Generator extends \yii\gii\Generator ...@@ -431,8 +413,14 @@ class Generator extends \yii\gii\Generator
} }
} }
private $_tableNames;
private $_classNames;
protected function getTableNames() protected function getTableNames()
{ {
if ($this->_tableNames !== null) {
return $this->_tableNames;
}
$db = $this->getDbConnection(); $db = $this->getDbConnection();
$tableNames = array(); $tableNames = array();
if (strpos($this->tableName, '*') !== false) { if (strpos($this->tableName, '*') !== false) {
...@@ -451,7 +439,42 @@ class Generator extends \yii\gii\Generator ...@@ -451,7 +439,42 @@ class Generator extends \yii\gii\Generator
} }
} elseif (($table = $db->getTableSchema($this->tableName, true)) !== null) { } elseif (($table = $db->getTableSchema($this->tableName, true)) !== null) {
$tableNames[] = $this->tableName; $tableNames[] = $this->tableName;
$this->_classNames[$this->tableName] = $this->modelClass;
}
return $this->_tableNames = $tableNames;
}
protected function generateClassName($tableName)
{
if (isset($this->_classNames[$tableName])) {
return $this->_classNames[$tableName];
}
if (($pos = strrpos($tableName, '.')) !== false) {
$tableName = substr($tableName, $pos + 1);
}
$db = $this->getDbConnection();
$patterns = array();
if (strpos($this->tableName, '*') !== false) {
$pattern = $this->tableName;
if (($pos = strrpos($pattern, '.')) !== false) {
$pattern = substr($pattern, $pos + 1);
}
$patterns[] = '/^' . str_replace('*', '(\w+)', $pattern) . '$/';
}
if (!empty($db->tablePrefix)) {
$patterns[] = "/^{$db->tablePrefix}(.*?)|(.*?){$db->tablePrefix}$/";
} else {
$patterns[] = "/^tbl_(.*?)$/";
}
$className = $tableName;
foreach ($patterns as $pattern) {
if (preg_match($pattern, $tableName, $matches)) {
$className = $matches[1];
}
} }
return $tableNames; return $this->_classNames[$tableName] = Inflector::id2camel($className, '_');
} }
} }
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
* @var string $className * @var string $className
* @var yii\db\TableSchema $tableSchema * @var yii\db\TableSchema $tableSchema
* @var string[] $labels * @var string[] $labels
* @var string[] $rules
* *
* - $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
...@@ -44,6 +45,14 @@ class <?php echo $className; ?> extends <?php echo '\\' . ltrim($generator->base ...@@ -44,6 +45,14 @@ class <?php echo $className; ?> extends <?php echo '\\' . ltrim($generator->base
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function rules()
{
return array(<?php echo "\n\t\t\t" . implode(",\n\t\t\t", $rules) . "\n\t\t"; ?>);
}
/**
* @inheritdoc
*/
public function attributeLabels() public function attributeLabels()
{ {
return array( return array(
......
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