Back to Blog listOriginal YII2 Operation MongoDB notesAir-Shenzhen
- Release time: 2015/12/17 15:20
- READ: 895
- Favorites: 2
- Likes: 0
- Comments: 0
Ensure that you have installed the Mongodb,windows installation can refer to another blog post before operation:
http://my.oschina.net/chinahub/blog/479268
Componets Configuration:
‘mongodb‘ => [ ‘class‘ => ‘\yii\mongodb\Connection‘, ‘dsn‘ => ‘mongodb://test:[email protected]:27017/yiimongodb‘,],
Controller:
<?phpNamespaceFrontend\ControllersUseYii;UseYii\Helpers\URL;UseYii\Mongodb\Query;UseYii\Web\Controller;UseYii\Data\Activedataprovider;UseFrontend\Models\Customer;ClassMoncontrollerExtendscontroller{PublicfunctionActionindex() {$collection = Yii::$app->mongodb->getcollection (' Customer ');$res =$collection->insert ([' Name ' + =' John Smith22 ',' Status ' =2]); Var_dump ($res); }PublicfunctionActionList() {$query =New Query ();$query->select ([' Name ',' Status '])->from (' Customer ')->offset (Ten)->limit (10);$rows =$query->all (); Var_dump ($rows); }PublicfunctionActionview() {$query =New Query ();$row =$query->from (' Customer ')->one ();Echo Url::toroute ([' Item/update ',' id ' = = (string)$row [' _id ']); Var_dump ($row [' _id ']); Var_dump (String)$row [' _id ']); }PublicfunctionActionfind() {$provider =New Activedataprovider ([' Query ' = Customer::find (),' Pagination ' = [' PageSize ' =10]]);$models =$provider->getmodels (); Var_dump ($models); }PublicfunctionActionquery() {$query =New Query ();$query->from (' Customer ')->where ([' Status ' =2]);$provider =New Activedataprovider ([' Query ' =$query,' Pagination ' = [' PageSize ' =10 ] ] ); $models = $provider->getmodels (); var_dump ( $models ); } public function actionSave () { $res = Customer::saveInfo (); var _dump ( $res ); }}
Model:
<?phpNamespaceFrontend\ModelsUseYii\Mongodb\ActiveRecord;ClassCustomerExtendsactiverecord{PublicStaticfunctionCollectionName() {Return' Customer '; }PublicfunctionSaveinfo() {$customer =New Customer ();$customer->name =' 111 ';$customer->email =' 222 ';$customer->insert ();return $customer; } public function attributes () { return [ ' _id ', ' name ', ' email ', ' address ',
YII2 's MongoDB expansion download:
Https://github.com/yiisoft/yii2-mongodb
Chinese website:
http://www.runoob.com/
http://www.mongoing.com/
Http://www.cnblogs.com/libingql/archive/2011/06/09/2076440.html
Common commands, with Mysql,eg:
Db.createuser ({"User": "Test", "pwd": "123456", "Roles": ["ReadWrite", "DbAdmin"]})
Show Users;
Show DBS;
Db.version ();
Db.stats ();
Use Yiimongodb;
Show collections;
YII2 manipulating MongoDB notes (RPM)