model.php 2.9 KB

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