0

I'm new to Yii2.

I want to use MongoDB with Yii2-advanced. For that, I have found this library:

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

The issue is it doesn't explain much about the installation and configurations.

As the read me says I have added following into the common\config\main-local.php file:

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

But what should I do with the default db key which is there for of the MySQL connection:

'db' => [
  'class' => 'yii\db\Connection',
  'dsn' => 'mysql:host=localhost;dbname=yii2_advanced_mongo',
  'username' => 'root',
  'password' => 'root',
  'charset' => 'utf8',
],

If I comment out that db key and try to login, then it gives me following error:

Invalid Configuration – yii\base\InvalidConfigException

Unknown component ID: db

Community
  • 1
  • 1
Parth Vora
  • 4,073
  • 7
  • 36
  • 59

1 Answers1

0

I think you should add the entry properly, if you want use the entry db for accessing your db datas you should comment the db entry related to mysql and assign db to your mongodb entry

'db' => [
  'class' => '\yii\mongodb\Connection',
  'dsn' => 'mongodb://your_user_name:your_password@localhost:27017/your_database',
],

//'db' => [
//   'class' => 'yii\db\Connection',
//   'dsn' => 'mysql:host=localhost;dbname=yii2_advanced_mongo',
//   'username' => 'root',
//   'password' => 'root',
//   'charset' => 'utf8',
//],
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • I tried that too. It gives Unknown Method – yii\base\UnknownMethodException Calling unknown method: yii\mongodb\QueryBuilder::build() – Parth Vora Aug 11 '17 at 12:40
  • 1 ) have you installe the mongo db components properly .. ? 2 ) hare you sure that the queryBuilder work correcly with mongoDB ? looking to Yii2 doc i don't see any http://www.yiiframework.com/doc-2.0/yii-mongodb-querybuilder.html ...build() function .. .. and PS : this mean that mongodb access work – ScaisEdge Aug 11 '17 at 12:42
  • Yes installed MongoDB components correctly. No, I'm not as I'm totally new to this and readme doesn't explains much. – Parth Vora Aug 11 '17 at 12:45
  • .. all what we have is the doc.. http://www.yiiframework.com/doc-2.0/yii-mongodb-querybuilder.html ... you can use the function you have in the doc .. – ScaisEdge Aug 11 '17 at 12:46