OrdersProductBom.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Model;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Schema;
  6. /**
  7. *
  8. * Class Unit
  9. * @package App\Models
  10. */
  11. class OrdersProductBom extends Model
  12. {
  13. protected $table = "orders_product_bom";
  14. const CREATED_AT = 'crt_time';
  15. const UPDATED_AT = 'upd_time';
  16. protected $dateFormat = 'U';
  17. //$fillable属性里面的字段是可以赋值的,其他的所有属性不能被赋值
  18. //$guarded属性里面的字段是不可以赋值,其他的所有属性都能被赋值
  19. protected $guarded = [];
  20. protected $month = [
  21. 1 => '01_03',
  22. 2 => '01_03',
  23. 3 => '01_03',
  24. 4 => '04_06',
  25. 5 => '04_06',
  26. 6 => '04_06',
  27. 7 => '07_09',
  28. 8 => '07_09',
  29. 9 => '07_09',
  30. 10 => '10_12',
  31. 11 => '10_12',
  32. 12 => '10_12',
  33. ];
  34. public function __construct(array $attributes = [])
  35. {
  36. parent::__construct($attributes);
  37. if (isset($attributes['channel']) && $attributes['channel'] > 0) {
  38. $channel = $attributes['channel'];
  39. $month = (int)substr($channel,4,2);
  40. $channel = substr($channel,0,4).$this->month[$month];
  41. $this->setTableById($channel);
  42. }
  43. }
  44. public function setTableById($channel)
  45. {
  46. if ($channel > 0) {
  47. $tb = $this->table.'_' . (string)$channel;
  48. $this->createTable($tb);
  49. $this->setTable($tb);
  50. }
  51. }
  52. //创建表
  53. private function createTable($table){
  54. if(! empty($table) && ! Schema::hasTable($table)){
  55. //执行建表语句
  56. DB::statement('create table '. $table .' like orders_product_bom');
  57. }
  58. }
  59. }