简体   繁体   English

CakePHP-使用表单安装数据库

[英]CakePHP - Setup database with a form

I would like to setup the database in CakePHP providing host, login and password with a form (so without writing the database.php file myself). 我想在CakePHP中设置数据库,以某种形式提供主机,登录名和密码(因此无需自己编写database.php文件)。 The purpose is some kind of automatic setup of CakePHP (I would use this to provide salt and cypherSeed too). 目的是某种CakePHP的自动设置(我也将使用它来提供盐和cypherSeed)。 Is this possible? 这可能吗? What's the best way to do go? 最好的方法是什么? I read something about writing file via PHP... 我读过一些有关通过PHP编写文件的信息...

If you create/load your database connection from your controller you can have this data variable. 如果您从控制器创建/加载数据库连接,则可以使用此数据变量。 I don't think it's a good idea to write database.php file with a form. 我认为用表格编写database.php文件不是一个好主意。 So I would do something like this: 所以我会做这样的事情:

//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('...'); ...

Hope I could help, good luck! 希望我能有所帮助,祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM