首页 文章

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

提问于
浏览
16

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

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

5 回答

  • 29

    In CakePHP 3.x 格式已更改为 -

    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'
    ]
    
  • 3

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

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

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

    App::import('Core', 'ConnectionManager');
    $dataSource = ConnectionManager::getDataSource('default');
    $username = $dataSource->config['login'];
    
  • 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());
    }
    
  • 10

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

相关问题