TableSuffix.php 650 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. namespace App\model;
  3. trait TableSuffix
  4. {
  5. private static $suffix;
  6. public static function suffix($suffix)
  7. {
  8. static::$suffix = $suffix;
  9. }
  10. public function __construct(array $attributes = [])
  11. {
  12. $this->table .= static::$suffix;
  13. parent::__construct($attributes);
  14. }
  15. public static function model(int $month = 0)
  16. {
  17. $suffix = '';
  18. if ($month == 0) {
  19. $suffix = date('Ym');
  20. } else {
  21. $suffix = date('Ym', strtotime("last day of {$month} month"));
  22. }
  23. self::suffix($suffix);
  24. $model = new self;
  25. return $model;
  26. }
  27. }