php MongoDB 和 CodeIgniter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2248789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MongoDB and CodeIgniter
提问by IEnumerator
Can anyone assist in pointing me to a tutorial, library, etc. that will allow me to work with MongoDB from CodeIgniter?
任何人都可以帮助我指向一个教程、库等,让我可以从 CodeIgniter 使用 MongoDB 吗?
采纳答案by Stephen Curran
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.
我不确定它是否是“CodeIgniter 方式”,但我创建了一个 CodeIgniter 库,它使用额外的属性扩展 Mongo 类来存储当前数据库连接。
Here are the relevant code files from my project.
这是我项目中的相关代码文件。
config/mongo.php
配置/mongo.php
$config['mongo_server'] = null;
$config['mongo_dbname'] = 'mydb';
libraries/Mongo.php
图书馆/ Mongo.php
class CI_Mongo extends Mongo
{
var $db;
function CI_Mongo()
{
// Fetch CodeIgniter instance
$ci = get_instance();
// Load Mongo configuration file
$ci->load->config('mongo');
// Fetch Mongo server and database configuration
$server = $ci->config->item('mongo_server');
$dbname = $ci->config->item('mongo_dbname');
// Initialise Mongo
if ($server)
{
parent::__construct($server);
}
else
{
parent::__construct();
}
$this->db = $this->$dbname;
}
}
And a sample controller
和一个示例控制器
controllers/posts.php
控制器/posts.php
class Posts extends Controller
{
function Posts()
{
parent::Controller();
}
function index()
{
$posts = $this->mongo->db->posts->find();
foreach ($posts as $id => $post)
{
var_dump($id);
var_dump($post);
}
}
function create()
{
$post = array('title' => 'Test post');
$this->mongo->db->posts->insert($post);
var_dump($post);
}
}
回答by sepehr
MongoDB is very well supported within CodeIgniter community, take the time and dive in :p
MongoDB 在 CodeIgniter 社区中得到了很好的支持,花点时间深入了解 :p
回答by Luke Tarplin
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.
我喜欢 Stephen Curran 的例子,因为它很简单,并且允许一个接口到 Mongo,而无需在 Php 中编写太多功能,我往往会为我所追求的东西找到大量的抽象类。
I have extended his example to include database authentication. Go here: http://www.mongodb.org/display/DOCS/Security+and+Authenticationto read about mongo authentication, don't forget to enable authentication for the Mongo Server you are connecting to.
我扩展了他的示例以包括数据库身份验证。转到这里:http: //www.mongodb.org/display/DOCS/Security+and+Authentication阅读有关 mongo 身份验证的信息,不要忘记为您正在连接的 Mongo 服务器启用身份验证。
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.
我还将旧式构造函数更改为 __construct 并正在处理 Mongo 连接异常,因为它们可以显示您的用户名和密码。
config/mongo.php
配置/mongo.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['mongo_server'] = 'localhost';
$config['mongo_dbname'] = 'my_mongo_db';
$config['mongo_username'] = 'mongo_user';
$config['mongo_password'] = 'password1234';
/* End of file mongo.php */
libraries/Mongo.php
图书馆/ Mongo.php
<?php
class CI_Mongo extends Mongo{
protected $db;
function __construct()
{
// Fetch CodeIgniter instance
$ci = get_instance();
// Load Mongo configuration file
$ci->load->config('mongo');
// Fetch Mongo server and database configuration
$server = $ci->config->item('mongo_server');
$username = $ci->config->item('mongo_username');
$password = $ci->config->item('mongo_password');
$dbname = $ci->config->item('mongo_dbname');
// Initialise Mongo - Authentication required
try{
parent::__construct("mongodb://$username:$password@$server/$dbname");
$this->db = $this->$dbname;
}catch(MongoConnectionException $e){
//Don't show Mongo Exceptions as they can contain authentication info
$_error =& load_class('Exceptions', 'core');
exit($_error->show_error('MongoDB Connection Error', 'A MongoDB error occured while trying to connect to the database!', 'error_db'));
}catch(Exception $e){
$_error =& load_class('Exceptions', 'core');
exit($_error->show_error('MongoDB Error',$e->getMessage(), 'error_db'));
}
}
}
回答by Phil Sturgeon
Working with MongoDB in CodeIgniter wouldn't be much different than working with it anywhere else.
在 CodeIgniter 中使用 MongoDB 与在其他任何地方使用它没有太大区别。
You could knock together a MongoDB library that would connect in the constructor and store $this->conn to be used in methods later on.
您可以组合一个 MongoDB 库,该库将在构造函数中连接并存储 $this->conn 以供稍后在方法中使用。
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.
然后要么直接使用控制器中的 conn 属性,要么在 MongoDB 库中创建一些方法来为您执行此操作。
Take a look hereto see the plain PHP tutorial for working with MongoDB.
看看这里,看看纯PHP教程使用MongoDB的工作。
I'd happily create you a library for this but it would come with a price. :-p
我很乐意为此创建一个库,但它会付出代价。:-p
回答by luckytaxi
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.
我正在使用带有 CI 的 MongoDB 并提出以下内容。它对我有用,但我相信它可以稍微调整一下。我会担心稍后调整它,但现在它可以做我想要的。
I created a model called "database_conn.php"
我创建了一个名为“database_conn.php”的模型
class Database_Conn extends Model {
function _connect() {
$m = new Mongo();
$db = $m->selectDB( "YOUR DATABASE NAME" );
return $db;
}
}
Then, if I need to connect to a collection from my models.
然后,如果我需要从我的模型连接到一个集合。
$collection = Database_Conn::_connect()->selectCollection( "COLLECTION NAME" );

