2013-10-25 35 views
0

我想在CakePHP中設置數據庫,提供主機,登錄名和密碼以及表單(所以不需要自己編寫database.php文件)。目的是CakePHP的某種自動設置(我會用它來提供salt和cypherSeed)。 這可能嗎?什麼是最好的方式去做?我閱讀了關於通過PHP編寫文件的一些信息...CakePHP - 使用表格設置數據庫

回答

0

如果您從控制器創建/加載數據庫連接,則可以使用此數據變量。我不認爲用表單編寫database.php文件是個好主意。 所以我會做這樣的事情:

//Controller 
$this->Formdatabase = ClassRegistry::init('Formdatabase'); 
$db_host = '';//load from file or DB 
$db_user = '';//load from file or DB 
$db_pass = '';//load from file or DB 
$db_database = '';//load from file or DB 
ConnectionManager::create("form_database", array(
     'datasource' => 'Database/Mysql', 
     'driver' => 'mysql', 
     'persistent' => false, 
     'host' => $db_host, 
     'login' => $db_user, 
     'password' => $db_pass, 
     'database' => $db_database, 
     'prefix' => '', 
     'encoding' => 'UTF8', 
     'port' => '', 
)); 
$this->Formdatabase->useTable = ''; //table you want to use. (in Model or controller) 
//Model 
<?php 
    class Formdatabase extends Model { 
public $useDbConfig = 'form_database'; 
    } 
?> 
//Now you can use $this->Formdatabase->find('...'); ... 

希望我可以幫助,祝你好運!

+0

是的我thinc這可能有所幫助。謝謝 :-) – Thiezar