|
|
@ -8,6 +8,7 @@ |
|
|
|
namespace yii\gii\generators\model; |
|
|
|
namespace yii\gii\generators\model; |
|
|
|
|
|
|
|
|
|
|
|
use Yii; |
|
|
|
use Yii; |
|
|
|
|
|
|
|
use yii\db\ActiveRecord; |
|
|
|
use yii\db\Connection; |
|
|
|
use yii\db\Connection; |
|
|
|
use yii\gii\CodeFile; |
|
|
|
use yii\gii\CodeFile; |
|
|
|
use yii\helpers\Inflector; |
|
|
|
use yii\helpers\Inflector; |
|
|
@ -50,7 +51,7 @@ class Generator extends \yii\gii\Generator |
|
|
|
array('ns', 'validateNamespace'), |
|
|
|
array('ns', 'validateNamespace'), |
|
|
|
array('tableName', 'validateTableName'), |
|
|
|
array('tableName', 'validateTableName'), |
|
|
|
array('modelClass', 'validateModelClass'), |
|
|
|
array('modelClass', 'validateModelClass'), |
|
|
|
array('baseClass', 'validateBaseClass'), |
|
|
|
array('baseClass', 'validateClass', 'params' => array('extends' => ActiveRecord::className())), |
|
|
|
array('generateRelations, generateLabelsFromComments', 'boolean'), |
|
|
|
array('generateRelations, generateLabelsFromComments', 'boolean'), |
|
|
|
)); |
|
|
|
)); |
|
|
|
} |
|
|
|
} |
|
|
@ -101,6 +102,9 @@ class Generator extends \yii\gii\Generator |
|
|
|
return array('ns', 'db', 'baseClass', 'generateRelations', 'generateLabelsFromComments'); |
|
|
|
return array('ns', 'db', 'baseClass', 'generateRelations', 'generateLabelsFromComments'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @return Connection |
|
|
|
|
|
|
|
*/ |
|
|
|
public function getDbConnection() |
|
|
|
public function getDbConnection() |
|
|
|
{ |
|
|
|
{ |
|
|
|
return Yii::$app->{$this->db}; |
|
|
|
return Yii::$app->{$this->db}; |
|
|
@ -108,33 +112,18 @@ class Generator extends \yii\gii\Generator |
|
|
|
|
|
|
|
|
|
|
|
public function generate() |
|
|
|
public function generate() |
|
|
|
{ |
|
|
|
{ |
|
|
|
$db = $this->getDbConnection(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (($pos = strrpos($this->tableName, '.')) !== false) { |
|
|
|
|
|
|
|
$schema = substr($this->tableName, 0, $pos); |
|
|
|
|
|
|
|
$tableName = substr($this->tableName, $pos + 1); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$schema = ''; |
|
|
|
|
|
|
|
$tableName = $this->tableName; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (strpos($tableName, '*') !== false) { |
|
|
|
|
|
|
|
$tables = $db->getSchema()->getTableNames($schema); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$tables = array($db->getTableSchema($this->tableName, true)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$files = array(); |
|
|
|
$files = array(); |
|
|
|
|
|
|
|
foreach ($this->getTableNames() as $tableName) { |
|
|
|
foreach ($tables as $table) { |
|
|
|
$className = $this->generateClassName($tableName); |
|
|
|
$className = $this->generateClassName($table->name); |
|
|
|
$tableSchema = $this->getTableSchema($tableName); |
|
|
|
$params = array( |
|
|
|
$params = array( |
|
|
|
'tableName' => $schema === '' ? $tableName : $schema . '.' . $tableName, |
|
|
|
'tableName' => $tableName, |
|
|
|
'className' => $className, |
|
|
|
'className' => $className, |
|
|
|
'columns' => $table->columns, |
|
|
|
'tableSchema' => $tableSchema, |
|
|
|
'labels' => $this->generateLabels($table), |
|
|
|
'labels' => $this->generateLabels($tableSchema), |
|
|
|
); |
|
|
|
); |
|
|
|
$files[] = new CodeFile( |
|
|
|
$files[] = new CodeFile( |
|
|
|
Yii::getAlias($this->modelPath) . '/' . $className . '.php', |
|
|
|
Yii::getAlias('@' . $this->ns) . '/' . $className . '.php', |
|
|
|
$this->render('model.php', $params) |
|
|
|
$this->render('model.php', $params) |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
@ -142,25 +131,9 @@ class Generator extends \yii\gii\Generator |
|
|
|
return $files; |
|
|
|
return $files; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
|
|
* 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" |
|
|
|
|
|
|
|
* @param CDbTableSchema $table the table schema object |
|
|
|
|
|
|
|
* @return string the invalid table column name. Null if no error. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function checkColumns($table) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
foreach ($table->columns as $column) { |
|
|
|
|
|
|
|
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $column->name)) { |
|
|
|
|
|
|
|
return $table->name . '.' . $column->name; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getTableSchema($tableName) |
|
|
|
public function getTableSchema($tableName) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$db = $this->getDbConnection(); |
|
|
|
return $this->getDbConnection()->getTableSchema($tableName, true); |
|
|
|
return $db->getSchema()->getTable($tableName, true); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function generateLabels($table) |
|
|
|
public function generateLabels($table) |
|
|
@ -403,18 +376,22 @@ class Generator extends \yii\gii\Generator |
|
|
|
public function validateDb() |
|
|
|
public function validateDb() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (Yii::$app->hasComponent($this->db) === false || !(Yii::$app->getComponent($this->db) instanceof Connection)) { |
|
|
|
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.'); |
|
|
|
$this->addError('db', 'Database Connection ID must refer to a valid application component.'); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function validateNamespace() |
|
|
|
public function validateNamespace() |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
$path = Yii::getAlias('@' . ltrim($this->ns, '\\'), false); |
|
|
|
|
|
|
|
if ($path === false) { |
|
|
|
|
|
|
|
$this->addError('ns', 'Namespace must be associated with an existing directory.'); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function validateModelClass() |
|
|
|
public function validateModelClass() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if ($this->isReservedKeyword($this->modelClass)) { |
|
|
|
if ($this->isReservedKeyword($this->modelClass)) { |
|
|
|
$this->addError('modelClass', 'The name is a reserved PHP keyword.'); |
|
|
|
$this->addError('modelClass', 'Class name cannot be a reserved PHP keyword.'); |
|
|
|
} |
|
|
|
} |
|
|
|
if (strpos($this->tableName, '*') === false && $this->modelClass == '') { |
|
|
|
if (strpos($this->tableName, '*') === false && $this->modelClass == '') { |
|
|
|
$this->addError('modelClass', 'Model Class cannot be blank.'); |
|
|
|
$this->addError('modelClass', 'Model Class cannot be blank.'); |
|
|
@ -423,56 +400,41 @@ class Generator extends \yii\gii\Generator |
|
|
|
|
|
|
|
|
|
|
|
public function validateTableName() |
|
|
|
public function validateTableName() |
|
|
|
{ |
|
|
|
{ |
|
|
|
$invalidTables = array(); |
|
|
|
$tables = $this->getTableNames(); |
|
|
|
$invalidColumns = array(); |
|
|
|
if (empty($tables)) { |
|
|
|
|
|
|
|
$this->addError('tableName', "Table '{$this->tableName}' does not exist.'"); |
|
|
|
if ($this->tableName[strlen($this->tableName) - 1] === '*') { |
|
|
|
|
|
|
|
if (($pos = strrpos($this->tableName, '.')) !== false) { |
|
|
|
|
|
|
|
$schema = substr($this->tableName, 0, $pos); |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$schema = ''; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->modelClass = ''; |
|
|
|
|
|
|
|
$tables = $this->getDbConnection()->schema->getTables($schema); |
|
|
|
|
|
|
|
foreach ($tables as $table) { |
|
|
|
foreach ($tables as $table) { |
|
|
|
if ($this->tablePrefix == '' || strpos($table->name, $this->tablePrefix) === 0) { |
|
|
|
$class = $this->generateClassName($table); |
|
|
|
if (in_array(strtolower($table->name), self::$keywords)) { |
|
|
|
if ($this->isReservedKeyword($class)) { |
|
|
|
$invalidTables[] = $table->name; |
|
|
|
$this->addError('tableName', "Table '$table' would generate a class which is a reserved PHP keyword."); |
|
|
|
} |
|
|
|
break; |
|
|
|
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) { |
|
|
|
protected function getTableNames() |
|
|
|
$invalidColumns[] = $invalidColumn; |
|
|
|
{ |
|
|
|
} |
|
|
|
$db = $this->getDbConnection(); |
|
|
|
|
|
|
|
$tableNames = array(); |
|
|
|
|
|
|
|
if ($this->tableName[strlen($this->tableName) - 1] === '*') { |
|
|
|
|
|
|
|
if (($pos = strrpos($this->tableName, '.')) !== false) { |
|
|
|
|
|
|
|
$schema = substr($this->tableName, 0, $pos); |
|
|
|
|
|
|
|
$pattern = '/' . str_replace('*', '\w+', substr($this->tableName, $pos + 1)) . '/'; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$schema = ''; |
|
|
|
|
|
|
|
$pattern = '/' . str_replace('*', '\w+', $this->tableName) . '/'; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ($invalidTables != array()) { |
|
|
|
foreach ($db->schema->getTableNames($schema) as $table) { |
|
|
|
$this->addError('tableName', 'Model class cannot take a reserved PHP keyword! Table name: ' . implode(', ', $invalidTables) . "."); |
|
|
|
if (preg_match($pattern, $table)) { |
|
|
|
} |
|
|
|
$tableNames[] = $schema === '' ? $table : ($schema . '.' . $table); |
|
|
|
if ($invalidColumns != array()) { |
|
|
|
|
|
|
|
$this->addError('tableName', 'Column names that does not follow PHP variable naming convention: ' . implode(', ', $invalidColumns) . "."); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} elseif (($table = $db->getTableSchema($this->tableName, true)) !== null) { |
|
|
|
public function validateBaseClass() |
|
|
|
$tableNames[] = $this->tableName; |
|
|
|
{ |
|
|
|
|
|
|
|
$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."); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return $tableNames; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|