Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP-使用表单设置数据库_Php_Database_Cakephp_Installation_Edit - Fatal编程技术网

CakePHP-使用表单设置数据库

CakePHP-使用表单设置数据库,php,database,cakephp,installation,edit,Php,Database,Cakephp,Installation,Edit,我想在CakePHP中设置数据库,用表单提供主机、登录名和密码(因此我自己不需要编写database.php文件)。其目的是某种CakePHP的自动设置(我也会使用它来提供盐和香草籽)。 这可能吗?最好的方法是什么?我读了一些关于通过PHP编写文件的内容…如果您从控制器创建/加载数据库连接,您可以使用此数据变量。我认为用表单编写database.php文件不是一个好主意。 所以我会这样做: //Controller $this->Formdatabase = ClassRegistry::

我想在CakePHP中设置数据库,用表单提供主机、登录名和密码(因此我自己不需要编写database.php文件)。其目的是某种CakePHP的自动设置(我也会使用它来提供盐和香草籽)。
这可能吗?最好的方法是什么?我读了一些关于通过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('...'); ...
//控制器
$this->Formdatabase=ClassRegistry::init('Formdatabase');
$db_host=''//从文件或数据库加载
$db_user=''//从文件或数据库加载
$db_pass=''//从文件或数据库加载
$db_数据库=“”//从文件或数据库加载
ConnectionManager::create(“form_数据库”,数组(
'datasource'=>'Database/Mysql',
“驱动程序”=>“mysql”,
'persistent'=>false,
“主机”=>$db\u主机,
'login'=>$db\u用户,
“密码”=>$db\u密码,
“数据库”=>$db\u数据库,
'前缀'=>'',
'编码'=>'UTF8',
'端口'=>'',
));
$this->Formdatabase->useTable=''//您要使用的表。(在模型或控制器中)
//模型
//现在您可以使用$this->Formdatabase->find(“…”)。。。

希望我能帮忙,祝你好运

是的,我觉得这会有帮助。谢谢:-)