CodeIgniter 和自动加载数据库
我在使用 CodeIgniter 连接数据库时遇到问题。
在模型中,如果我
$this->load->database();
这样做,则诸如之类的查询
$query = $this->db->get('articles', 10);
会起作用并返回数据。但是,当我删除 load->database 行并尝试在 application/config/autoload.php 中使用自动加载数据库时
$autoload['libraries'] = array('database');
,上述查询失败。由于当我显式加载库时它可以工作,因此我的数据库连接不是问题。我正在使用 CodeIgniter 2.0.2(当前最新版本)。
由于我的大多数页面都使用数据库,我想自动加载它以避免必须在每个模型中手动加载它。我是否误解了有关加载数据库的文档或者我做错了什么?
I've got an issue connecting to the database with CodeIgniter.
In the model, if I do
$this->load->database();
then a query such as
$query = $this->db->get('articles', 10);
works and returns data. However, when I remove the load->database line and try autoloading the database library, using
$autoload['libraries'] = array('database');
in application/config/autoload.php, the above query fails. As it works when I explicitly load the library, it's not a problem with my database connection. I'm using CodeIgniter 2.0.2 (the current latest release).
As most of my pages use the database I'd like to autoload it to avoid having to load it anually in each model. Have I misunderstood the documentation regarding loading the database library or am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我知道我可能只是在回答一个死问题,但我也遇到了同样的问题。
如果您使用 eclipse pdt 或类似的 IDE,并通过在构造函数之前添加一些变量来修改 system>core>model.php 以获得代码完成,那么自动加载不起作用,
我不知道为什么,但事实是它不起作用。我将 core>model.php 恢复为原始状态并且自动加载工作正常。我现在无法自动加载,这对于 CI 的新编码员来说确实是一次糟糕的体验。
如果您有解决方法,请发表评论。
I know I am probably just answering a dead question but i had the same problem.
if you are using eclipse pdt or similar IDE and modified the system>core>model.php by adding some variables just before the constructor to get code completion then the autoload doesnt work
i dont know why but the fact is it doesnt work. I restored the core>model.php to original and autoload works fine. I dont get autoload now and its really a bad experience for new coder to CI.
If you have a workaround please comment so.
如果您仍然无法解决问题,这只是一个替代方案。只需扩展 CI_Model 并在构造函数中自动加载数据库即可。然后你可以让所有其他模型继承该基类。
示例:
所有其他方法都会像这样开始:
此方法还有另一个好处,即您不必在创建的每个模型中包含加载代码行。您还可以轻松地将模型之间的共享功能推送到父 MY_Model 类中。
希望有帮助!
This is just an alternative if you still can't resolve the issue. Just extend the CI_Model and auto load the database library in the constructor. Then you can just make all other models inherit from that base class.
Example:
And the all others will start like :
This method has the other benefit that you don't have to include the loading code line in every model you create. Also you can easily push shared functionalities among models into the parent MY_Model class.
Hope it helps!
这是我的 application/config/ 目录中的 database.php
检查您的数据库看起来像这样,然后重新运行 http://codeigniter.com/user_guide/database/configuration.html 并重新阅读 config 目录中 autoload.php 文件中的注释。
我的相关行内容如下:
$autoload['libraries'] = array('database', 'form_validation', 'session');
您应该像这样加载数据库库:
$this->load->library('database');
This is my database.php from my application/config/ directory
Check yours looks like this and rerun through the user guide at http://codeigniter.com/user_guide/database/configuration.html and reread the comments in the autoload.php file in the config directory.
The relevant line in mine reads:
$autoload['libraries'] = array('database', 'form_validation', 'session');
You should be loading the db library like this:
$this->load->library('database');
我唯一能想到的是在您的 application/config/database.php 文件中,以下行设置为 false,而不是 true。
其他一切看起来都不错。
The only thing I can think of is in your
application/config/database.php
file, the following line is set to false, instead of true.Everything else looks ok.
Shababhsiduque,
只有你是对的,至少就我而言!
让 ide 正常工作的解决方法是在另一个 CI 项目中创建引用变量。
即保留当前项目中的系统模型,但在另一个项目中对其进行修改。
然后将 php buildpath (project->properties->phpbuildpath) 更改为带有变量的项目。
请注意,这是 Aptana Studio 3 的设置。
对我有用。
Shababhsiduque,
Just you we're right, at least in my case!
The workaround to getting the ide to work is creating the reference variables in ANOTHER CI project.
That is leave the system model as it is in the current project but modify it in another project.
Then change the php buildpath (project->properties->phpbuildpath) to the project with the variables.
Note this are settings for Aptana Studio 3.
Worked for me.
在这种情况下,您可以尝试 PHP Autoload Class Magic 功能。
必须将此代码复制到 Application 文件夹内的 config.php 内(最后一行之后)。
该代码将在需要时自动为您加载库。
它可能会起作用。快乐编码。
You could try PHP Autoload Class Magic function in this case.
This code must copied inside the config.php (after the last line) within Application folder.
This code will automatically load libraries for you when its needed.
It might work. Happy coding.
就我而言,问题是因为同一文件中再次出现
$autoload['libraries']
,并且它重置了数组。In my case the problem was because there was another occurrence of
$autoload['libraries']
inside the same file and it reset the array.