如何从cakepp中的当前网站自动登录到另一个数据库


How do I automatically login to another database from a current website in cakephp

在cakephp中,我登录到mysql数据库没有问题。我有一个问题,登录后我需要访问另一个cakepp网站上同一服务器上的另一个数据库(点击按钮)。我不希望用户再次登录其他网站。我找不到解决这个问题的资料。如何自动登录到另一个cakepp网站?更清楚地说,我希望用户登录,然后点击一个按钮,使用另一个带有不同数据库的cakepp网站,而无需登录。Setdatasource似乎停留在同一个cakepp网站上,并切换数据库

以下代码工作正常(在我的电脑上),但我需要自动登录到网站,而无需再次登录

echo $this->Html->link( 'link!', 'http://127.0.0.1/maths/numeracyStudents/dashboardst'); 
public $mathdb = array(
    'datasource' => 'Database/Mysql',
    'persistent' => false,
    'host' => '127.0.0.1',
    'login' => 'root',
    'password' => '',
    'database' => 'maths',
    'prefix' => '',
    //'encoding' => 'utf8',
);
//called this function from the controller and nothing happens?
public function changedb() {
    $this->NumeracyStudents->setDataSource('mathdb');
}

在应用程序的app.php中,您将创建另一个与默认数据库配置类似的数据库配置并对其进行标记。然后,您将使用该配置创建连接。

编辑:我假设你使用的是CakePHP 2.X

database.php

class DATABASE_CONFIG {
   public $default = array(
    'datasource'  => 'Database/Mysql',
    'persistent'  => false,
    'host'        => 'localhost',
    'login'       => 'cakephpuser',
    'password'    => 'c4k3roxx!',
    'database'    => 'my_cakephp_project',
    'prefix'      => ''
    );
   public $otherDb = array(
      'datasource'  => 'Database/Mysql',
      'persistent'  => false,
      'host'        => 'localhost',
      'login'       => 'cakephpuser',
      'password'    => 'c4k3roxx!',
      'database'    => 'my_cakephp_project2',
      'prefix'      => ''**strong text**
   );
}

文章控制器:

//inside a function(action) you would call this to set to the other datasource
$this->Articles->setDataSource('otherDb');