CakePHP v2.x を使用しています。私の現在のデータベースは MySQL です。コントローラーの下に記述されたメソッド/関数内で他のデータベースに接続する必要があります。ここまでで、別の DB 接続配列 $test を Config/database.php に追加しました。
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'test',
'password' => 'test1',
'database' => 'test_portal',
'prefix' => ''
//'encoding' => 'utf8',
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'dfffd_23',
'password' => 'dsfsd324',
'database' => 'testdbuser',
'prefix' => ''
//'encoding' => 'utf8',
);
}
「aezips」という名前のテーブルを接続する必要があるため、新しいモデルを作成しました。
class Aezips extends AppModel {
public $name = 'Aezip';
public $useDbConfig = 'test';
//public $useTable = 'aezips';
}
私のコントローラーに追加されました;
public $uses = array('Aezip');
addという名前の関数/メソッドを持つコントローラー、
public function add() {
$this->layout = NULL;
$this->autoRender = false; //will prevent render of view
if($this->RequestHandler->isAjax()){
Configure::write('debug', 0); //it will avoid any extra output
}
//$this->Aezip->useDbConfig('test');
$t = $this->Aezip->find('all'); //To fetch data from aezips table
print_r($t);
print_r($this->data);
}
しかし、Aezip テーブルに接続できず、エラーも表示されません。両方のデータベースは同じサーバーにありますが、cpanel アカウントは異なります。