| 1 | <?php |
||
| 9 | abstract class SqliteModel extends Model |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * $tableCreated. |
||
| 13 | * |
||
| 14 | * @var array |
||
| 15 | */ |
||
| 16 | protected static $tableCreated = []; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Get the table associated with the model. |
||
| 20 | * |
||
| 21 | * @return string |
||
| 22 | */ |
||
| 23 | 31 | public function getTable() |
|
| 24 | { |
||
| 25 | 31 | return $this->createTable(parent::getTable()); |
|
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Resolve a connection instance. |
||
| 30 | * |
||
| 31 | * @param string|null $connection |
||
| 32 | * @return \Illuminate\Database\Connection |
||
| 33 | */ |
||
| 34 | 31 | public static function resolveConnection($connection = null) |
|
| 35 | { |
||
| 36 | 31 | return ConnectionResolver::getInstance()->connection($connection); |
|
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | * |
||
| 42 | * @param string $table |
||
| 43 | * @return string |
||
| 44 | */ |
||
| 45 | 31 | protected function createTable($table) |
|
| 46 | { |
||
| 47 | 31 | if (isset(static::$tableCreated[$table]) === true) { |
|
| 48 | 31 | return $table; |
|
| 49 | } |
||
| 50 | |||
| 51 | 2 | $schema = $this->getConnection()->getSchemaBuilder(); |
|
| 52 | 2 | if ($schema->hasTable($table) === false) { |
|
| 53 | 2 | $schema->create($table, function (Blueprint $table) { |
|
| 54 | 2 | $this->createSchema($table); |
|
| 55 | 2 | }); |
|
| 56 | 2 | static::$tableCreated[$table] = true; |
|
| 57 | 2 | if ($this instanceof FileModel === true) { |
|
| 58 | 1 | $this->initializeTable($table); |
|
|
|
|||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | 2 | return $table; |
|
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * createSchema. |
||
| 67 | * |
||
| 68 | * @param Blueprint $table |
||
| 69 | * @return void |
||
| 70 | */ |
||
| 71 | abstract protected function createSchema(Blueprint $table); |
||
| 72 | } |
||
| 73 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: