corcel / corcel

Use WordPress backend with Laravel or any PHP application

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use dynamic database connection instead of the one definded in config?

refocusTheory opened this issue · comments

  • Corcel Version: 5
  • Framework Name & Version: Laravel 8
  • PHP Version: 8
  • Database Driver & Version: MySql

Description:

Is there a way to send the DB connection params like:

$params = [
    'database'  => 'database_name',
    'username'  => 'username',
    'password'  => 'pa$$word',
    'prefix'    => 'wp_' // default prefix is 'wp_', you can change to your own prefix
];
Corcel\Database::connect($params);

in Laravel?....

If I connect this way, I cant seem to get POST or any other model to connect. It always looks for the Laravel Config specified connection. It connects successfully, but the models wont use the dynamic connection.

In short I just want to set the connection dynamically from DB values. Or should I just point Corcel to Laravel default and use Laravel to manage the connection?

Or it could be me missing something obvious, which happens :)

Steps To Reproduce:

maybe something like this:

https://stackoverflow.com/questions/36085131/laravel-connect-to-databases-dynamically

Config::set("database.connections.mysql", [
    "host" => "...",
    "database" => "...",
    "username" => "...",
    "password" => "..."
]);

you can add this before calling
$posts = Post::published()->get();

maybe something like this:

https://stackoverflow.com/questions/36085131/laravel-connect-to-databases-dynamically

Config::set("database.connections.mysql", [
    "host" => "...",
    "database" => "...",
    "username" => "...",
    "password" => "..."
]);

you can add this before calling $posts = Post::published()->get();

Wont this affect following database queries too? Imo would be better to use a predefined connection and explicitly call it with that or raw connection info rather than overwriting.

yoy can change the ’.mysql’ to your connection name

yoy can change the ’.mysql’ to your connection name

Yes but if you do something after that the config still is changed so you have to actively change it back. If you now use lets say Octane that is a bug.

Maybe this can be used https://github.com/corcel/corcel#other-php-framework-not-laravel-setup or it can be patched in if it is not already possible that way.

Reference:

public static function connect(array $params)

as @refocusTheory mentioned:
“ I just want to set the connection dynamically from DB values”

if course it’s depends on the server setup

Might use something like this then so you dont forget to revert your changes, also maybe just define a empty dynamic connection so you dont interfere with actual statically defined connections.

static function withDynamicConnection(array $connection, callable $callback) {
  $original = Config::get('database.connections.dynamic');
  
  $response = $callback()
  
   Config::set('database.connections.dynamic', $original);
   
   return $response;
}

Tought i still think it would be better if this was a feature, should not be hard since Database::connect is also building a Capsule. Something like Capsule::addConnection($dbConnection) instead of Config::set.