@ -8,6 +8,8 @@
namespace yii\gii\generators\model;
namespace yii\gii\generators\model;
use Yii;
use Yii;
use yii\base\InvalidConfigException;
use yii\gii\CodeFile;
/**
/**
*
*
@ -36,16 +38,15 @@ class Generator extends \yii\gii\Generator
public function getDescription()
public function getDescription()
{
{
return 'This generator generates a model class for the specified database table.';
return 'This generator generates an ActiveRecord class for the specified database table.';
}
}
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('tablePrefix, baseClass, tableName, modelClass, modelPath, connectionId', 'filter', 'filter' => 'trim'),
array('connectionId, tableName, modelPath, baseClass', 'required'),
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('tablePrefix, tableName, modelPath', 'match', 'pattern' => '/^(\w+[\w\.]*|\*?|\w+\.\*)$/', 'message' => '{attribute} should only contain word characters, dots, and an optional ending asterisk.'),
array('connectionId', 'validateConnectionId'),
array('tableName', 'validateTableName'),
array('tableName', 'validateTableName'),
array('tablePrefix, modelClass', 'match', 'pattern' => '/^[a-zA-Z_]\w*$/', 'message' => '{attribute} should only contain word characters.'),
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('baseClass', 'match', 'pattern' => '/^[a-zA-Z_][\w\\\\]*$/', 'message' => '{attribute} should only contain word characters and backslashes.'),
@ -57,7 +58,7 @@ class Generator extends \yii\gii\Generator
public function attributeLabels()
public function attributeLabels()
{
{
return array_merge(parent::attributeLabels(), array (
return array(
'tablePrefix' => 'Table Prefix',
'tablePrefix' => 'Table Prefix',
'tableName' => 'Table Name',
'tableName' => 'Table Name',
'modelPath' => 'Model Path',
'modelPath' => 'Model Path',
@ -66,7 +67,7 @@ class Generator extends \yii\gii\Generator
'buildRelations' => 'Build Relations',
'buildRelations' => 'Build Relations',
'commentsAsLabels' => 'Use Column Comments as Attribute Labels',
'commentsAsLabels' => 'Use Column Comments as Attribute Labels',
'connectionId' => 'Database Connection',
'connectionId' => 'Database Connection',
)) ;
);
}
}
public function requiredTemplates()
public function requiredTemplates()
@ -78,15 +79,14 @@ class Generator extends \yii\gii\Generator
public function stickyAttributes()
public function stickyAttributes()
{
{
return array('connectionId', ' tablePrefix', 'modelPath', 'baseClass', 'buildRelations', 'commentsAsLabels');
return array('tablePrefix', 'modelPath', 'baseClass', 'buildRelations', 'commentsAsLabels');
}
}
public function generate()
public function generate()
{
{
if (Yii::$app->{$this->connectionI d} === null) {
if (($db = Yii::$app->{$this->db }) === null) {
throw new CHttpException(500, 'A valid database connection is required to run this generator .');
throw new InvalidConfigException('The "db" property must refer to a valid DB connection .');
}
}
$this->tablePrefix = Yii::$app->{$this->connectionId}->tablePrefix;
if (($pos = strrpos($this->tableName, '.')) !== false) {
if (($pos = strrpos($this->tableName, '.')) !== false) {
$schema = substr($this->tableName, 0, $pos);
$schema = substr($this->tableName, 0, $pos);
@ -95,25 +95,16 @@ class Generator extends \yii\gii\Generator
$schema = '';
$schema = '';
$tableName = $this->tableName;
$tableName = $this->tableName;
}
}
if ($tableName[strlen($tableName) - 1] === '*') {
if (strpos($tableName, '*') !== false) {
$tables = Yii::$app->{$this->connectionId}->schema->getTables($schema);
$tables = $db->getSchema()->getTableSchemas($schema);
if ($this->tablePrefix != '') {
foreach ($tables as $i => $table) {
if (strpos($table->name, $this->tablePrefix) !== 0) {
unset($tables[$i]);
}
}
}
} else {
} else {
$tables = array($this->getTableSchema($this->tableNam e));
$tables = array($db->getTableSchema($this->tableName, true));
}
}
$this->files = array();
$files = array();
$templatePath = $this->templatePath;
$relations = $this->generateRelations();
$this->relations = $this->generateRelations();
foreach ($tables as $table) {
foreach ($tables as $table) {
$tableName = $this->removePrefix($table->name);
$className = $this->generateClassName($table->name);
$className = $this->generateClassName($table->name);
$params = array(
$params = array(
'tableName' => $schema === '' ? $tableName : $schema . '.' . $tableName,
'tableName' => $schema === '' ? $tableName : $schema . '.' . $tableName,
@ -122,13 +113,14 @@ class Generator extends \yii\gii\Generator
'labels' => $this->generateLabels($table),
'labels' => $this->generateLabels($table),
'rules' => $this->generateRules($table),
'rules' => $this->generateRules($table),
'relations' => isset($this->relations[$className]) ? $this->relations[$className] : array(),
'relations' => isset($this->relations[$className]) ? $this->relations[$className] : array(),
'connectionId' => $this->connectionId,
);
);
$this-> files[] = new C CodeFile(
$files[] = new CodeFile(
Yii::getPathOf Alias($this->modelPath) . '/' . $className . '.php',
Yii::getAlias($this->modelPath) . '/' . $className . '.php',
$this->render($templatePath . '/ model.php', $params)
$this->render('model.php', $params)
);
);
}
}
return $files;
}
}
public function validateTableName($attribute, $params)
public function validateTableName($attribute, $params)
@ -195,13 +187,6 @@ class Generator extends \yii\gii\Generator
}
}
}
}
public function validateModelPath($attribute, $params)
{
if (Yii::getPathOfAlias($this->modelPath) === false) {
$this->addError('modelPath', 'Model Path must be a valid path alias.');
}
}
public function validateBaseClass($attribute, $params)
public function validateBaseClass($attribute, $params)
{
{
$class = @Yii::import($this->baseClass, true);
$class = @Yii::import($this->baseClass, true);