Using MongoDB extensions in the YII framework

Source: Internet
Author: User
Tags yii

Prerequisites: The MongoDB database is installed

PHP driver with MONGO installed

Download MONGO extension for Yii:

Download Link: http://pan.baidu.com/s/1rPtx0

Official Download Link: https://github.com/canni/YiiMongoDbSuite

Get file: YiiMongoDbSuite.tar.gz

Unpack and place the file under the app's/protected/extensions folder

Make sure the folder name is: Yiimongodbsuite

Configure the App

1 VI/protected/config/main.php2 3 4 5  'Import'=Array (6       ...  7       'ext. Yiimongodbsuite.*',//plug-in root directory files8     ), 9 Ten     ' Components'=Array ( One       ...   A       'MongoDB'=Array ( -         'class'='Emongodb',//Master File -         'connectionString'='mongodb://127.0.0.1:27017',//Server Address the         'DbName'='MyDatabaseName',//Database name -         'Fsyncflag'=true,//MongoDB ensures secure storage of all writes to the database to disk -         'Safeflag'=true,//MongoDB waits to retrieve the status of all write operations and check -         'Usecursor'=false,//set to True to enable cursors +       ),   -),

This is configured, of course, make sure your MongoDB is installed locally, and the port number is 27107 above the configuration to be able to connect.

Test the model below:

Put the following code in the/protected/models/user.php

1 classUserextendsemongodocument2     {3        Public $login;4        Public $name;5        Public $pass;6  7       //This have to being defined in every model, the is same as with the standard Yii ActiveRecord8        Public Static functionModel$className=__class__)9       {Ten         returnParent::model ($className); One       } A   -       //This method is required! -        Public functionGetcollectionname () the       { -         return' Users '; -       } -   +        Public functionrules () -       { +         return Array( A           Array(' Login, pass ', ' required '), at           Array(' Login, pass ', ' length ', ' max ' = 20), -           Array(' name ', ' length ', ' max ' = 255), -         ); -       } -   -        Public functionattributelabels () in       { -         return Array( to' Login ' = ' User login ', +' Name ' = ' Full name ', -' Pass ' = ' Password ', the         ); *       } $}

Test it in the controller.

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

If there is no error, then the instructions 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.