Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :

Database table tablenae for model moedlname was not found.

..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.

like image 608
SMSM Avatar asked Jun 08 '10 16:06

SMSM


1 Answers

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
$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;
    }
}
?>
like image 179
zeroasterisk Avatar answered Oct 28 '22 10:10

zeroasterisk