Cakephp and mongodb integration tutorial _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.