model.stub 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace {{namespace_start}}model{{namespace_end}};
  3. {% if generate_model == 1 %}
  4. use plugin\saiadmin\basic\BaseModel;
  5. /**
  6. * {{menu_name}}模型
  7. */
  8. class {{class_name}} extends BaseModel
  9. {% else %}
  10. use plugin\saiadmin\basic\BaseNormalModel;
  11. /**
  12. * {{menu_name}}模型
  13. */
  14. class {{class_name}} extends BaseNormalModel
  15. {% endif %}
  16. {
  17. /**
  18. * 数据表主键
  19. * @var string
  20. */
  21. protected $pk = '{{pk}}';
  22. /**
  23. * 数据库表名称
  24. * @var string
  25. */
  26. protected $table = '{{table_name}}';
  27. {% if source != db_source and source != '' %}
  28. /**
  29. * 数据库连接
  30. * @var string
  31. */
  32. protected $connection = '{{source}}';
  33. {% endif %}
  34. {% for column in columns %}
  35. {% if column.view_type == 'inputTag' or column.view_type == 'checkbox' or column.view_type == 'cityLinkage' or column.view_type == 'userSelect' %}
  36. /**
  37. * {{column.column_comment}} 保存数组转换
  38. */
  39. public function set{{column.column_name | camel}}Attr($value)
  40. {
  41. return json_encode($value, JSON_UNESCAPED_UNICODE);
  42. }
  43. /**
  44. * {{column.column_comment}} 读取数组转换
  45. */
  46. public function get{{column.column_name | camel}}Attr($value)
  47. {
  48. return json_decode($value ?? '', true);
  49. }
  50. {% endif %}
  51. {% if column.view_type == 'uploadImage' and column.options.multiple == true %}
  52. /**
  53. * {{column.column_comment}} 保存数组转换
  54. */
  55. public function set{{column.column_name | camel}}Attr($value)
  56. {
  57. return json_encode($value, JSON_UNESCAPED_UNICODE);
  58. }
  59. /**
  60. * {{column.column_comment}} 读取数组转换
  61. */
  62. public function get{{column.column_name | camel}}Attr($value)
  63. {
  64. return json_decode($value ?? '', true);
  65. }
  66. {% endif %}
  67. {% if column.view_type == 'uploadFile' and column.options.multiple == true %}
  68. /**
  69. * {{column.column_comment}} 保存数组转换
  70. */
  71. public function set{{column.column_name | camel}}Attr($value)
  72. {
  73. return json_encode($value, JSON_UNESCAPED_UNICODE);
  74. }
  75. /**
  76. * {{column.column_comment}} 读取数组转换
  77. */
  78. public function get{{column.column_name | camel}}Attr($value)
  79. {
  80. return json_decode($value ?? '', true);
  81. }
  82. {% endif %}
  83. {% endfor %}
  84. {% for column in columns %}
  85. {% if column.is_query == 2 and column.query_type == 'neq' %}
  86. /**
  87. * {{column.column_comment}} 搜索
  88. */
  89. public function search{{column.column_name | camel}}Attr($query, $value)
  90. {
  91. $query->where('{{column.column_name}}', '<>', $value);
  92. }
  93. {% endif %}
  94. {% if column.is_query == 2 and column.query_type == 'gt' %}
  95. /**
  96. * {{column.column_comment}} 搜索
  97. */
  98. public function search{{column.column_name | camel}}Attr($query, $value)
  99. {
  100. $query->where('{{column.column_name}}', '>', $value);
  101. }
  102. {% endif %}
  103. {% if column.is_query == 2 and column.query_type == 'gte' %}
  104. /**
  105. * {{column.column_comment}} 搜索
  106. */
  107. public function search{{column.column_name | camel}}Attr($query, $value)
  108. {
  109. $query->where('{{column.column_name}}', '>=', $value);
  110. }
  111. {% endif %}
  112. {% if column.is_query == 2 and column.query_type == 'lt' %}
  113. /**
  114. * {{column.column_comment}} 搜索
  115. */
  116. public function search{{column.column_name | camel}}Attr($query, $value)
  117. {
  118. $query->where('{{column.column_name}}', '<', $value);
  119. }
  120. {% endif %}
  121. {% if column.is_query == 2 and column.query_type == 'lte' %}
  122. /**
  123. * {{column.column_comment}} 搜索
  124. */
  125. public function search{{column.column_name | camel}}Attr($query, $value)
  126. {
  127. $query->where('{{column.column_name}}', '<=', $value);
  128. }
  129. {% endif %}
  130. {% if column.is_query == 2 and column.query_type == 'like' %}
  131. /**
  132. * {{column.column_comment}} 搜索
  133. */
  134. public function search{{column.column_name | camel}}Attr($query, $value)
  135. {
  136. $query->where('{{column.column_name}}', 'like', '%'.$value.'%');
  137. }
  138. {% endif %}
  139. {% if column.is_query == 2 and column.query_type == 'in' %}
  140. /**
  141. * {{column.column_comment}} 搜索
  142. */
  143. public function search{{column.column_name | camel}}Attr($query, $value)
  144. {
  145. $query->where('{{column.column_name}}', 'in', $value);
  146. }
  147. {% endif %}
  148. {% if column.is_query == 2 and column.query_type == 'notin' %}
  149. /**
  150. * {{column.column_comment}} 搜索
  151. */
  152. public function search{{column.column_name | camel}}Attr($query, $value)
  153. {
  154. $query->where('{{column.column_name}}', 'not in', $value);
  155. }
  156. {% endif %}
  157. {% if column.is_query == 2 and column.query_type == 'between' %}
  158. /**
  159. * {{column.column_comment}} 搜索
  160. */
  161. public function search{{column.column_name | camel}}Attr($query, $value)
  162. {
  163. $query->whereTime('{{column.column_name}}', 'between', $value);
  164. }
  165. {% endif %}
  166. {% endfor %}
  167. {% for item in options.relations %}
  168. {% if item.type == 'belongsTo' %}
  169. /**
  170. * 关联模型 {{item.name}}
  171. */
  172. public function {{item.name}}()
  173. {
  174. return $this->{{item.type}}({{item.model}}::class, '{{item.localKey}}', '{{item.foreignKey}}');
  175. }
  176. {% endif %}
  177. {% if item.type == 'hasOne' or item.type == 'hasMany' %}
  178. /**
  179. * 关联模型 {{item.name}}
  180. */
  181. public function {{item.name}}()
  182. {
  183. return $this->{{item.type}}({{item.model}}::class, '{{item.localKey}}', '{{item.foreignKey}}');
  184. }
  185. {% endif %}
  186. {% if item.type == 'belongsToMany' %}
  187. /**
  188. * 关联模型 {{item.name}}
  189. */
  190. public function {{item.name}}()
  191. {
  192. return $this->{{item.type}}({{item.model}}::class, {{item.table}}::class, '{{item.localKey}}', '{{item.foreignKey}}');
  193. }
  194. {% endif %}
  195. {% endfor %}
  196. }