2015-11-02 67 views
0

在cakephp中,我登录到mysql数据库没有问题。我有一个问题,我登录后需要访问另一个cakephp网站上同一服务器上的另一个数据库(按钮单击)。我不希望用户在另一个网站上再次登录。我找不到解决这个问题的信息。我如何自动登录到另一个cakephp网站? 要更清楚我想要的是我想要用户登录,然后单击一个按钮并使用另一个不同数据库的cakephp网站,而无需登录。的setDataSource似乎是住在同一个CakePHP的网站和交换数据库如何从cakephp中的当前网站自动登录另一个数据库

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

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'); 
} 

回答

0

里面你应用程序的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** 
    ); 
} 

ArticlesController:

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

我不明白这一点,因为database.php中是不同的你说什么。有没有newQuery – ajt

+0

对不起,我应该问你使用什么版本的蛋糕? – chrisShick

+0

我正在使用cakephp 2.x,我没有得到如何在登录后自动登录到另一个数据库? – ajt

相关问题