connect to database mid-script |
Hi,
I have created a CLI command which sets up the database. The user is asked for DB credentials. PHP Code: $dbHost = CLI::prompt('Database host', 'localhost'); This sets the provided values in the .env file. But, within this same command I want to run migrations. PHP Code: $migrate = Services::migrations(); But since it's in the same script/command, I don't have access to the database yet. Is there a way I can connect to the provided credentials mid-script and run the migrations on that connection?
Create array $config = [dbhost, dbname, ...] for db connect and inject to Services::migrations(null, db_connect($config))
I'm afraid that doesn't work.
Here's what I did... PHP Code: $config = [ The print_r does provide a connection object: Code: CodeIgniter\Database\MySQLi\Connection Object But the error when trying to migrate is: Access denied for user ''@localhost (using password: NO). Since there is no user, I suspect the connection object is not used properly, because the user is present in the connection object... I use the same credentials to login to the database localy....
[password:protected] =>
Empty. Connection work, but config failed. Hmm
In this particular case the password should be empty, it's a local XAMP install.
I have tried it with another user and pass, results are the same.
Can I run another cli command from this cli command.
Then I can put this migration in the second one. Then the .env should be loaded when runnning the second one.
May be \Config\Services::commands()->run('migrate', []); ?
|
Welcome Guest, Not a member yet? Register Sign In |