Cakephp and mongodb integration tutorial. If you do not have a mongodb testing environment, you can install a mongodb service locally. here is a graphic installation tutorial for mongodb in windows. By default, the mongodb extension is not enabled in php. you need to manually install a mongodb service locally if you do not have a mongodb test environment on the official website. here is a graphic installation tutorial for mongodb in windows.
By default, the mongodb extension is not enabled in php. you need to manually download the mongo extension from the official website. here, find the mongo extension suitable for your system and decompress it to the ext directory specified in the php environment, at the same time. add to ini file
extension=php_mongo.dll
Restart apache and other servers.
Download the cakephp and mongodb datasouce from github and install it in the app/plugins/directory.
PS: Do not worry about git. you can download it directly.
After the download is complete, you can configure mongodb in database. php:
Var $ mongo = array (
'Datasource '=> 'mongodb. mongodbsource ',
'Database' => 'testmongo ',
'Host' => 'localhost ',
'Port' => 27017
);
You can create a model and use it in the controller:
// Mongb. php
Class Mondb extends AppModel {
Var $ name = 'mongodb ';
Var $ primaryKey = '_ id ';
Var $ useDbConfig = 'mongo ';
Function schema (){
$ This-> _ schema = array (
'_ Id' => array ('type' => 'INTEGER', 'primary' => true, 'length' => 40 ),
'A' => array ('type' => 'string '),
'B' => array ('type' => 'integer '),
);
Return $ this-> _ schema;
}
}
?>
Then you can operate mongodb in the controller:
Function mongo (){
$ This-> loadModel ('mongodb ');
$ Res = $ this-> Mondb-> save (array ("a" = "test mongodb", "B" => time ()));
$ Res = $ this-> Mondb-> find ('all ');
Pr ($ res );
Exit;
}
Bytes. By default, the mongodb extension is not enabled for php. you need to manually go to the official website...