【问题标题】:CakePHP - Setup database with a formCakePHP - 使用表单设置数据库
【发布时间】:2013-10-25 13:51:43
【问题描述】:

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

【问题讨论】:

    标签: php database cakephp installation edit


    【解决方案1】:

    如果您从控制器创建/加载数据库连接,则可以使用此数据变量。我认为用表单编写 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('...'); ...
    

    希望我能帮上忙,祝你好运!

    【讨论】:

    • 是的,我认为这会有所帮助。谢谢:-)
    猜你喜欢
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多