Maff
Maff

Reputation: 1032

Using MongoDb in the PHP Phalcon framework

I am currently experimenting with the Phalcon Framework, and running into some complications when I attempt to save content into the Mongo Database. I can correctly setup the MySQL database without issues. Whenever I send the simple request through I get a 500 Internal server error (checking devTools). I have setup everything accordingly as the documentation specifies. This is my simple index.php bootstrap Mongo initialisation along with the collection manager:

// Setting Mongo Connection
$di->set('mongo', function() {
    $mongo = new Mongo();
    return $mongo->selectDb("phalcon");
}, true);

// Setting up the collection Manager
$di->set('collectionManager', function(){
    return new Phalcon\Mvc\Collection\Manager();
}, true);

This is my controller handling the request:

public function createAction() {
    $user = new User();

    $user->firstname = "Test ACC";
    $user->lastname = "tester";
    $user->password = "password";
    $user->email = "[email protected]";

    if($user->create() == false) {
        echo 'Failed to insert into the database' . "\n";
        foreach($user->getMessages as $message) {
            echo $message . "\n";
        }
    } else {
        echo 'Happy Days, it worked';
    }

}

And finally my simple User class:

class User extends \Phalcon\Mvc\Collection {

public $firstname;
public $lastname;
public $email;
public $password;
public $created_at = date('Y-m-d H:i:s');

}

Much appreciated for everyones input/suggestions.

Upvotes: 1

Views: 7176

Answers (2)

Hassan
Hassan

Reputation: 626

A little bit late, but for anyone else facing this issue, it would be a good idea to try and connect to mongo (run "mongo" in your terminal) to ensure that mongo is setup correctly in your dev environment.

Also, I usually find in this sort of situation, that adding a collection to a database in mongo and then testing the CRUD process with a simple read helps move things along. If all is well at this stage, then you know your app is able to connect and you can proceed to writes, and so on.

This looks useful.

Upvotes: 0

Lukas Liesis
Lukas Liesis

Reputation: 26413

i think it's because your installation of Mongo is not valid.

try printing phpinfo() and check if mongo is loaded at all, if not - install it, add to ini files (if you use cli, don't forget to add to cli ini too) and reach the moment, when mongo is fully loaded.

try mongo w/o phalcon. any simple connection/insertation. you can see here: Fatal Error - 'Mongo' class not found that there are problems with apache module version for some people. Try reinstalling different mongo version.

if you can print this out:

echo Phalcon\Version::get(); 

there should be no problems with phalcon instalation

to validate mongo installation, try any of examples from php.net:

http://www.php.net/manual/en/mongo.tutorial.php

Upvotes: 1

Related Questions