mpyw / laravel-pdo-emulation-control
Temporarily enable/disable PDO prepared statement emulation
Installs: 75 344
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- ext-pdo: *
- illuminate/database: ^9.0 || ^10.0 || ^11.0
- illuminate/support: ^9.0 || ^10.0 || ^11.0
- mpyw/unclosure: ^3.0
Requires (Dev)
- mockery/mockery: ^1.3.3 || ^1.4.2
- nunomaduro/larastan: >=1.0
- orchestra/testbench: *
- orchestra/testbench-core: >=7.0
- phpstan/extension-installer: >=1.1
- phpstan/phpstan: >=1.1
- phpunit/phpunit: >=9.5
README
Temporarily enable/disable PDO prepared statement emulation.
Requirements
Installing
composer require mpyw/laravel-pdo-emulation-control
Basic Usage
Important
The default implementation is provided by ConnectionServiceProvider
, however, package discovery is not available.
Be careful that you MUST register it in config/app.php
by yourself.
<?php return [ /* ... */ 'providers' => [ /* ... */ Mpyw\LaravelPdoEmulationControl\ConnectionServiceProvider::class, /* ... */ ], ];
<?php use Illuminate\Support\Facades\DB; // Temporarily enable PDO prepared statement emulation. DB::emulated(function () { // Your code goes here }); // Temporarily disable PDO prepared statement emulation. // (Only if you've already configured your connection by options [PDO::ATTR_EMULATE_PREPARES => true]) DB::native(function () { // Your code goes here });
Important
Note that DB::getPdo()
DB::getReadPdo()
are not always called even though these methods directly touch the PDO
instances.
Connections are lazily resolved as possible as they can.
PDO::setAttribute()
is called only after the PDO
instance has been created and the socket connection to the database has been really established.
Advanced Usage
Tip
You can extend Connection classes with ControlsEmulation
trait by yourself.
<?php namespace App\Providers; use App\Database\MySqlConnection; use Illuminate\Database\Connection; use Illuminate\Support\ServiceProvider; class DatabaseServiceProvider extends ServiceProvider { public function register(): void { Connection::resolverFor('mysql', function (...$parameters) { return new MySqlConnection(...$parameters); }); } }
<?php namespace App\Database; use Illuminate\Database\Connection as BaseMySqlConnection; use Mpyw\LaravelPdoEmulationControl\ControlsEmulation; class MySqlConnection extends BaseMySqlConnection { use ControlsEmulation; }