PHP code example of connehito / cakephp2-master-replica

1. Go to this page and download the library: Download connehito/cakephp2-master-replica library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

connehito / cakephp2-master-replica example snippets


// database.php

class DATABASE_CONFIG {

	public $default = array(
		'datasource' => 'MasterReplica.Database/MasterReplicaMysql',
		'persistent' => true,
		// default connection role(optional)
		'connection_role' => 'master',
		'connections' => array(
			// shared values(you can leave this values empty, but must be declared)
			'_common_' => array(
				'database' => 'app_db',
			),
			// default connection values
			'master' => array(
				'host' => 'db-host',
				'login' => 'root',
				'password' => 'password',
			),
			// `secondary` role connection values
			'secondary' => array(
				'host' => 'replica-host',
				'login' => 'read-only-user',
				'password' => 'another-password',
			),
		),
	);
}

$Post = ClassRegistry::init('Post');
// as default, connect with `master` role.
$Post->save(array('Post' => array('user_id' => 10, 'title' => 'new post', 'content' => 'some content')));

// switch to `replica` role
$conn = $this->Post->getDataSource();
$conn->switchConnectionRole('secondary');