MongoDB 和 CodeIgniter [关闭]
-
20-09-2019 - |
题
任何人都可以帮助我找到教程、图书馆等吗?这将允许我从 CodeIgniter 使用 MongoDB?
解决方案
我不知道,如果它的“笨方法”,但我创建了一个额外的属性来存储当前数据库连接扩展蒙戈类CodeIgniter的库。
下面是相关的代码文件从我的项目。
<强>配置/ 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;
}
}
和样品控制器
<强>控制器/ 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
我喜欢史蒂芬·柯伦的例子,因为它很简单,并允许蒙戈的接口,而在PHP编写过多的功能,我往往会发现巨大的抽象有时clases位之多是为了什么我后。
我已经扩展他的例子为包括数据库验证。去这里: http://www.mongodb.org/display/DOCS/Security +和+认证阅读有关蒙戈认证,不要忘记启用身份验证您连接到服务器蒙戈。
我也改变为__construct旧式的构造函数和我处理蒙戈连接异常,因为它们可以揭示你的用户名和密码。
配置/ 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 */
库/ 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'));
}
}
}
在笨与MongoDB的工作不会比任何其他地方的工作非常不同。
您可以拼凑一个MongoDB的库,将连接在构造函数和存储$这个 - >连接到的方法来使用以后。
然后直接在你的控制器康恩物业工作或在你的MongoDB库中创建一些方法来为你做这个。
看看这里看到纯PHP教程与合作MongoDB的。
我会高兴地创建你为这个图书馆,但它会提出一个价格。 :-P
我使用的MongoDB瓦特/ CI并用以下想出。它为我,但我敢肯定,这可以在一定程度进行调整。我会担心以后调整,但现在它我想要做什么。
我创建一个名为 “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" );