【问题标题】:Cakephp can't change database on-the-flyCakephp 不能即时更改数据库
【发布时间】:2012-12-07 01:34:02
【问题描述】:

我正在尝试从循环中连接多个数据库,但看到 CakePHP 无法更改 database,只能更改其他信息(如用户/密码/主机)。

app/Config/database.php

<?php
class DATABASE_CONFIG {
    [...]

    public $default = array(
        [..] // Where I have the companies
    );
    public $client = array(
        [...] // Fakke settings, because I will change it on-the-fly
    );
}

app/Controller/CronController.php

$companies = $this->Company->find('all');
foreach($companies as $company) {
    $settings = array(
        'datasource' => 'Database/Mysql',
        'host' => $company['Company']['host'],
        'login' => $company['Company']['username'],
        'password' => $company['Company']['password'],
        'database' => $company['Company']['database'],
    );

    ConnectionManager::drop('client');
    $db = ConnectionManager::create('client', $settings);

    try {
        debug($this->MyModel->find('first'));
    } catch (Exception $e) {
        echo '<pre>';
        echo "Exception: ",  $e->getMessage(), "\n";

        /*
        debug($this->MyModel->getDataSource());

        Outputs:

        [...]
        [config] => Array
            (
                [persistent] => 
                [host] => 0.0.0.0 // CORRECT HOST
                [login] => root // CORRECT LOGIN
                [password] => pass // CORRECT PASSWORD
                [database] => database1
                [port] => 3306
                [datasource] => Database/Mysql
                [prefix] => 
                [encoding] => utf8
            )
        [...]
        */
    }
}

它返回第一个连接和所有其他连接,我无法从 MyModel 中选择任何内容,因为它是错误的。它看到来自用户/密码/主机的连接是好的,但是,数据库没有改变,所以,因为用户没有在 cdatabase 上选择的权限,我得到了错误。

Array
(
    // First connection, connection ok, MyModel return nothing
)

// Second connection
Exception: SQLSTATE[42000]: Syntax error or access violation: 1142 SELECT command denied to user 'database_user_2'@'localhost' for table 'my_model'

// Third connection
Exception: SQLSTATE[42000]: Syntax error or access violation: 1142 SELECT command denied to user 'database_user_3'@'localhost' for table 'my_model'

// Fourth connection
Exception: SQLSTATE[42000]: Syntax error or access violation: 1142 SELECT command denied to user 'database_user_4'@'localhost' for table 'my_model'

// Fifth connection
Exception: SQLSTATE[42000]: Syntax error or access violation: 1142 SELECT command denied to user 'database_user_5'@'localhost' for table 'my_model'

谢谢!

【问题讨论】:

标签: php cakephp cakephp-2.0 cakephp-2.1 saas


【解决方案1】:

尽量不要放弃配置,只需改变你需要的东西。

我成功使用了这个任务

$dataSource = ConnectionManager::getDataSource('company_data');
$dataSource->config['schema'] = 'company_'.$id;

我不知道数据库切换和 mysql 作为引擎是否是好的一对。为此,我使用了 postgresql 模式。

【讨论】:

    【解决方案2】:

    您可以使用模型方法setSource() 更改表或setDataSource() 更改数据库配置。

    【讨论】:

      猜你喜欢
      • 2017-08-04
      • 2019-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多