【问题标题】:MongoDB and CodeIgniter [closed]MongoDB 和 CodeIgniter [关闭]
【发布时间】:2011-01-15 23:13:52
【问题描述】:

谁能帮我指出一个教程、库等,让我可以从 CodeIgniter 使用 MongoDB?

【问题讨论】:

标签: php codeigniter mongodb mongodb-php


【解决方案1】:

我不确定它是否是“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);
    }
}

【讨论】:

  • 非常感谢。很好的开始。
  • 斯蒂芬 模特呢?有什么需要特别注意的地方吗?
  • 我也能够让模型功能正常工作。没什么特别的,仍然从 Model 继承,你的函数只需要调用 Mongo 特定的函数。轻松
  • 没错。您应该可以直接在模型代码中使用 $this->mongo->db。
  • $ci =& get_instance() 应该被使用。来自 CI 文档:这非常重要。通过引用分配允许您使用原始 CodeIgniter 对象,而不是创建它的副本。
【解决方案2】:

MongoDB 在 CodeIgniter 社区中得到了很好的支持,花点时间潜入:p

【讨论】:

  • 谢谢。两年前并没有太多,但我同意现在对 Mongo 的支持要好得多。
【解决方案3】:

我喜欢 Stephen Curran 的示例,因为它很简单,并且允许一个与 Mongo 的接口,而无需在 Php 中编写太多功能,我往往会发现巨大的抽象类有时对于我所追求的东西有点多。

我已经扩展了他的示例以包括数据库身份验证。转到此处:http://www.mongodb.org/display/DOCS/Security+and+Authentication 阅读有关 mongo 身份验证的信息,不要忘记为您正在连接的 Mongo 服务器启用身份验证。

我还将旧式构造函数更改为 __construct 并且正在处理 Mongo 连接异常,因为它们会泄露您的用户名和密码。

config/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'));           
        }
    }
}

【讨论】:

  • 一点 if() 如果没有身份验证凭据会有所帮助。给定;)
  • 可能,但是如果用户想要在没有 --auth 标志的情况下运行 mongo 服务器,那么他们可以使用 Stephen Curran 的原始帖子。或根据需要调整我的。卢克
【解决方案4】:

在 CodeIgniter 中使用 MongoDB 与在其他任何地方使用 MongoDB 没有太大区别。

您可以组合一个 MongoDB 库,该库将在构造函数中连接并存储 $this->conn 以供以后在方法中使用。

然后直接使用控制器中的 conn 属性或在 MongoDB 库中创建一些方法来为您执行此操作。

查看here 了解使用 MongoDB 的普通 PHP 教程。

我很乐意为此为您创建一个库,但它需要付出代价。 :-p

【讨论】:

  • 谢谢 - 我意识到有了 MongoDB 驱动程序和上面的 PHP 教程,我可以让这一切发生。
  • 如果您创建了一个好的库,请与社区分享。我喜欢一个玩 MongoDB 的借口。 :-)
  • @Phil Sturgeon - 看起来 @stephenc 打败了我。
  • 甜,我只希望我有时间自己做一个。 :-) 看起来很酷。
  • 对不起,我太厚了? @stephenc 的图书馆在哪里?
【解决方案5】:

我正在使用带有 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" );

【讨论】:

    猜你喜欢
    • 2020-09-29
    • 1970-01-01
    • 2012-09-14
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    相关资源
    最近更新 更多