Cakephp如何更改数据库连接 [英] Cakephp How to change database connection

查看:150
本文介绍了Cakephp如何更改数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个控制器,ContentController为一般用户,ManageController为管理员。我需要将连接从默认更改为admin,我在我的database.php中有此代码

I have 2 controllers, ContentController for general user and ManageController for administrator. I need to change the connection from default to admin and I have this code in my database.php

class DATABASE_CONFIG {

    public $default = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'user',
        'password' => '',
        'database' => 'ComputerScience',
        'prefix' => '',
        'encoding' => 'utf8',
    );

    public $admin = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'admin',
        'password' => '',
        'database' => 'ComputerScience',
        'prefix' => '',
        'encoding' => 'utf8',
    );
}

谢谢

推荐答案

因此,在你的模型中,你将使用useDbConfig属性:

So, inside your Model, you would use the useDbConfig Attribute:

class Example extends AppModel {
    public $useDbConfig = 'admin';
}

在您的控制器中,只需使用:

Inside your Controller, simply use:

$this->ModelName->useDbConfig = 'admin';

这一切。

这篇关于Cakephp如何更改数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆