日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

YII2操作mongodb笔记(转)

發(fā)布時間:2023/12/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 YII2操作mongodb笔记(转) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

componets配置:

'mongodb' => ['class' => '\yii\mongodb\Connection','dsn' => 'mongodb://test:123456@127.0.0.1:27017/yiimongodb', ],

  

?

控制器:

<?php namespace frontend\controllers; use Yii; use yii\helpers\Url; use yii\mongodb\Query; use yii\web\Controller; use yii\data\ActiveDataProvider; use frontend\models\Customer; class MonController extends Controller {public function actionIndex(){$collection = Yii::$app->mongodb->getCollection ( 'customer' );$res = $collection->insert ( [ 'name' => 'John Smith22','status' => 2 ] );var_dump($res);}public function actionList(){$query = new Query ();$query->select ( [ 'name','status' ] )->from ( 'customer' )->offset ( 10 )->limit ( 10 );$rows = $query->all ();var_dump ( $rows );}public function actionView(){$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'] );}public function actionFind(){$provider = new ActiveDataProvider ( [ 'query' => Customer::find (),'pagination' => [ 'pageSize' => 10 ] ] );$models = $provider->getModels ();var_dump ( $models );}public function actionQuery(){$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 );} }

  

?

模型:

<?php namespace frontend\models; use yii\mongodb\ActiveRecord; class Customer extends ActiveRecord {public static function collectionName(){return 'customer';}public function saveInfo(){$customer = new Customer ();$customer->name = '111';$customer->email = '222';$customer->insert ();return $customer;}public function attributes(){return [ '_id','name','email','address','status' ];} }

  

?

YII2的mongodb拓展下載:

https://github.com/yiisoft/yii2-mongodb

?

中文網(wǎng)址:

http://www.runoob.com/

http://www.mongoing.com/

http://www.cnblogs.com/libingql/archive/2011/06/09/2076440.html

常用命令 ,同mysql,eg:

db.createUser({"user":"test","pwd":"123456","roles":["readWrite","dbAdmin"]})show users;show dbs;db.version();db.stats();use yiimongodb;show collections;

  

?

總結(jié)

以上是生活随笔為你收集整理的YII2操作mongodb笔记(转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。