如何从Cake Shell读取数据库配置设置?

时间:2010-10-09 04:04:37

标签: cakephp

我想编写一个蛋糕shell,使用mysqldump对我的数据库进行夜间备份。我可以将它作为一个shell脚本,但是如果我可以将它塞进一个CakePHP shell中,那么我将获得它在开发和实时服务器上工作的额外好处,如果我可以让它自动读取我的数据库配置设置。我知道我经常备份我的数据库,并且知道我经常备份我的数据库,因此我会安排蛋壳外壳。

在我的shell中,我正在尝试构建一个以“mysqldump --user =”开头的字符串,我想从app / config / database.php获取用户名。我怎样才能获得database.php中的数据?

5 个答案:

答案 0 :(得分:29)

在蛋糕2.1中,格式已更改为:

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

答案 1 :(得分:10)

以下代码段可以解决这个问题:

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

答案 2 :(得分:2)

CakePHP 3.x格式已更改为 -

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

debug($source); #Debugging the 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
}

获取结果:

debug($source->config()); #Accessing the 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'
]

答案 3 :(得分:1)

仅供分享。

对于任何cakephp项目,如果使用php作为cronjob或命令行来进行大数据处理,我会构建一个没有cakephp的独立php脚本,之所以这样做是因为cakephp很慢(例如读取和处理100K记录)。

为了让我的生活变得简单,我将所有独立脚本放在app / Vendor / myscripts /下(例如:app / Vendor / myscripts / process.php)

下面还有一些基本代码,以确保在cakephp的独立脚本中使用相同的数据库设置(使用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());
}

答案 4 :(得分:-1)

控制器中的示例,在CakePHP 2.5.x中更改DataSource的多个数据库

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');
}