DeviceData.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. class DeviceData extends Model
  7. {
  8. protected $table = "device_data"; //指定表
  9. const CREATED_AT = 'crt_time';
  10. const UPDATED_AT = 'upd_time';
  11. protected $dateFormat = 'U';
  12. protected $guarded = [];
  13. public function __construct(array $attributes = [])
  14. {
  15. parent::__construct($attributes);
  16. if (isset($attributes['channel']) && ! empty($attributes['channel'])) {
  17. $channel = $attributes['channel'];
  18. $year_month = substr($channel,0,7);
  19. $year_month = str_replace('-','_',$year_month);
  20. $this->setTableById($year_month);
  21. }
  22. }
  23. public function setTableById($channel)
  24. {
  25. if (! empty($channel)) {
  26. $tb = $this->table.'_' . (string)$channel;
  27. $this->createTable($tb);
  28. $this->setTable($tb);
  29. }
  30. }
  31. //创建表
  32. private function createTable($table){
  33. if(! empty($table) && ! Schema::hasTable($table)){
  34. //执行建表语句
  35. DB::statement('create table '. $table .' like device_data_for_create');
  36. }
  37. }
  38. public function is_table_isset(){
  39. if(Schema::hasTable($this->table)) return true;
  40. return false;
  41. }
  42. }