YII2 manipulating MongoDB Notes

Source: Internet
Author: User

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)

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.