简体   繁体   English

找不到cakephp数据库

[英]cakephp database not found

I have project developed using cakephp which is getting data from different DBs, but if one of theses database some pages not open and give me the following error : 我有一个使用cakephp开发的项目,它从不同的数据库获取数据,但是如果其中一个数据库的某些页面无法打开,则出现以下错误:

Database table tablenae for model moedlname was not found. 找不到模型名称的数据库表tablenae。

..and I have in this page other data displayed from the other database which work probably. ..并且我在此页面中显示了可能从其他数据库显示的其他数据。

how i can determine if database is offline using cake and i can make this model read from another place like a cache file until the database startup again. 我如何使用Cake确定数据库是否脱机,并且可以使该模型从另一个地方(如缓存文件)读取,直到再次启动数据库。

Perhaps a better approach is to cache results and read from the cache, only hitting the DB when needed... 也许更好的方法是缓存结果并从缓存中读取,仅在需要时才访问数据库...

<?php
$cacheKey = 'myCacheNumber1';
if (($data = Cache::read($cacheKey)) === false) {
    $data = $this->Model->find('all');
    if ($data) {
        Cache::write($cacheKey, $data);
    }
}
?>

The problem with this is it assumes the model and database connection are available for the time when the cache doesn't exist (or has expired), and if it wasn't, you'd still get the same errors, but the frequency would certainly be reduced. 这样做的问题是,它假定模型和数据库连接在缓存不存在(或已过期)时可用,如果不存在,您仍然会得到相同的错误,但是频率会当然会减少。

I think to test if the DB is available at all would require some custom code trickery since the cake core method of connecting assumes success and fails heavily when not available. 我认为测试数据库是否完全可用将需要一些自定义代码技巧,因为蛋糕的核心连接方法假定成功,并且在不可用时严重失败。 I'd probably make a component with standard PHP connect methods to control if you should attempt to load a model. 我可能会使用标准的PHP connect方法制作一个组件,以控制是否应该尝试加载模型。

<?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,$username,$password);
        if (!$link) {
            $result = false;
        }
        @mysql_close($link);
        return $result;
    }
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM