【发布时间】:2011-01-15 23:13:52
【问题描述】:
谁能帮我指出一个教程、库等,让我可以从 CodeIgniter 使用 MongoDB?
【问题讨论】:
-
我有一个疑问。 Mongodb 查询语法已经很简单了。无需编写复杂的 SQL。为什么不在配置中禁用“ActiveRecord”类并使用默认的 mongo 驱动程序编写自己的查询?
标签: php codeigniter mongodb mongodb-php
谁能帮我指出一个教程、库等,让我可以从 CodeIgniter 使用 MongoDB?
【问题讨论】:
标签: php codeigniter mongodb mongodb-php
我不确定它是否是“CodeIgniter 方式”,但我创建了一个 CodeIgniter 库,它使用额外的属性扩展了 Mongo 类来存储当前的数据库连接。
这是我项目中的相关代码文件。
config/mongo.php
$config['mongo_server'] = null;
$config['mongo_dbname'] = 'mydb';
库/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;
}
}
还有一个示例控制器
controllers/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);
}
}
【讨论】:
MongoDB 在 CodeIgniter 社区中得到了很好的支持,花点时间潜入:p
【讨论】:
我喜欢 Stephen Curran 的示例,因为它很简单,并且允许一个与 Mongo 的接口,而无需在 Php 中编写太多功能,我往往会发现巨大的抽象类有时对于我所追求的东西有点多。
我已经扩展了他的示例以包括数据库身份验证。转到此处:http://www.mongodb.org/display/DOCS/Security+and+Authentication 阅读有关 mongo 身份验证的信息,不要忘记为您正在连接的 Mongo 服务器启用身份验证。
我还将旧式构造函数更改为 __construct 并且正在处理 Mongo 连接异常,因为它们会泄露您的用户名和密码。
<?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 */
<?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'));
}
}
}
【讨论】:
在 CodeIgniter 中使用 MongoDB 与在其他任何地方使用 MongoDB 没有太大区别。
您可以组合一个 MongoDB 库,该库将在构造函数中连接并存储 $this->conn 以供以后在方法中使用。
然后直接使用控制器中的 conn 属性或在 MongoDB 库中创建一些方法来为您执行此操作。
查看here 了解使用 MongoDB 的普通 PHP 教程。
我很乐意为此为您创建一个库,但它需要付出代价。 :-p
【讨论】:
我正在使用带有 CI 的 MongoDB,并提出了以下建议。它对我有用,但我确信它可以进行一些调整。我会担心稍后对其进行调整,但现在它可以满足我的需求。
我创建了一个名为“database_conn.php”的模型
class Database_Conn extends Model {
function _connect() {
$m = new Mongo();
$db = $m->selectDB( "YOUR DATABASE NAME" );
return $db;
}
}
然后,如果我需要从我的模型连接到集合。
$collection = Database_Conn::_connect()->selectCollection( "COLLECTION NAME" );
【讨论】: