gpt4 book ai didi

php - 找不到 cakephp 数据库

转载 作者:可可西里 更新时间:2023-11-01 06:33:59 25 4
gpt4 key购买 nike

我有使用 cakephp 开发的项目,它从不同的数据库获取数据,但是如果这些数据库之一的某些页面无法打开并给我以下错误:

Database table tablenae for model moedlname was not found.

..我在此页面中显示了其他数据库中可能有效的其他数据。

我如何使用 cake 确定数据库是否离线,并且我可以让这个模型像缓存文件一样从另一个地方读取,直到数据库再次启动。

最佳答案

也许更好的方法是缓存结果并从缓存中读取,只在需要时访问数据库...

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

关于php - 找不到 cakephp 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2999311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com