Model.php 744 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model as BaseModel;
  4. /**
  5. * Class Model
  6. *
  7. * @package App\Models
  8. * @method increment($column, $amount = 1, array $extra = [])
  9. */
  10. class Model extends BaseModel
  11. {
  12. /** @var int 分表的粒度 */
  13. public const SLICE = 10000;
  14. /** @var int 用来分表的key */
  15. protected $sliceKey = 0;
  16. /**
  17. * 设置用来分表的key
  18. * @param int $sliceKey 用来分表的key(目前仅支持int型分表)
  19. */
  20. public function setSliceKey(int $sliceKey)
  21. {
  22. $this->sliceKey = $sliceKey;
  23. if ($this->sliceKey) {
  24. $index = intdiv($this->sliceKey, self::SLICE);
  25. $this->table = "{$this->table}_{$index}";
  26. }
  27. }
  28. }