Codeigniter 在运行时更改数据库配置
我可以更改控制器中每个方法的数据库配置吗?
$db['default']['db_debug'] = TRUE;
默认值为 TRUE,而我需要在某种方法中将其设置为 false 以捕获错误并执行其他操作(例如显示 404 页面)。
当我尝试 $this->config->load('database')
它失败了。
另一个问题:
除了将 db_debug
配置设置为 FALSE
之外,我是否可以检查不正确的查询并将其捕获到某些变量,而不是将其显示给用户?
Can I change the database config per method in a controller?
$db['default']['db_debug'] = TRUE;
The default is TRUE
, while I need to make it false in a certain method to catch the error and do something else (for example show 404 page).
When I tried $this->config->load('database')
it fails.
Another question :
Can I check an incorrect query and catch it to some variables rather than displaying it to users other than setting the db_debug
config to FALSE
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我检查了 system/database/DB_Driver 的代码,发现:
将在我的控制器中工作以动态启用/禁用调试功能。
I checked the code of system/database/DB_Driver and found that:
will work in my controller to enable/disable the debug thing on the fly.
扩展comenk的答案,您可以扩展数据库类并实现各种方法来实现您的目标。
首先,您需要通过创建
MY_Loader.php
文件来扩展核心 Loader 类。通过实现上述内容,您将可以创建一个
MY_DB_mysqli_driver.php
,其中mysqli
被您在 CI database.php 配置中使用的任何驱动程序替换。此时,您将comenk的答案添加到
MY_DB_mysqli_driver.php
然后在您的模型/控制器中,
Expanding on the answer by comenk, you can extend the database class and implement various methods by which to achieve your goal.
First, you'll need to extend the core Loader class by creating a
MY_Loader.php
fileBy implementing the above this will allow you to create a
MY_DB_mysqli_driver.php
wherebymysqli
is replaced by whatever driver you're using in your CI database.php config.At this point you'd add comenk's answer to
MY_DB_mysqli_driver.php
Then in your model/controller,
您必须在 system/database/DB_driver.php 上添加函数,
然后您可以简单地执行此命令来在运行时进行更改
you must add function on system/database/DB_driver.php
after that you can simply do this command to changes at run-time
$this->db->db_debug = 0; // 0:关闭,1:打开
这对我来说...
您可以查看 $GLOBALS 变量来找到此通用设置。
$this->db->db_debug = 0; // 0: off, 1: on
That worx for me...
You can look at the $GLOBALS variable to locate this generic setting.
要向用户隐藏错误的 SQL(和其他错误),您需要设置 php 错误报告级别。 CodeIgniter 基本上以开发模式发布。
转到index.php并将其替换
为this
这是执行此操作的快速方法。您还可以使用钩子来实现这一点,这样您就不必接触 CI 文件。您还可以向该挂钩添加逻辑,以便它仅在生产服务器上设置它。
为了调试 SQL,您可以创建一个继承自 CI_Model 的类,然后创建所有模型类来扩展该类。在该类中,您可以添加用于运行查询的代码,将查询写入日志,以便您可以更轻松地调试它们。如果查询本身很糟糕,这将无济于事,但您应该能够在达到这一点之前弄清楚这一点。
To hide bad SQL (and other errors) from users, you need to set the php error reporting level. CodeIgniter ships in basically development mode.
Go to index.php and replace this
with this
This is the quick way to do it. You can also implement this using a hook, so you don't have to touch CI files. You can also add logic to that hook so that it only sets it on the production server.
For debugging SQL, you can create a class that inherits from CI_Model, then create all your model classes to extend that class. In that class, you can add code for running queries that writes the queries to the log so that you can debug them easier. This won't help if the query itself is bad, but you should be able to figure that out before you get to that point.