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.