开发者

How Can I Read the DB Configuration Settings From a Cake Shell?

I would like to write a cake shell to do a nightly backup of my database using mysqldump. I could do this as a shell script, but if I can cram it into a CakePHP shell, then I will get the added benefit of it working across both the development and live server, if I can get it to automagically read my database confi开发者_如何转开发g settings. I will cron the cake shell and have some peace-of-mind knowing that I have frequent backups of my DB.

In my shell I'm trying to build up a string which starts with "mysqldump --user=" and I'd like to get the username from app/config/database.php. How can I get at the data in database.php?


In cake 2.1 the format has changed to:

App::uses('ConnectionManager', 'Model');
$dataSource = ConnectionManager::getDataSource('default');
$username = $dataSource->config['login'];


The following snippet should do the trick:

App::import('Core', 'ConnectionManager');
$dataSource = ConnectionManager::getDataSource('default');
$username = $dataSource->config['login'];


In CakePHP 3.x the format has changed to -

use Cake\Datasource\ConnectionManager;
$source = ConnectionManager::get('default');

debug($source); #Debugging the result

Result :

object(Cake\Database\Connection) {
  'config' => [
    'password' => '*****',
    'username' => '*****',
    'host' => '*****',
    'database' => '*****',
    'driver' => 'Cake\Database\Driver\Mysql',
    'persistent' => false,
    'encoding' => 'utf8',
    'timezone' => 'UTC',
    'cacheMetadata' => true,
    'quoteIdentifiers' => false,
    'log' => false,
    'url' => null,
    'name' => 'remote'
  ],
  'driver' => object(Cake\Database\Driver\Mysql) {
    'connected' => false
  },
  'transactionLevel' => (int) 0,
  'transactionStarted' => false,
  'useSavePoints' => false,
  'logQueries' => false,
  'logger' => null
}

Get the Result :

debug($source->config()); #Accessing the result

Result :

[
  'driver' => 'Cake\Database\Driver\Mysql',
  'persistent' => false,
  'host' => 'localhost',
  'username' => 'username',
  'password' => 'password',
  'database' => 'database',
  'encoding' => 'utf8',
  'timezone' => 'UTC',
  'cacheMetadata' => true,
  'quoteIdentifiers' => false,
  'log' => false,
  'url' => null,
  'name' => 'remote'
]


Just for sharing.

For any cakephp project, if using php as cronjob or command line to do large data processing I would build a standalone php script without cakephp, the reason for doing this because cakephp is slow (e.g. read & process 100K records).

To make my life simple I put all my standalone scripts under app/Vendor/myscripts/ (e.g: app/Vendor/myscripts/process.php)

below also the basic code to make sure you use the same database settings in standalone script with cakephp (tested with MySQL)

require_once '/XYZ/app/Config/database.php';
$database = new DATABASE_CONFIG;
try{
        $dblink = new PDO('mysql:host='.$database->default['host'].';dbname='.$database->default['database'], $database->default['login'],  $database->default['password'], array(PDO::ATTR_PERSISTENT => false));
        $dblink->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $dblink->exec('SET CHARACTER SET '.$database->default['encoding']);
} catch (Exception $e) {
        die('DB Error'. $e->getMessage());
}


Example in controller, Change multi DB for DataSources in CakePHP 2.5.x

App::uses('AppController', 'Controller');

class DemoController extends AppController {

public $uses = array('AppModel', 'GVA21', 'GVA01', 'GVA14', 'GVA24' );
public function test_dbs(){
    $this->autoRender=false;
    // Load ConnectManager
    App::uses('ConnectionManager', 'Model');
    // DataSource ['default']
    $MDM = $this->GVA14->find('count');
    echo "MDM.GVA14\n<br>";
    debug($MDM);
    // Get DataSource Config DB ['default'] and ['SRL']
    $confDeafult = ConnectionManager::$config->default;
    $confSrl = ConnectionManager::$config->SRL;
    // Change DataSource ['SRL']
    ConnectionManager::drop('default');
    ConnectionManager::create('default',$confSrl);  //<== Is permanet change Find All models Down
    // $this->GVA01->setDataSource('SRL'); //<== Is temp change Find model
    echo "SRL.GVA14\n<br>";
    $SRL = $this->GVA14->find('count');
    debug($SRL);
    $SRL = $this->GVA01->find('count');
    echo "SRL.GVA01\n<br>";
    debug($SRL);
    $SRL = $this->GVA21->find('count');
    echo "SRL.GVA21\n<br>";
    debug($SRL);
    // Change to DataSource ['default']
    debug(ConnectionManager::drop('default'));
    ConnectionManager::create('default',$confDeafult); //<== Is permanet change Find All models Down
    //$this->GVA01->setDataSource('default'); //<== Is temp change Find model
    $MDM = $this->GVA01->find('count');
    echo "MDM.GVA01\n<br>";
    debug($MDM);
    $MDM = $this->GVA21->find('count');
    echo "MDM.GVA21\n<br>";
    debug($MDM);
    ////FIN
    exit('FIN');
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜