You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

633 lines
24KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\gii\generators\model;
  8. use Yii;
  9. use yii\db\ActiveRecord;
  10. use yii\db\Connection;
  11. use yii\db\Schema;
  12. use yii\gii\CodeFile;
  13. use yii\helpers\Inflector;
  14. use yii\base\NotSupportedException;
  15. /**
  16. * This generator will generate one or multiple ActiveRecord classes for the specified database table.
  17. *
  18. * @author Qiang Xue <qiang.xue@gmail.com>
  19. * @since 2.0
  20. */
  21. class Generator extends \yii\gii\Generator
  22. {
  23. public $db = 'db';
  24. public $ns = 'app\models';
  25. public $tableName;
  26. public $modelClass;
  27. public $baseClass = 'yii\db\ActiveRecord';
  28. public $generateRelations = true;
  29. public $generateLabelsFromComments = false;
  30. public $useTablePrefix = false;
  31. /**
  32. * @inheritdoc
  33. */
  34. public function getName()
  35. {
  36. return 'Model Generator';
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function getDescription()
  42. {
  43. return 'This generator generates an ActiveRecord class for the specified database table.';
  44. }
  45. /**
  46. * @inheritdoc
  47. */
  48. public function rules()
  49. {
  50. return array_merge(parent::rules(), [
  51. [['db', 'ns', 'tableName', 'modelClass', 'baseClass'], 'filter', 'filter' => 'trim'],
  52. [['ns'], 'filter', 'filter' => function($value) { return trim($value, '\\'); }],
  53. [['db', 'ns', 'tableName', 'baseClass'], 'required'],
  54. [['db', 'modelClass'], 'match', 'pattern' => '/^\w+$/', 'message' => 'Only word characters are allowed.'],
  55. [['ns', 'baseClass'], 'match', 'pattern' => '/^[\w\\\\]+$/', 'message' => 'Only word characters and backslashes are allowed.'],
  56. [['tableName'], 'match', 'pattern' => '/^(\w+\.)?([\w\*]+)$/', 'message' => 'Only word characters, and optionally an asterisk and/or a dot are allowed.'],
  57. [['db'], 'validateDb'],
  58. [['ns'], 'validateNamespace'],
  59. [['tableName'], 'validateTableName'],
  60. [['modelClass'], 'validateModelClass', 'skipOnEmpty' => false],
  61. [['baseClass'], 'validateClass', 'params' => ['extends' => ActiveRecord::className()]],
  62. [['generateRelations', 'generateLabelsFromComments'], 'boolean'],
  63. [['enableI18N'], 'boolean'],
  64. [['useTablePrefix'], 'boolean'],
  65. [['messageCategory'], 'validateMessageCategory', 'skipOnEmpty' => false],
  66. ]);
  67. }
  68. /**
  69. * @inheritdoc
  70. */
  71. public function attributeLabels()
  72. {
  73. return array_merge(parent::attributeLabels(), [
  74. 'ns' => 'Namespace',
  75. 'db' => 'Database Connection ID',
  76. 'tableName' => 'Table Name',
  77. 'modelClass' => 'Model Class',
  78. 'baseClass' => 'Base Class',
  79. 'generateRelations' => 'Generate Relations',
  80. 'generateLabelsFromComments' => 'Generate Labels from DB Comments',
  81. ]);
  82. }
  83. /**
  84. * @inheritdoc
  85. */
  86. public function hints()
  87. {
  88. return array_merge(parent::hints(), [
  89. 'ns' => 'This is the namespace of the ActiveRecord class to be generated, e.g., <code>app\models</code>',
  90. 'db' => 'This is the ID of the DB application component.',
  91. 'tableName' => 'This is the name of the DB table that the new ActiveRecord class is associated with, e.g. <code>post</code>.
  92. The table name may consist of the DB schema part if needed, e.g. <code>public.post</code>.
  93. The table name may end with asterisk to match multiple table names, e.g. <code>tbl_*</code>
  94. will match tables who name starts with <code>tbl_</code>. In this case, multiple ActiveRecord classes
  95. will be generated, one for each matching table name; and the class names will be generated from
  96. the matching characters. For example, table <code>tbl_post</code> will generate <code>Post</code>
  97. class.',
  98. 'modelClass' => 'This is the name of the ActiveRecord class to be generated. The class name should not contain
  99. the namespace part as it is specified in "Namespace". You do not need to specify the class name
  100. if "Table Name" ends with asterisk, in which case multiple ActiveRecord classes will be generated.',
  101. 'baseClass' => 'This is the base class of the new ActiveRecord class. It should be a fully qualified namespaced class name.',
  102. 'generateRelations' => 'This indicates whether the generator should generate relations based on
  103. foreign key constraints it detects in the database. Note that if your database contains too many tables,
  104. you may want to uncheck this option to accelerate the code generation process.',
  105. 'generateLabelsFromComments' => 'This indicates whether the generator should generate attribute labels
  106. by using the comments of the corresponding DB columns.',
  107. 'useTablePrefix' => 'This indicates whether the table name returned by the generated ActiveRecord class
  108. should consider the <code>tablePrefix</code> setting of the DB connection. For example, if the
  109. table name is <code>tbl_post</code> and <code>tablePrefix=tbl_</code>, the ActiveRecord class
  110. will return the table name as <code>{{%post}}</code>.',
  111. ]);
  112. }
  113. /**
  114. * @inheritdoc
  115. */
  116. public function autoCompleteData()
  117. {
  118. $db = $this->getDbConnection();
  119. if ($db !== null) {
  120. return [
  121. 'tableName' => function () use ($db) {
  122. return $db->getSchema()->getTableNames();
  123. },
  124. ];
  125. } else {
  126. return [];
  127. }
  128. }
  129. /**
  130. * @inheritdoc
  131. */
  132. public function requiredTemplates()
  133. {
  134. return ['model.php'];
  135. }
  136. /**
  137. * @inheritdoc
  138. */
  139. public function stickyAttributes()
  140. {
  141. return array_merge(parent::stickyAttributes(), ['ns', 'db', 'baseClass', 'generateRelations', 'generateLabelsFromComments']);
  142. }
  143. /**
  144. * @inheritdoc
  145. */
  146. public function generate()
  147. {
  148. $files = [];
  149. $relations = $this->generateRelations();
  150. $db = $this->getDbConnection();
  151. foreach ($this->getTableNames() as $tableName) {
  152. $className = $this->generateClassName($tableName);
  153. $tableSchema = $db->getTableSchema($tableName);
  154. $params = [
  155. 'tableName' => $tableName,
  156. 'className' => $className,
  157. 'tableSchema' => $tableSchema,
  158. 'labels' => $this->generateLabels($tableSchema),
  159. 'rules' => $this->generateRules($tableSchema),
  160. 'relations' => isset($relations[$className]) ? $relations[$className] : [],
  161. ];
  162. $files[] = new CodeFile(
  163. Yii::getAlias('@' . str_replace('\\', '/', $this->ns)) . '/' . $className . '.php',
  164. $this->render('model.php', $params)
  165. );
  166. }
  167. return $files;
  168. }
  169. /**
  170. * Generates the attribute labels for the specified table.
  171. * @param \yii\db\TableSchema $table the table schema
  172. * @return array the generated attribute labels (name => label)
  173. */
  174. public function generateLabels($table)
  175. {
  176. $labels = [];
  177. foreach ($table->columns as $column) {
  178. if ($this->generateLabelsFromComments && !empty($column->comment)) {
  179. $labels[$column->name] = $column->comment;
  180. } elseif (!strcasecmp($column->name, 'id')) {
  181. $labels[$column->name] = 'ID';
  182. } else {
  183. $label = Inflector::camel2words($column->name);
  184. if (!empty($label) && substr_compare($label, ' id', -3, 3, true) === 0) {
  185. $label = substr($label, 0, -3) . ' ID';
  186. }
  187. $labels[$column->name] = $label;
  188. }
  189. }
  190. return $labels;
  191. }
  192. /**
  193. * Generates validation rules for the specified table.
  194. * @param \yii\db\TableSchema $table the table schema
  195. * @return array the generated validation rules
  196. */
  197. public function generateRules($table)
  198. {
  199. $types = [];
  200. $lengths = [];
  201. foreach ($table->columns as $column) {
  202. if ($column->autoIncrement) {
  203. continue;
  204. }
  205. if (!$column->allowNull && $column->defaultValue === null) {
  206. $types['required'][] = $column->name;
  207. }
  208. switch ($column->type) {
  209. case Schema::TYPE_SMALLINT:
  210. case Schema::TYPE_INTEGER:
  211. case Schema::TYPE_BIGINT:
  212. $types['integer'][] = $column->name;
  213. break;
  214. case Schema::TYPE_BOOLEAN:
  215. $types['boolean'][] = $column->name;
  216. break;
  217. case Schema::TYPE_FLOAT:
  218. case Schema::TYPE_DOUBLE:
  219. case Schema::TYPE_DECIMAL:
  220. case Schema::TYPE_MONEY:
  221. $types['number'][] = $column->name;
  222. break;
  223. case Schema::TYPE_DATE:
  224. case Schema::TYPE_TIME:
  225. case Schema::TYPE_DATETIME:
  226. case Schema::TYPE_TIMESTAMP:
  227. $types['safe'][] = $column->name;
  228. break;
  229. default: // strings
  230. if ($column->size > 0) {
  231. $lengths[$column->size][] = $column->name;
  232. } else {
  233. $types['string'][] = $column->name;
  234. }
  235. }
  236. }
  237. $rules = [];
  238. foreach ($types as $type => $columns) {
  239. $rules[] = "[['" . implode("', '", $columns) . "'], '$type']";
  240. }
  241. foreach ($lengths as $length => $columns) {
  242. $rules[] = "[['" . implode("', '", $columns) . "'], 'string', 'max' => $length]";
  243. }
  244. // Unique indexes rules
  245. try {
  246. $db = $this->getDbConnection();
  247. $uniqueIndexes = $db->getSchema()->findUniqueIndexes($table);
  248. foreach ($uniqueIndexes as $uniqueColumns) {
  249. // Avoid validating auto incremental columns
  250. if (!$this->isColumnAutoIncremental($table, $uniqueColumns)) {
  251. $attributesCount = count($uniqueColumns);
  252. if ($attributesCount == 1) {
  253. $rules[] = "[['" . $uniqueColumns[0] . "'], 'unique']";
  254. } elseif ($attributesCount > 1) {
  255. $labels = array_intersect_key($this->generateLabels($table), array_flip($uniqueColumns));
  256. $lastLabel = array_pop($labels);
  257. $columnsList = implode("', '", $uniqueColumns);
  258. $rules[] = "[['" . $columnsList . "'], 'unique', 'targetAttribute' => ['" . $columnsList . "'], 'message' => 'The combination of " . implode(', ', $labels) . " and " . $lastLabel . " has already been taken.']";
  259. }
  260. }
  261. }
  262. } catch (NotSupportedException $e) {
  263. // doesn't support unique indexes information...do nothing
  264. }
  265. return $rules;
  266. }
  267. /**
  268. * @return array the generated relation declarations
  269. */
  270. protected function generateRelations()
  271. {
  272. if (!$this->generateRelations) {
  273. return [];
  274. }
  275. $db = $this->getDbConnection();
  276. if (($pos = strpos($this->tableName, '.')) !== false) {
  277. $schemaName = substr($this->tableName, 0, $pos);
  278. } else {
  279. $schemaName = '';
  280. }
  281. $relations = [];
  282. foreach ($db->getSchema()->getTableSchemas($schemaName) as $table) {
  283. $tableName = $table->name;
  284. $className = $this->generateClassName($tableName);
  285. foreach ($table->foreignKeys as $refs) {
  286. $refTable = $refs[0];
  287. unset($refs[0]);
  288. $fks = array_keys($refs);
  289. $refClassName = $this->generateClassName($refTable);
  290. // Add relation for this table
  291. $link = $this->generateRelationLink(array_flip($refs));
  292. $relationName = $this->generateRelationName($relations, $className, $table, $fks[0], false);
  293. $relations[$className][$relationName] = [
  294. "return \$this->hasOne($refClassName::className(), $link);",
  295. $refClassName,
  296. false,
  297. ];
  298. // Add relation for the referenced table
  299. $hasMany = false;
  300. if (count($table->primaryKey) > count($fks)) {
  301. $hasMany = true;
  302. } else {
  303. foreach ($fks as $key) {
  304. if (!in_array($key, $table->primaryKey, true)) {
  305. $hasMany = true;
  306. break;
  307. }
  308. }
  309. }
  310. $link = $this->generateRelationLink($refs);
  311. $relationName = $this->generateRelationName($relations, $refClassName, $refTable, $className, $hasMany);
  312. $relations[$refClassName][$relationName] = [
  313. "return \$this->" . ($hasMany ? 'hasMany' : 'hasOne') . "($className::className(), $link);",
  314. $className,
  315. $hasMany,
  316. ];
  317. }
  318. if (($fks = $this->checkPivotTable($table)) === false) {
  319. continue;
  320. }
  321. $table0 = $fks[$table->primaryKey[0]][0];
  322. $table1 = $fks[$table->primaryKey[1]][0];
  323. $className0 = $this->generateClassName($table0);
  324. $className1 = $this->generateClassName($table1);
  325. $link = $this->generateRelationLink([$fks[$table->primaryKey[1]][1] => $table->primaryKey[1]]);
  326. $viaLink = $this->generateRelationLink([$table->primaryKey[0] => $fks[$table->primaryKey[0]][1]]);
  327. $relationName = $this->generateRelationName($relations, $className0, $db->getTableSchema($table0), $table->primaryKey[1], true);
  328. $relations[$className0][$relationName] = [
  329. "return \$this->hasMany($className1::className(), $link)->viaTable('" . $this->generateTableName($table->name) . "', $viaLink);",
  330. $className1,
  331. true,
  332. ];
  333. $link = $this->generateRelationLink([$fks[$table->primaryKey[0]][1] => $table->primaryKey[0]]);
  334. $viaLink = $this->generateRelationLink([$table->primaryKey[1] => $fks[$table->primaryKey[1]][1]]);
  335. $relationName = $this->generateRelationName($relations, $className1, $db->getTableSchema($table1), $table->primaryKey[0], true);
  336. $relations[$className1][$relationName] = [
  337. "return \$this->hasMany($className0::className(), $link)->viaTable('" . $this->generateTableName($table->name) . "', $viaLink);",
  338. $className0,
  339. true,
  340. ];
  341. }
  342. return $relations;
  343. }
  344. /**
  345. * Generates the link parameter to be used in generating the relation declaration.
  346. * @param array $refs reference constraint
  347. * @return string the generated link parameter.
  348. */
  349. protected function generateRelationLink($refs)
  350. {
  351. $pairs = [];
  352. foreach ($refs as $a => $b) {
  353. $pairs[] = "'$a' => '$b'";
  354. }
  355. return '[' . implode(', ', $pairs) . ']';
  356. }
  357. /**
  358. * Checks if the given table is a junction table.
  359. * For simplicity, this method only deals with the case where the pivot contains two PK columns,
  360. * each referencing a column in a different table.
  361. * @param \yii\db\TableSchema the table being checked
  362. * @return array|boolean the relevant foreign key constraint information if the table is a junction table,
  363. * or false if the table is not a junction table.
  364. */
  365. protected function checkPivotTable($table)
  366. {
  367. $pk = $table->primaryKey;
  368. if (count($pk) !== 2) {
  369. return false;
  370. }
  371. $fks = [];
  372. foreach ($table->foreignKeys as $refs) {
  373. if (count($refs) === 2) {
  374. if (isset($refs[$pk[0]])) {
  375. $fks[$pk[0]] = [$refs[0], $refs[$pk[0]]];
  376. } elseif (isset($refs[$pk[1]])) {
  377. $fks[$pk[1]] = [$refs[0], $refs[$pk[1]]];
  378. }
  379. }
  380. }
  381. if (count($fks) === 2 && $fks[$pk[0]][0] !== $fks[$pk[1]][0]) {
  382. return $fks;
  383. } else {
  384. return false;
  385. }
  386. }
  387. /**
  388. * Generate a relation name for the specified table and a base name.
  389. * @param array $relations the relations being generated currently.
  390. * @param string $className the class name that will contain the relation declarations
  391. * @param \yii\db\TableSchema $table the table schema
  392. * @param string $key a base name that the relation name may be generated from
  393. * @param boolean $multiple whether this is a has-many relation
  394. * @return string the relation name
  395. */
  396. protected function generateRelationName($relations, $className, $table, $key, $multiple)
  397. {
  398. if (!empty($key) && substr_compare($key, 'id', -2, 2, true) === 0 && strcasecmp($key, 'id')) {
  399. $key = rtrim(substr($key, 0, -2), '_');
  400. }
  401. if ($multiple) {
  402. $key = Inflector::pluralize($key);
  403. }
  404. $name = $rawName = Inflector::id2camel($key, '_');
  405. $i = 0;
  406. while (isset($table->columns[lcfirst($name)])) {
  407. $name = $rawName . ($i++);
  408. }
  409. while (isset($relations[$className][lcfirst($name)])) {
  410. $name = $rawName . ($i++);
  411. }
  412. return $name;
  413. }
  414. /**
  415. * Validates the [[db]] attribute.
  416. */
  417. public function validateDb()
  418. {
  419. if (!Yii::$app->has($this->db)) {
  420. $this->addError('db', 'There is no application component named "db".');
  421. } elseif (!Yii::$app->get($this->db) instanceof Connection) {
  422. $this->addError('db', 'The "db" application component must be a DB connection instance.');
  423. }
  424. }
  425. /**
  426. * Validates the [[ns]] attribute.
  427. */
  428. public function validateNamespace()
  429. {
  430. $this->ns = ltrim($this->ns, '\\');
  431. $path = Yii::getAlias('@' . str_replace('\\', '/', $this->ns), false);
  432. if ($path === false) {
  433. $this->addError('ns', 'Namespace must be associated with an existing directory.');
  434. }
  435. }
  436. /**
  437. * Validates the [[modelClass]] attribute.
  438. */
  439. public function validateModelClass()
  440. {
  441. if ($this->isReservedKeyword($this->modelClass)) {
  442. $this->addError('modelClass', 'Class name cannot be a reserved PHP keyword.');
  443. }
  444. if ((empty($this->tableName) || substr_compare($this->tableName, '*', -1, 1)) && $this->modelClass == '') {
  445. $this->addError('modelClass', 'Model Class cannot be blank if table name does not end with asterisk.');
  446. }
  447. }
  448. /**
  449. * Validates the [[tableName]] attribute.
  450. */
  451. public function validateTableName()
  452. {
  453. if (strpos($this->tableName, '*') !== false && substr_compare($this->tableName, '*', -1, 1)) {
  454. $this->addError('tableName', 'Asterisk is only allowed as the last character.');
  455. return;
  456. }
  457. $tables = $this->getTableNames();
  458. if (empty($tables)) {
  459. $this->addError('tableName', "Table '{$this->tableName}' does not exist.");
  460. } else {
  461. foreach ($tables as $table) {
  462. $class = $this->generateClassName($table);
  463. if ($this->isReservedKeyword($class)) {
  464. $this->addError('tableName', "Table '$table' will generate a class which is a reserved PHP keyword.");
  465. break;
  466. }
  467. }
  468. }
  469. }
  470. protected $tableNames;
  471. protected $classNames;
  472. /**
  473. * @return array the table names that match the pattern specified by [[tableName]].
  474. */
  475. protected function getTableNames()
  476. {
  477. if ($this->tableNames !== null) {
  478. return $this->tableNames;
  479. }
  480. $db = $this->getDbConnection();
  481. if ($db === null) {
  482. return [];
  483. }
  484. $tableNames = [];
  485. if (strpos($this->tableName, '*') !== false) {
  486. if (($pos = strrpos($this->tableName, '.')) !== false) {
  487. $schema = substr($this->tableName, 0, $pos);
  488. $pattern = '/^' . str_replace('*', '\w+', substr($this->tableName, $pos + 1)) . '$/';
  489. } else {
  490. $schema = '';
  491. $pattern = '/^' . str_replace('*', '\w+', $this->tableName) . '$/';
  492. }
  493. foreach ($db->schema->getTableNames($schema) as $table) {
  494. if (preg_match($pattern, $table)) {
  495. $tableNames[] = $schema === '' ? $table : ($schema . '.' . $table);
  496. }
  497. }
  498. } elseif (($table = $db->getTableSchema($this->tableName, true)) !== null) {
  499. $tableNames[] = $this->tableName;
  500. $this->classNames[$this->tableName] = $this->modelClass;
  501. }
  502. return $this->tableNames = $tableNames;
  503. }
  504. /**
  505. * Generates the table name by considering table prefix.
  506. * If [[useTablePrefix]] is false, the table name will be returned without change.
  507. * @param string $tableName the table name (which may contain schema prefix)
  508. * @return string the generated table name
  509. */
  510. public function generateTableName($tableName)
  511. {
  512. if (!$this->useTablePrefix) {
  513. return $tableName;
  514. }
  515. $db = $this->getDbConnection();
  516. if (preg_match("/^{$db->tablePrefix}(.*?)$/", $tableName, $matches)) {
  517. $tableName = '{{%' . $matches[1] . '}}';
  518. } elseif (preg_match("/^(.*?){$db->tablePrefix}$/", $tableName, $matches)) {
  519. $tableName = '{{' . $matches[1] . '%}}';
  520. }
  521. return $tableName;
  522. }
  523. /**
  524. * Generates a class name from the specified table name.
  525. * @param string $tableName the table name (which may contain schema prefix)
  526. * @return string the generated class name
  527. */
  528. protected function generateClassName($tableName)
  529. {
  530. if (isset($this->classNames[$tableName])) {
  531. return $this->classNames[$tableName];
  532. }
  533. if (($pos = strrpos($tableName, '.')) !== false) {
  534. $tableName = substr($tableName, $pos + 1);
  535. }
  536. $db = $this->getDbConnection();
  537. $patterns = [];
  538. $patterns[] = "/^{$db->tablePrefix}(.*?)$/";
  539. $patterns[] = "/^(.*?){$db->tablePrefix}$/";
  540. if (strpos($this->tableName, '*') !== false) {
  541. $pattern = $this->tableName;
  542. if (($pos = strrpos($pattern, '.')) !== false) {
  543. $pattern = substr($pattern, $pos + 1);
  544. }
  545. $patterns[] = '/^' . str_replace('*', '(\w+)', $pattern) . '$/';
  546. }
  547. $className = $tableName;
  548. foreach ($patterns as $pattern) {
  549. if (preg_match($pattern, $tableName, $matches)) {
  550. $className = $matches[1];
  551. break;
  552. }
  553. }
  554. return $this->classNames[$tableName] = Inflector::id2camel($className, '_');
  555. }
  556. /**
  557. * @return Connection the DB connection as specified by [[db]].
  558. */
  559. protected function getDbConnection()
  560. {
  561. return Yii::$app->get($this->db, false);
  562. }
  563. /**
  564. * Checks if any of the specified columns is auto incremental.
  565. * @param \yii\db\TableSchema $table the table schema
  566. * @param array $columns columns to check for autoIncrement property
  567. * @return boolean whether any of the specified columns is auto incremental.
  568. */
  569. protected function isColumnAutoIncremental($table, $columns)
  570. {
  571. foreach ($columns as $column) {
  572. if (isset($table->columns[$column]) && $table->columns[$column]->autoIncrement) {
  573. return true;
  574. }
  575. }
  576. return false;
  577. }
  578. }