在codeigniter mongodb中插入函数的位置 [英] where to insert functions in codeigniter mongodb

查看:49
本文介绍了在codeigniter mongodb中插入函数的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过codeigniter驱动程序将php Web应用程序连接到mongodb。

I have connected my php web app to mongodb with the help of codeigniter driver.

现在我正在使用mongoo_db类将集合插入默认数据库中, db_name。

now i am using the mongoo_db class to insert a collection into the default database, db_name.

我文件的源代码为

ghy.php

<?php
/**
* @author
* @copyright 2014
*/
class ghy
{
    public $id;
    public $name;
    public function insert($collection = "", $data = array()) {
        if(empty($collection))
            show_error("No Mongo collection selected to insert into", 500);
        if(count($data) == 0 || !is_array($data))
            show_error("Nothing to insert into Mongo collection or insert is not an array", 500);
        try {
            $this->db->{$collection}->insert($insert, array('safe' => TRUE));
            if(isset($insert['_id']))
                return($insert['_id']);
            else
                return(FALSE);
        } catch(MongoCursorException $e) {
            show_error("Insert of data into MongoDB failed: {$e->getMessage()}", 500);
        }
        $use=$this->mongo_db->insert('foo', $data = array('4','hgfh'));
        var_dump();
        }
    }
?>

问题是我的浏览器未获得任何输出。

the problem is that i am not getting any output in my browser.

有人可以向我解释我要去哪里错吗?

can anyone plss explain me where am i going wrong? replies at the the earliest will be highly appreciated.

谢谢

推荐答案

我不确定为什么您的班级无法正常工作,但是要了解MongoDB和Codeigniter的协同工作原理,请查看以下 answer

I'm not sure why your class isn't working, but to get an understanding of how MongoDB and Codeigniter works together take a look at this answer.

answer 回答有关如何建立与MongoDB的连接的问题:

From the answer to answer your question on how to establish a connection to MongoDB:

config / mongo.php

$config['mongo_server'] = null;
$config['mongo_dbname'] = 'mydb';

libraries / 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

MongoDB is very well supported within CodeIgniter community, take the time and dive in :p

  • CodeIgniter MongoDB Active Document Library
  • CodeIgniter MongoDB Session Library
  • CodeIgniter MongoDB Authentication Library
  • CodeIgniter MongoDB REST Server Library
  • CodeIgniter MongoDB Base Model

我希望这可以帮助您获得Codeigniter(MongoDB )项目工作

I hope this helps you to get your Codeigniter(MongoDB) project working

这篇关于在codeigniter mongodb中插入函数的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆