Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 10 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
我不确定它是否是“CodeIgniter 方式”,但我创建了一个 CodeIgniter 库,它使用一个额外的属性来扩展 Mongo 类来存储当前的数据库连接。
这是我的项目中的相关代码文件。
config/mongo.php
libraries/Mongo.php
和示例控制器
controllers/posts.php
I'm not sure if its the "CodeIgniter way" but I created a CodeIgniter library that extends the Mongo class with an extra property to store the current database connection.
Here are the relevant code files from my project.
config/mongo.php
libraries/Mongo.php
And a sample controller
controllers/posts.php
MongoDB 在 CodeIgniter 社区中得到了很好的支持,请花点时间深入了解 :p
MongoDB is very well supported within CodeIgniter community, take the time and dive in :p
我喜欢 Stephen Curran 的示例,因为它很简单,并且允许与 Mongo 接口,而无需在 Php 中编写太多功能,我倾向于有时会发现巨大的抽象类对于我所追求的东西来说有点多。
我扩展了他的示例以包括数据库身份验证。转到此处: http://www.mongodb.org/display/DOCS/Security +and+Authentication 要了解 mongo 身份验证,请不要忘记为您要连接的 Mongo 服务器启用身份验证。
我还将旧式构造函数更改为 __construct 并正在处理 Mongo 连接异常,因为它们可以泄露您的用户名和密码。
配置/mongo.php
库/Mongo.php
I like Stephen Curran's example as it is simple and allows an interface to Mongo without too much functionality written within Php, I tend to find huge abstraction clases a bit much at times for what I am after.
I have extended his example to include database authentication. Go here: http://www.mongodb.org/display/DOCS/Security+and+Authentication to read about mongo authentication, don't forget to enable authentication for the Mongo Server you are connecting to.
I have also changed the old style constructor function to be __construct and am handling Mongo Connection Exceptions as they can reveal your username and password.
config/mongo.php
libraries/Mongo.php
在 CodeIgniter 中使用 MongoDB 与在其他地方使用 MongoDB 没有太大区别。
您可以组合一个 MongoDB 库,该库将在构造函数中连接并存储 $this->conn 以便稍后在方法中使用。
然后要么直接使用控制器中的 conn 属性,要么在 MongoDB 库中创建一些方法来为您执行此操作。
请查看此处,了解使用 MongoDB 的简单 PHP 教程。
我很乐意为此创建一个库,但它是有代价的。 :-p
Working with MongoDB in CodeIgniter wouldn't be much different than working with it anywhere else.
You could knock together a MongoDB library that would connect in the constructor and store $this->conn to be used in methods later on.
then either work directly with the conn property in your controllers or create a few methods in your MongoDB library to do this for you.
Take a look here to see the plain PHP tutorial for working with MongoDB.
I'd happily create you a library for this but it would come with a price. :-p
我正在使用带有 CI 的 MongoDB,并提出了以下建议。它对我有用,但我确信它可以进行一些调整。我稍后会担心调整它,但现在它已经达到了我想要的效果。
我创建了一个名为“database_conn.php”的模型,
然后,如果我需要连接到我的模型中的集合。
I'm using MongoDB w/ CI and came up with the following. It works for me, but I'm sure it can be tweaked somewhat. I'll worry about tweaking it later but right now it does what I want.
I created a model called "database_conn.php"
Then, if I need to connect to a collection from my models.