2010-10-09 71 views
16

我想寫一個蛋殼來使用mysqldump來做每晚數據庫的備份。我可以將它作爲一個shell腳本來執行,但是如果我可以將它塞進一個CakePHP shell中,那麼如果我可以自動讀取數據庫配置設置,那麼我將在開發和活動服務器上獲得額外的好處。 。我會克隆蛋糕殼,並有一些安心的知道,我有我的數據庫頻繁備份。如何從Cake Shell讀取數據庫配置設置?

在我的shell中,我試圖建立一個以「mysqldump --user =」開頭的字符串,我想從app/config/database.php獲取用戶名。我如何獲取database.php中的數據?

回答

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' 
] 
+1

請參閱[CurrentConfig](https://github.com/dereuromark/cakephp-setup/blob/master/src/Shell/CurrentConfigShell.php)shell。 – mark 2017-04-23 13:37:36

9

下面的代碼片段應該做的伎倆:

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

工程就像一個魅力。謝謝! – the0ther 2010-10-09 23:25:07

29

在蛋糕2.1格式已更改爲:

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

感謝您的更新。最近我一直在玩新版蛋糕,似乎有不少變化。 – the0ther 2012-04-18 03:27:28

1

只是爲了共享。對於任何cakephp項目,如果使用php作爲cronjob或命令行來執行大數據處理,我將構建一個獨立的php腳本,而不使用cakephp,原因是cakephp很慢(例如,讀取&進程100K記錄)。

使我的生活簡單的我把下應用程序/供應商/ myscripts /我所有的獨立腳本(如:應用程序/供應商/ 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()); 
} 
-1

例如,更改多DB爲數據源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'); 
}