cakephp calls data tables in different databases

黄舟
Release: 2023-03-03 16:16:02
Original
1250 people have browsed it

I encountered this problem when using the cakephp framework to make a project: I need to create a new database, and then link this project with the table in the newly created library, use loadModel('testQuestion'); and then $this->testQuestion->useDbConfig = 'testBase'; There should be no problem with this logic, but the default library in the database configuration is wss, and there is no such table in this library.

Php code

class DATABASE_CONFIG {   
  
    var $default = array(   
        'driver' => 'mysql',   
        'persistent' => false,   
        'host' => '192.168.9.10',   
        'login' => 'root',   
        'password' => '123456',   
        'database' => 'wss',   
        'encoding' => 'utf8',   
        'prefix' => '',   
    );   
        var $testBase = array(   
        'driver' => 'mysql',   
        'persistent' => false,   
        'host' => '192.168.9.10',   
        'login' => 'root',   
        'password' => '123456',   
        'database' => 'wss_test',   
        'encoding' => 'utf8',   
        'prefix' => '',   
    );  
class DATABASE_CONFIG {

	var $default = array(
		'driver' => 'mysql',
		'persistent' => false,
		'host' => '192.168.9.10',
		'login' => 'root',
		'password' => '123456',
		'database' => 'wss',
		'encoding' => 'utf8',
		'prefix' => '',
	);
        var $testBase = array(
		'driver' => 'mysql',
		'persistent' => false,
		'host' => '192.168.9.10',
		'login' => 'root',
		'password' => '123456',
		'database' => 'wss_test',
		'encoding' => 'utf8',
		'prefix' => '',
	);
Copy after login

Then 404 will be reported when the page is displayed. wrong. Finally, I found a solution, as follows:

Php code

/*  
 * testBase库中model  
 *   
 */  
class TestQuestion extends AppModel{   
    public $name = 'PreschoolTestQuestion';   
    var $useDbConfig = 'testBase';   
    var $useTable = false;   
}  
/*
 * testBase库中model
 * 
 */
class TestQuestion extends AppModel{
	public $name = 'PreschoolTestQuestion';
	var $useDbConfig = 'testBase';
	var $useTable = false;
}
Copy after login

Just create a model file named after the table name in the wss_test library under the models folder, and then indicate which library to use.

By the way: If there is this table in the default library, there is no need to create a model file description. For example, in the master-slave library, you can use loadModel('testQuestion'); and then $this->testQuestion->useDbConfig = 'testBase';

The above is the content of cakephp calling data tables in different databases, more related Please pay attention to the PHP Chinese website (www.php.cn) for content!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template