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.

model.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * This is the template for generating the model class of a specified table.
  4. */
  5. /* @var $this yii\web\View */
  6. /* @var $generator yii\gii\generators\model\Generator */
  7. /* @var $tableName string full table name */
  8. /* @var $className string class name */
  9. /* @var $queryClassName string query class name */
  10. /* @var $tableSchema yii\db\TableSchema */
  11. /* @var $labels string[] list of attribute labels (name => label) */
  12. /* @var $rules string[] list of validation rules */
  13. /* @var $relations array list of relations (name => relation declaration) */
  14. echo "<?php\n";
  15. ?>
  16. namespace <?= $generator->ns ?>;
  17. use Yii;
  18. /**
  19. * This is the model class for table "<?= $generator->generateTableName($tableName) ?>".
  20. *
  21. <?php foreach ($tableSchema->columns as $column): ?>
  22. * @property <?= "{$column->phpType} \${$column->name}\n" ?>
  23. <?php endforeach; ?>
  24. <?php if (!empty($relations)): ?>
  25. *
  26. <?php foreach ($relations as $name => $relation): ?>
  27. * @property <?= $relation[1] . ($relation[2] ? '[]' : '') . ' $' . lcfirst($name) . "\n" ?>
  28. <?php endforeach; ?>
  29. <?php endif; ?>
  30. */
  31. class <?= $className ?> extends <?= '\\' . ltrim($generator->baseClass, '\\') . "\n" ?>
  32. {
  33. /**
  34. * @inheritdoc
  35. */
  36. public static function tableName()
  37. {
  38. return '<?= $generator->generateTableName($tableName) ?>';
  39. }
  40. <?php if ($generator->db !== 'db'): ?>
  41. /**
  42. * @return \yii\db\Connection the database connection used by this AR class.
  43. */
  44. public static function getDb()
  45. {
  46. return Yii::$app->get('<?= $generator->db ?>');
  47. }
  48. <?php endif; ?>
  49. /**
  50. * @inheritdoc
  51. */
  52. public function rules()
  53. {
  54. return [<?= "\n " . implode(",\n ", $rules) . ",\n " ?>];
  55. }
  56. /**
  57. * @inheritdoc
  58. */
  59. public function attributeLabels()
  60. {
  61. return [
  62. <?php foreach ($labels as $name => $label): ?>
  63. <?= "'$name' => " . $generator->generateString($label) . ",\n" ?>
  64. <?php endforeach; ?>
  65. ];
  66. }
  67. <?php foreach ($relations as $name => $relation): ?>
  68. /**
  69. * @return \yii\db\ActiveQuery
  70. */
  71. public function get<?= $name ?>()
  72. {
  73. <?= $relation[0] . "\n" ?>
  74. }
  75. <?php endforeach; ?>
  76. <?php if ($queryClassName): ?>
  77. <?php
  78. $queryClassFullName = ($generator->ns === $generator->queryNs) ? $queryClassName : '\\' . $generator->queryNs . '\\' . $queryClassName;
  79. echo "\n";
  80. ?>
  81. /**
  82. * @inheritdoc
  83. * @return <?= $queryClassFullName ?> the active query used by this AR class.
  84. */
  85. public static function find()
  86. {
  87. return new <?= $queryClassFullName ?>(get_called_class());
  88. }
  89. <?php endif; ?>
  90. }