Connect yii2 with mongodb

2019-06-22 14:21发布

I am new to Yii2. Can anyone tell me how to configure YII2 with mongodb and how to establish connection between YII2 and mongodb? I have tried to download the mongodb package from git hub and tried to run the following command

php composer.phar require --prefer-dist yiisoft/yii2-mongodb "*"

In the command prompt inside the root folder where I have installed Yii2 but I get the following error

 Your requirements could not be resolved to an installable set of packages.
 Problem 1
- yiisoft/yii2 2.0.0 requires bower-asset/jquery 2.1.*@stable | 1.11.*@stable -> no matching      package found.
- yiisoft/yii2 2.0.0 requires bower-asset/jquery 2.1.*@stable | 1.11.*@stable -> no matching package found.
- Installation request for yiisoft/yii2 == 2.0.0.0 -> satisfiable by yiisoft/yii2[2.0.0].

4条回答
干净又极端
2楼-- · 2019-06-22 14:47
'mongodb' => [
        'class' => '\yii\mongodb\Connection',
        'dsn' => 'mongodb://127.0.0.1:27017/vinagex',
        'options' => [
            "username" => "vinagex",
            "password" => "vinagex"
        ]
    ],
查看更多
贪生不怕死
3楼-- · 2019-06-22 14:48

Seems like problem was with yii2 composer dependencies, Kindly RUN this command in your console to add global dependencies,

1) composer global require "fxp/composer-asset-plugin:~1.1.1"

2) Add "yiisoft/yii2-mongodb": "~2.0.0" in your composer.json file

3) Now run composer install or composer update

4) Composer install will only install require packages in your composer file but composer update will also check if there is any new release for your packages that you mentioned in composer.js and then install the new release.

5) now add below code in your common/config/main.php file

return [
    //....
    'components' => [
        'mongodb' => [
            'class' => '\yii\mongodb\Connection',
            'dsn' => 'mongodb://developer:password@localhost:27017/mydatabase',
        ],
    ],
]; 

Now is the time to test either mongoDb working or not.

$collection = Yii::$app->mongodb->getCollection('customer');
$collection->insert(['name' => 'John Smith', 'status' => 1]);

For further information Please follow below link,

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

查看更多
相关推荐>>
4楼-- · 2019-06-22 14:52

If you are trying to install it through command prompt then try the following command which using composer

composer require --prefer-dist yiisoft/yii2-mongodb "*"

This works in my windows 8 environment.

To ignore dependency errors while installing the package use --ignore-platform-refs switch:

composer require --ignore-platform-refs --prefer-dist yiisoft/yii2-mongodb "*"
查看更多
再贱就再见
5楼-- · 2019-06-22 15:00

Remember that you must also have the MongoDB extension installed in PHP for this plugin to work:

http://php.net/manual/en/class.mongodb.php

查看更多
登录 后发表回答