model.php 2.9 KB

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