model.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * This is the template for generating the model class of a specified table.
  4. *
  5. * @var yii\web\View $this
  6. * @var yii\gii\generators\model\Generator $generator
  7. * @var string $tableName full table name
  8. * @var string $className class name
  9. * @var yii\db\TableSchema $tableSchema
  10. * @var string[] $labels list of attribute labels (name => label)
  11. * @var string[] $rules list of validation rules
  12. * @var array $relations list of relations (name => relation declaration)
  13. */
  14. echo "<?php\n";
  15. ?>
  16. namespace <?= $generator->ns ?>;
  17. use Yii;
  18. /**
  19. * This is the model class for table "<?= $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 '<?= $tableName ?>';
  39. }
  40. /**
  41. * @inheritdoc
  42. */
  43. public function rules()
  44. {
  45. return [<?= "\n " . implode(",\n ", $rules) . "\n " ?>];
  46. }
  47. /**
  48. * @inheritdoc
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. <?php foreach ($labels as $name => $label): ?>
  54. <?= "'$name' => " . $generator->generateString($label) . ",\n" ?>
  55. <?php endforeach; ?>
  56. ];
  57. }
  58. <?php foreach ($relations as $name => $relation): ?>
  59. /**
  60. * @return \yii\db\ActiveQuery
  61. */
  62. public function get<?= $name ?>()
  63. {
  64. <?= $relation[0] . "\n" ?>
  65. }
  66. <?php endforeach; ?>
  67. }