An example of the simplest Thinkphp3.2 operation on Mongodb

Source: Internet
Author: User
Tags mongodb example
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn. If you see the Thinkphp website does not call Mongodb, write the simplest Thinkphp operation Mongodb example. Welcome to discussion
[Prerequisite]
Thinkphp's support for Mongdb depends on PHP's support for Mongodb. This is the premise. If the prompt is similar to "the system does not support: mongoClient", it means that your PHP does not support Mongodb.

So what you need to do is
1,Go to renewal
For example, if my PHP is 5.3.13 or win7 64-bit
Change php_mongo-1.3.2RC1-5.3-vc9-x86_64.dll to php_cmd.dll
2,Copy the renamed file to the php ext folder.
3,Modify php. ini and add extension = php_assist.dll.



[Configuration]
Modify the configuration file as follows to connect to the mongodb database: Return array (
// 'Config maps '=> 'configuration value'
'Db _ type' => 'mongo ', // Database TYPE
'Db _ host' => 'localhost', // server address
'Db _ name' => 'local', // Database NAME
'Db _ user' => '', // USER Name
'Db _ pwd' => '', // Password
'Db _ port' => '123', // PORT
);
[Code]
The M method is required for mongodb operations.
Therefore, you must initialize a class. Assume that the database contains a data table user, create a model for the user that inherits from the External Model: Namespace Home \ Model;
Use Think \ Model \ custom Model;
Class UserModel extends Model
{
// It can be empty.
}
Modify IndexController as follows to call the user Collection in the local database of Mongodb. Namespace Home \ Controller;
Use Think \ Controller;
Class IndexController extends Controller {
Public function index (){
$ Model = D ("User ");
$ Model-> create ();
$ Model-> name = 'current year 2 ';
$ Model-> add ();
$ A = $ Model-> select ();
Var_dump ($ );

}
}

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.