在用框架cakephp做项目时遇到这样的问题:需要新建一个数据库,然后此项目与新建的库中的表链接,用loadModel('testQuestion’);然后$this->testQuestion->useDbCOnfig= 'testBase';这样的逻辑应该是没有问题的,但是在database配置中默认的库是wss,这个库中是没有这个表的,
-
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' => '',
);
那么显示页面的时候就会报404错。最后找到了解决方法,如下:
-
/*
-
* 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;
}
就是在models文件夹下建立一个以wss_test库中的该表名为名的model文件,然后注明使用哪个库就好了。
顺便说一下:如默认的库中有这个表的话可以不用建model文件说明。如在主从库中可以这么使loadModel('testQuestion’);然后$this->testQuestion->useDbCOnfig= 'testBase';