zoukankan      html  css  js  c++  java
  • YII2操作mongodb笔记(转)

    componets配置:

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

      

     

    控制器:

    <?php
    namespace frontendcontrollers;
    use Yii;
    use yiihelpersUrl;
    use yiimongodbQuery;
    use yiiwebController;
    use yiidataActiveDataProvider;
    use frontendmodelsCustomer;
    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 frontendmodels;
    use yiimongodbActiveRecord;
    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

    中文网址:

    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;
    

      

  • 相关阅读:
    execl csv导出
    input里面check 状态检测
    注意这种方法的判断
    本周,上周,本月,上月
    bootstrap 兼容IE8设置
    js jquery 验证写法
    Jquery radio checked
    最简单的XML转数组
    eq,neq,gt,lt等表达式缩写
    python中的注释
  • 原文地址:https://www.cnblogs.com/sandea/p/5651046.html
Copyright © 2011-2022 走看看