0

我想在 CakePHP 中设置数据库,通过表单提供主机、登录名和密码(因此无需自己编写 database.php 文件)。目的是某种 CakePHP 的自动设置(我也会用它来提供 salt 和 cypherSeed)。这可能吗?最好的方法是什么?我读了一些关于通过 PHP 编写文件的内容......

4

1 回答 1

0

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. 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!

于 2013-10-25T18:48:08.930 回答