我有使用cakePHP开发的项目,它从不同的数据库中获取数据,但如果其中一个数据库某些页面未打开并给我以下错误:
Database table tablenae for model moedlname was not found.
也许更好的方法是缓存结果并从缓存中读取,只需在需要时命中数据库…
- <?PHP
- $cacheKey = 'myCacheNumber1';
- if (($data = Cache::read($cacheKey)) === false) {
- $data = $this->Model->find('all');
- if ($data) {
- Cache::write($cacheKey,$data);
- }
- }
- ?>
这个问题是它假设模型和数据库连接在缓存不存在(或已经过期)时可用,如果不存在,你仍然会得到相同的错误,但频率会肯定会减少.
我认为测试数据库是否可用将需要一些自定义代码技巧,因为连接的蛋糕核心方法假定成功并且在不可用时大量失败.我可能会使用标准的PHP连接方法创建一个组件来控制是否应该尝试加载模型.
- <?PHP
- $cacheKey = 'myCacheNumber1';
- if (($data = Cache::read($cacheKey)) === false) {
- if ($this->DbTest->check('hostname','username','password')) {
- $data = $this->Model->find('all');
- if ($data) {
- Cache::write($cacheKey,$data);
- }
- }
- }
- ?>
- <?PHP
- // app/controllers/components/db_test.PHP
- class DbTestComponent extends Object {
- function check($hostname,$username,$password) {
- $result = true;
- $link = @MysqL_connect($hostname,$password);
- if (!$link) {
- $result = false;
- }
- @MysqL_close($link);
- return $result;
- }
- }
- ?>