We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Fatal error: Class 'mongoid' not found in

PHP 7, Mongo 3, Phalcon 3 incubator library 3.4 from https://github.com/phalcon/incubator/releases

PROBLEM: trying to connect to mongo. In controller when i run ->save() i get fatal error ... Fatal error: Class 'mongoid' not found in

CODE:

            # bootstrap
            $mongo = new MongoClient($dburl);
            return $mongo->selectDatabase($dbname);
            # controller...
            $user = new \MyApp\Models\Users();
            $user->email    = $this->request->getPost('email', 'striptags');
            $user->name = $this->request->getPost('name', 'striptags');
            $user->password = $this->security->hash($this->request->getPost('password'));
            $user->save();

Any ideas on how to fix?



2.3k
Accepted
answer
edited Sep '18

This helped...

instead of...

use Phalcon\Mvc\Collection;

do...

use Phalcon\Mvc\MongoCollection;
class Somedata extends MongoCollection{
public function initialize()
{
    // set actual collection name
    $this->setSource('some_data');
}
}

I also had to add the db name to the end of the connection string or i got authentication failure...

$di->setShared('mongo', function () {
        $mongo = new MongoClient('mongodb://mongousername:[email protected]:12345/mongodbname');
        return $mongo->selectDatabase('mongodbname');
    });