|
| 1 | +# Laravel MySQL System Variable Manager<br>[](https://travis-ci.org/mpyw/laravel-mysql-system-variable-manager) [](https://scrutinizer-ci.com/g/mpyw/laravel-mysql-system-variable-manager/?branch=master) [](https://scrutinizer-ci.com/g/mpyw/laravel-mysql-system-variable-manager/?branch=master) |
| 2 | + |
| 3 | +A tiny extension of `MySqlConnection` that manages **session** system variables |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +- PHP: ^7.1 |
| 8 | +- Laravel: ^5.6 || ^6.0 |
| 9 | + |
| 10 | +## Installing |
| 11 | + |
| 12 | +```bash |
| 13 | +composer require mpyw/laravel-mysql-system-variable-manager |
| 14 | +``` |
| 15 | + |
| 16 | +The default implementation is provided by the automatically-discovered service provider. |
| 17 | + |
| 18 | +## Basic Usage |
| 19 | + |
| 20 | +```php |
| 21 | +<?php |
| 22 | + |
| 23 | +use Illuminate\Support\Facades\DB; |
| 24 | + |
| 25 | +// Assign an auto-recoverable system variable |
| 26 | +// The variable is reassigned on accidental disconnections |
| 27 | +DB::setSystemVariable('long_query_time', 10.0); |
| 28 | + |
| 29 | +// Assign a system variable without auto-recovery |
| 30 | +DB::setSystemVariable('long_query_time', 10.0, false); |
| 31 | + |
| 32 | +// Assign multiple variables |
| 33 | +DB::setSystemVariables(['long_query_time' => 10.0, 'tx_isolation' => 'read-committed']); |
| 34 | + |
| 35 | +// Assign a variable on a different connection |
| 36 | +DB::connection('other_mysql_connection')->setSystemVariable('long_query_time', 10.0); |
| 37 | +``` |
| 38 | + |
| 39 | +**WARNING:** |
| 40 | +Don't use `DB::disconnect()` directly or auto-recovery won't be fired. |
| 41 | +Use **`DB::connection()->disconnect()`** instead. |
| 42 | + |
| 43 | +## Advanced Usage |
| 44 | + |
| 45 | +You can manually customize extended `MySqlConnection` using `ManagesSystemVariables` trait. |
| 46 | + |
| 47 | +```php |
| 48 | +<?php |
| 49 | + |
| 50 | +namespace App\Providers; |
| 51 | + |
| 52 | +use App\Database\MySqlConnection; |
| 53 | +use Illuminate\Database\Connection; |
| 54 | +use Illuminate\Support\ServiceProvider; |
| 55 | + |
| 56 | +class DatabaseServiceProvider extends ServiceProvider |
| 57 | +{ |
| 58 | + public function boot(): void |
| 59 | + { |
| 60 | + Connection::resolverFor('mysql', function (...$parameters) { |
| 61 | + return new MySqlConnection(...$parameters); |
| 62 | + }); |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +```php |
| 68 | +<?php |
| 69 | + |
| 70 | +namespace App\Database; |
| 71 | + |
| 72 | +use Illuminate\Database\Connection as BaseMySqlConnection; |
| 73 | +use Mpyw\LaravelMysqlSystemVariableManager\ManagesSystemVariables; |
| 74 | + |
| 75 | +class MySqlConnection extends BaseMySqlConnection |
| 76 | +{ |
| 77 | + use ManagesSystemVariables; |
| 78 | + |
| 79 | + public function withoutForeignKeyChecks(callable $callback, ...$args) |
| 80 | + { |
| 81 | + $this->setSystemVariable('foreign_key_checks', false); |
| 82 | + try { |
| 83 | + return $callback(...$args); |
| 84 | + } finally { |
| 85 | + $this->setSystemVariable('foreign_key_checks', true); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +```php |
| 92 | +<?php |
| 93 | + |
| 94 | +use App\Post; |
| 95 | +use Illuminate\Support\Facades\Auth; |
| 96 | +use Illuminate\Support\Facades\DB; |
| 97 | + |
| 98 | +$post = new Post(); |
| 99 | +$post->user()->associate(Auth::user()); |
| 100 | +$post->save(); |
| 101 | + |
| 102 | +DB::withoutForeignKeyChecks(function () use ($post) { |
| 103 | + $post->user()->associate(null); |
| 104 | + $post->save(); |
| 105 | +}); |
| 106 | +``` |
0 commit comments