vaanipala
vaanipala

Reputation: 1291

cakephp mongodb: Notice (1024): connecting to localhost: 27017 failed: unknown error in mongodb_source.php

i'm getting the following error when i run my website:

Notice (1024):connecting to localhost:27017 failed: Unknown error [APP\plugins\Mongodb\models\datasources\mongodb_source.php, line 201]Code | Context        } catch(MongoException $e) {
            $this->error = $e->getMessage();
            trigger_error($this->error);$host   =   "localhost:27017"
$e  =   MongoConnectionException
MongodbSource::connect() - APP\plugins\Mongodb\models\datasources\mongodb_source.php, line 201
MongodbSource::isConnected() - APP\plugins\Mongodb\models\datasources\mongodb_source.php, line 251
MongodbSource::read() - APP\plugins\Mongodb\models\datasources\mongodb_source.php, line 876
Model::find() - CORE\cake\libs\model\model.php, line 2130
MotorDevelopmentsController::search() - APP\controllers\motor_developments_controller.php, line 13
Dispatcher::_invoke() - CORE\cake\dispatcher.php, line 204
Dispatcher::dispatch() - CORE\cake\dispatcher.php, line 171
[main] - APP\webroot\index.php, line 87

Can anyone tell on what i'm doing wrong? Thank you.

I have placed the Mongodb database driver (version 0.4) for cakephp from https://github.com/ichikaway in \merry_flowers\plugins. I renamed it to Mongodb (camelized).

In \merry_flowers\config\database.php

    <?php
     class DATABASE_CONFIG {

var $default = array(
    'driver' => 'mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'root',
    'password' => 'pass',
    'database' => 'merry_flowers_db',
);

public $mongo = array(
    'driver' => 'mongodb.mongodbSource',
    'database' => 'student_db',
    'host' => 'localhost',
    'port' => 27017

);  

  }
      ?>

i'm using a mysql database (for everything) and mongodb (only for reporting purpose) for my website.

in model: motor_development.php

    <?php
class MotorDevelopment extends AppModel {
    var $name ='MotorDevelopment';
    //var $primaryKey ='student_id';
    var $useDbConfig='mongo';
    //var $useTable=false;
    var $mongoSchema=array(
                            'student_id'=>array('type'=>'integer','primary'=>true,'length'=>10),
                            'Draws freehand pictures'=>array('type'=>'string','length'=>1),
                            'Cuts and paste pictures'=>array('type'=>'string','length'=>1)
                            );

}


?>

merry_parents_controller.php

  .....
  $this->redirect(array('controller'=>'motor_developments', 'action'=>'search',$student_info));

controller: motor_developments_controller.php

      <?php
class MotorDevelopmentsController extends appController{
    //public $MotorDevelopment;
    public $name='MotorDevelopments';

    public function search(){
        echo 'SEARCH';
        var_dump($student_info);
        $x=$this->MotorDevelopment->find('one',array('conditions'=>array('MotorDevelopment.student_id'=>12)));
        $this->set('x',$x);
        $this->set(compact('x'));
    }
}
?>

Mongodb database details:

mongodb database name: student_db
collection: motor_developments
the record i have inserted:
         student_id:12
         Draws freehand pictures:B
         Cuts and pastes pictures:B

Upvotes: 1

Views: 2169

Answers (1)

marc_ferna
marc_ferna

Reputation: 5847

I had a lot of difficulties to make it work but I finally did! I created a github repo with a simple template with the latest CakePHP version with the MongoDB datasource working:

https://github.com/marcferna/CakePHP-MongoDB-Template

If you download this template and it doesn't work it will be a problem with the MongoDB server running in your localhost.

Hope you can make it work.

Upvotes: 1

Related Questions