Different cakephp datasource for local and live - cakephp

My MySQL connection details are different for both my local connection and my deployed live hosted. I am using CakePHP 3
At the moment I have to keep changing the default datasource which is not really the best way to do it.
I have not added two datasources but I am not sure how to switch between them?
'Datasources' => [
'development' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => '127.0.0.1',
'port' => '8889',
'username' => 'root',
'password' => 'root',
'database' => 'local',
],
'deployment' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'username',
'password' => 'password',
'database' => 'live_database',
],

In boostrap or in App Controller, paste this
if(Configure::read('debug')){
ConnectionManager::config('deployment');
}
this change the default config of database when the debug is true.

Related

How to use multiple db2 databases in Laravel

Currently i use the package cooperl/laravel-db2 for the database connection. For my project, i should to toggle schema database.
I try this in db2.php :
'connections' => [
'ibmi_1' => [
...
'host' => '***********',
'username' => '***********',
'password' => '***********',
'database' => 'database',
'prefix' => '',
'schema' => 'schema_1',
'port' => *****,
'date_format' => 'Y-m-d H:i:s',
...
],
'ibmi_2' => [
...
'host' => '***********',
'username' => '***********',
'password' => '***********',
'database' => 'database',
'prefix' => '',
'schema' => 'schema_2',
'port' => *****,
...
],
And this in .env
DB_CONNECTION=ibmi_1
DB_HOST=
DB_PORT=
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
DB_CONNECTION=ibmi_2
DB_HOST=
DB_PORT=
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
But when i try in tinker
DB::connection('ibmi_2')
I have this error
InvalidArgumentException with message 'Database connection [ibmi_2] not configured.'
What is wrong ?
The problem was that the database configuration wasn't in bootstrap/cache/config.php.

Laravel Unit Test separate database

Can somebody help me, how can i run my tests, but they will interact with database copy or a virtual database. I know, that i need to use phpunit.xml file, but how? Give me a example please.
I have found a workaround for this.
In your config/database.php bellow mysql add:
'mysql_testing' => [
'driver' => 'mysql',
'host' => env('TESTING_DB_HOST', 'localhost'),
'database' => env('TESTING_DB_DATABASE', 'forge'),
'username' => env('TESTING_DB_USERNAME', 'forge'),
'password' => env('TESTING_DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
And now in your .env add the values for:
TESTING_DB_HOST=localhost
TESTING_DB_DATABASE=homestead_testing
TESTING_DB_USERNAME=homestead
TESTING_DB_PASSWORD=secret
Now you can run
php artisan migrate —database=mysql_testing
And the last thing - open your phpunit.xml file in the app folder and add this:
<env name="DB_CONNECTION" value="mysql_testing"/>
<?php
return [
// other stuff
'default' => env('DB_DEFAULT', 'mysql'),
'connections' => [
'sqlite_testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
// other stuff
];
For controller
public function createApplication()
{
putenv('DB_DEFAULT=sqlite_testing');
$app = require __DIR__ . '/../../bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
return $app;
}
public function setUp()
{
parent::setUp();
Artisan::call('migrate');
}
public function tearDown()
{
Artisan::call('migrate:reset');
parent::tearDown();
}
#pardeep in test\CreatesApplication trait there is just one function
add this putenv('DB_DEFAULT=sqlite_testing');

create a separate database config file in CakePHP 3

I'm using CakePHP 3.4
default database settings exists in config/app.php
I want to separate out or override database configuration outside app.php say in config/my_db.php and load it in bootstrap.php file.
This setting will now override default database setting that exists in app.php file.
Is there some way to do this ?
Edit 2
config/my.db.php file
<?php
return [
'my_db' => [
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'root',
'password' => 'my_pass',
'database' => 'testdb',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
]
]
]
];
loading in bootstrap.php
Configure::load('my_db', 'default', false);
Create new file into folder config/
Name it whatever you like: my_db.php
Add your code configuration Code:
return [
'my_db' => [
'setting_1' => 'value_1',
'setting_2' => 'value_2',
'setting_3' => 'value_3',
],
];
Now you have to load it. Open file config/bootstrap.php,
locate line:
Configure::load('app', 'default', false);
and append this line underneath:
Configure::load('my_db', 'default');
Try THis ::
config/bootstrap.php
Configure::load('my_app', 'default','false');
config/my_app.php
<?php
return [
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'root',
'password' => 'my_pass',
'database' => 'my_db',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
]
]
];
make a copy of your app.php, name it app_override.php and change your db-settings.
Then adapt your bootstrap.php like this
try {
Configure::config('default', new PhpConfig());
Configure::load('app', 'default', false);
} catch (\Exception $e) {
exit($e->getMessage() . "\n");
}
Configure::load('app_override', 'default');

CakePHP 3 Multiple Databases

I want to connect to a second (remote) database using CakePHP 3. I have found solutions online that suggest how to associate different models with different databases but that is not what I need to achieve.
I need to be able to connect to a remote database (that is not associated with any model) and read/write some records from an action in my controller. Can this be achieved using CakePHP?
Edit (more information):
I have a website that acts as a booking platform for hotel rooms. The availability of these rooms can be controlled via my website and stored in my database. But for some clients I want to be able to connect to their private database directly and use their records to check availability.
Follow the Below Instructions
Step 1 : Open config/app.php Find Datasources array and add Remote Database configuration like as -
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'port' => '3306',
'username' => 'YOUR_DB_USER',
'password' => 'YOUR_DB_PASS',
'database' => 'YOUR_DB_NAME',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => false,
'log' => false,
'quoteIdentifiers' => false,
'url' => env('DATABASE_URL', null),
],
'remote_db_1' => [ /*Remote Database 1*/
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => '192.168.1.47', /*YOUR_REMOTE_SERVER_IP*/
'port' => '3306',
'username' => 'REMOTE_DB_USER',
'password' => 'REMOTE_DB_PASS',
'database' => 'REMOTE_DB_NAME',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => false,
'log' => false,
'quoteIdentifiers' => false,
'url' => env('DATABASE_URL', null),
],
'remote_db_2' => [ /*Remote Database 2*/
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => '192.168.1.47', /*YOUR_REMOTE_SERVER_IP*/
'port' => '3306',
'username' => 'REMOTE_DB_USER',
'password' => 'REMOTE_DB_PASS',
'database' => 'REMOTE_DB_NAME',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => false,
'log' => false,
'quoteIdentifiers' => false,
'url' => env('DATABASE_URL', null),
],
Step 2 : Now you can access Remote database in your controller like as-
use Cake\Datasource\ConnectionManager;
use \PDO;
class YourController extends AppController{
public function getRemoteData(){
$conn1 = ConnectionManager::get('remote_db_1'); #Remote Database 1
$conn2 = ConnectionManager::get('remote_db_2'); #Remote Database 2
}
}
Note: Now you can use PDO methods to Insert,Retrieve,Update
Example :
class YourController extends AppController{
public function getRemoteData(){
$conn1 = ConnectionManager::get('remote_db_1'); #Remote Database 1
$sql = "SELECT * FROM users";
$query = $conn1->prepare($sql);
$query->execute();
$result = $query->fetchAll(); #Here is the result
}
}

Cakephp3 multiple databases

I wanted to ask, if is possible to work with 2 databases in cakephp3, using raw sql ?
I have query like this:
select * from shop.brochures b, upload.documents up where b.doc_id = up.id;
The problem is, this are 2 databases.
I don't know, how setup connection, i think that will not work ( doc example )
$conn = ConnectionManager::get('default');
Thank You for any advice.
in /config/app.php you can set as many db as you want; i have 2.
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'sisarticulos',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
],
'gente' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'sggeneral',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,´
then in the controller you can set
$conn = ConnectionManager::get('default');
//some code
$conn = ConnectionManager::get('get');
use default first and then use gente

Resources