代码之家  ›  专栏  ›  技术社区  ›  Randy L

如何从蛋糕外壳读取数据库配置设置?

  •  16
  • Randy L  · 技术社区  · 15 年前

    我想写一个蛋糕外壳,用mysqldump每晚备份我的数据库。我可以把它作为shell脚本来完成,但是如果我能把它塞进一个cakephp shell中,那么我将从它在开发和实时服务器上工作中获得额外的好处,如果我能让它自动读取我的数据库配置设置。我会选择蛋糕壳,并且知道我有数据库的频繁备份,我会有一些平静的想法。

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

    5 回复  |  直到 9 年前
        1
  •  3
  •   Sumon Sarker    9 年前

    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'
    ]
    
        2
  •  29
  •   Morgan O'Neal    14 年前

    App::uses('ConnectionManager', 'Model');
    $dataSource = ConnectionManager::getDataSource('default');
    $username = $dataSource->config['login'];
    
        3
  •  10
  •   dhofstet    15 年前

    App::import('Core', 'ConnectionManager');
    $dataSource = ConnectionManager::getDataSource('default');
    $username = $dataSource->config['login'];
    
        4
  •  1
  •   AFT    12 年前

    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());
    }
    
        5
  •  -1
  •   Sergio    11 年前

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