Tutorial on operating mongodb database instances in the yii2 framework

Source: Internet
Author: User
Tags mongodb port number yii

I. How about installing yii2 ?? Aluminum player? Why ?? Install and download omposer. We can install basic

Template

Composer global require "fxp/composer-asset-plugin :~ 1.0.3"
Composer create-project -- prefer-dist yiisoft/yii2-app-basic basic

Or advanced

Composer global require "fxp/composer-asset-plugin :~ 1.0.3"
Composer create-project -- prefer-dist yiisoft/yii2-app-advanced yii-application

Enter the github key during installation and follow the prompts.

II. yii2 soft comes with mongodb and installs the append composer.

Composer require -- prefer-dist yiisoft/yii2-mongodb

/Vendor/yiisoft/yii2-mongodb introduced mongodb

3. Use crud

Config/web. php

'Mongodb '=> [
'Class' => '\ yii \ mongodb \ connection ',
'Dsn '=> 'mongodb: // tank: test@192.168.33.10: 27017/admin ',
],

Use yii \ mongodb \ Query;
 
Public function actionIndex ()
{
 
$ Query = new Query;
// Compose the query
$ Query-> select (['_ id', 'sex'])
-> From ('admin ')
-> Limit (10 );
// Execute the query
$ Rows = $ query-> all ();
 
Print_r ($ rows );
 
// Return $ this-> render ('index ');
}



Use mongodb extension in Yii Framework

Prerequisites: The mongodb database is installed.

Install the php driver of mongo

Download the mongo extension of Yii:

This is version 1.3.6 of YiiMongoDbSuite.
PHP Mongo driver version 1.0.5 or earlier

Download link: http://pan.baidu.com/s/1jGuWP1O

Other versions Download link: https://github.com/canni/YiiMongoDbSuite

File: YiiMongoDbSuite.tar.gz

Decompress the package and place the file under the/protected/extensions folder of the application.

Make sure that the folder name is YiiMongoDbSuite.

Configure application

Vi/protected/config/main. php



'Import' => array (
...
'Ext. YiiMongoDbSuite. * ', // plug-in root directory file
),

'Components' => array (
...
'Mongodb '=> array (
'Class' => 'ongodb', // master file
'Connectionstring' => 'mongodb: // 127.0.0.1: 8080', // server address
'Dbname' => 'mydatabasename ', // database name
'Fsyncflag' => true, // mongodb ensures that all data written to the database is stored securely to the disk.
'Safeflag' => true, // The status of all write operations that mongodb is waiting to retrieve, and check
'Usecursor '=> false, // if it is set to true, the cursor is enabled.
),
),

In this way, the configuration is complete. Of course, make sure that your mongodb is installed locally and the port number is configured on port 27107 before it can be connected.

Test the Model below:

Put the following code in/protected/models/User. php:

Class User extends EMongoDocument
{
Public $ login;
Public $ name;
Public $ pass;

// This has to be defined in every model, this is same as with standard Yii ActiveRecord
Public static function model ($ className =__ CLASS __)
  {
Return parent: model ($ className );
  }

// This method is required!
Public function getCollectionName ()
  {
Return 'users ';
  }

Public function rules ()
  {
Return array (
Array ('login, pass', 'required '),
Array ('login, pass', 'length', 'Max' => 20 ),
Array ('name', 'length', 'Max' => 255 ),
);
  }

Public function attributeLabels ()
  {
Return array (
'Login' => 'User login ',
'Name' => 'Full name ',
'Pass' => 'password ',
);
  }
}

Test it in the controller.

$ Users = User: model ()-> findAll ();
Var_dump ($ users );

If no error is reported, it indicates that it can be used normally.

Related Article

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.