php - Laravel 5.2 - Make different Read/Write Connections -
in laravel 5.2, wanted have different connections read/write, followed advice provided in laravel documents. but, default, creating default mysql named connection , not 2 different read/write connections, therefore picking read connection operations insert/update.
after debugging, found in databasemanager.php file connection named passed argument makeconnection()
mysql , not mysql::read or mysql::write.
before
config/database.php
'mysql' => [ //we need have nested options both read/write 'read' => [ 'host' => env('db_read_host'), ], 'write' => [ 'host' => env('db_write_host'), ], 'host' => env('db_read_host'), 'username' => env('db_username'), 'password' => env('db_password'), 'driver' => 'mysql', 'database' => env('db_database'), 'collation' => 'utf8_unicode_ci', 'port' => env('db_port', '3306'), 'charset' => 'utf8', 'prefix' => '', 'strict' => false, ],
file - vendor/laravel/framework/src/illuminate/database/databasemanager.php
public function connection($name = null) { list($name, $type) = $this->parseconnectionname($name); // if haven't created connection, we'll create based on config // provided in application. once we've created connections // set "fetch mode" pdo determines query return types. if (! isset($this->connections[$name])) { $connection = $this->makeconnection($name); $this->setpdofortype($connection, $type); $this->connections[$name] = $this->prepare($connection); } return $this->connections[$name]; }
so, have added small change in file, after adding this, it's creating 2 different connections mysql.read/mysql.write , switching them appropriately according given sql operations select,insert,update
needed feedback if viable solution ?
after changing file
config/database.php
'mysql' => [ //we need have nested options both read/write 'read' => [ 'host' => env('db_read_host'), 'username' => env('db_read_username'), 'password' => env('db_read_password'), 'driver' => 'mysql', 'database' => env('db_database'), 'collation' => 'utf8_unicode_ci', 'port' => env('db_port', '3306'), 'charset' => 'utf8', 'prefix' => '', 'strict' => false, ], 'write' => [ 'host' => env('db_write_host'), 'username' => env('db_write_username'), 'password' => env('db_write_password'), 'driver' => 'mysql', 'database' => env('db_database'), 'collation' => 'utf8_unicode_ci', 'port' => env('db_port', '3306'), 'charset' => 'utf8', 'prefix' => '', 'strict' => false, ], 'host' => env('db_read_host'), 'username' => env('db_read_username'), 'password' => env('db_read_password'), 'driver' => 'mysql', 'database' => env('db_database'), 'collation' => 'utf8_unicode_ci', 'port' => env('db_port', '3306'), 'charset' => 'utf8', 'prefix' => '', 'strict' => false, ],
file - vendor/laravel/framework/src/illuminate/database/databasemanager.php
public function connection($name = null) { list($name, $type) = $this->parseconnectionname($name); // check if $type read/write , store appropriate connections if( $type != null ) { $name .= '.' . $type; } //end // if haven't created connection, we'll create based on config // provided in application. once we've created connections // set "fetch mode" pdo determines query return types. if (! isset($this->connections[$name])) { $connection = $this->makeconnection($name); $this->setpdofortype($connection, $type); $this->connections[$name] = $this->prepare($connection); } return $this->connections[$name]; }
wiki
Comments
Post a Comment