2

I'm building a simple Admin system for an Honours level University project.

Build on Codeigniter with MongoDB using the Alex Bible driver. I'm and trying to carry out a simple insert with hard coded values at the moment.

My Model

$username = "William";
        $email = "Test";
        
        $registerdata = array(
                "username"   => $username,
                "email"      => $email);
        
        $collection = 'students';
        
        $this->mongo_db->insert($collection, $registerdata);

Throwing the following errors:

PHP Error was encountered

Severity: Notice

Message: Undefined variable: insert

Filename: libraries/Mongo_db.php

Line Number: 456

And:

A PHP Error was encountered

Severity: Warning

Message: MongoCollection::insert(): expects parameter 1 to be an array or object, null given

Filename: libraries/Mongo_db.php

Line Number: 456

Any ideas?

It calls this function in the library if that helps any:

     * Insert a new document into the passed collection
     *
     *  @usage = $this->mongo_db->insert('foo', $data = array());
     */
    
     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);
        }
        
     }
3

1 Answer 1

0

Your method signature is wrong - I wonder if you have copied and pasted version 2 of this library without installing the whole thing? The new method is:

public function insert($collection = '', $insert = array(), $options = array())

Reinstall the library in your application and check the library files have been pulled in their entirety.

Sign up to request clarification or add additional context in comments.

2 Comments

Thats exactly the problem, I haven't tried it yet but I know that's the issue, pulled y hair out for hours and couldn't see it for looking at it!! Thank You :)
Great. It's worth regarding libraries as "non-editable" - having them as Git submodules or Composer dependencies helps avoid these library+edits problems :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.