[英]CakePHP - Setup database with a form
我想在CakePHP中设置数据库,以某种形式提供主机,登录名和密码(因此无需自己编写database.php文件)。 目的是某种CakePHP的自动设置(我也将使用它来提供盐和cypherSeed)。 这可能吗? 最好的方法是什么? 我读过一些有关通过PHP编写文件的信息...
如果您从控制器创建/加载数据库连接,则可以使用此数据变量。 我认为用表格编写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('...'); ...
希望我能有所帮助,祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.