CakePpP Switch Database Connection in Controller for CakePHP 2.2.5

I am trying to get data from two different databases in the controller

application /Controller/UsersController.php

my db connections are declared in database.php

$default = array(
    ...
    'database' => 'test'
    ...
    );
$test = array(
    ...
    'database' => 'test1'
    ...
    );

and in action display():

public function display() {
    $this->set('defaultUsers', $this->User->find('all'));
    $this->User->schemaName = 'test1';
    $this->User->cacheQueries = false;
    $this->set('testUsers', $this->User->find('all'));
}

This would allow me to get data from two different sources, however, the problem is that these two databases must have the same password, otherwise it will not work.

I tried other solutions found here and on other sites. as:

  • change $this->User->useDbConfig = 'test'and $this->User->cacheQueries = falsestill give me the same dataset;

  • Using ConnectionManager::getDataSource()and setConfig(), create(), drop(), setDataSource(), etc. None of them work, and some do not even exist anymore.

! a.k.a .

!

+5
1

, 'setDataSource()' /, reset "" .. ;

public function display() {
    $this->set('defaultUsers', $this->User->find('all'));
    $this->User->setDataSource('test1');
    $this->set('testUsers', $this->User->find('all'));
}

, - , "" , , "test1" . ,

:

class TestUser extends AppModel {
    public $useTable = 'users'; // name of the database table 
    public $useDbConfig = 'test1';  // name of the database configuration in database.php
}

: "$ test" - , CakePHP.

+4

All Articles