简体   繁体   English

MongoDB连接在YII2中不起作用

[英]MongoDB connection not working in YII2

I'm new bee to yii2 and php. 我是yii2和php的新手。 I am creating a project using YII2 and mongodb but giving an error {"response":"failed","status_code":500,"message":"\\"db\\" refers to a yii\\mongodb\\Connection component. yii\\db\\Connection is expected."} 我正在使用YII2和mongodb创建一个项目,但给出了一个错误{“” response“:” failed“,” status_code“:500,” message“:” \\“ db \\”指向yii \\ mongodb \\ Connection组件。yii \\ db \\ Connection是预期的。“}

my component file is 我的组件文件是

 return [
    'db' => require (__DIR__ . '/_mongodb.php'),
    'urlManager' => require (__DIR__ . '/_urlRules.php'),
    'request' => require (__DIR__ . '/_request.php'),   
    'log' => require (__DIR__ . '/_log.php'),
    'response' => require (__DIR__ . '/_response.php'),
    'user' => require (__DIR__ . '/_user.php'),
    'cache' => require (__DIR__ . '/_cache.php'),
    'mailer' => require (__DIR__ . '/_mail.php'),
    'languageSelector' => require (__DIR__ . '/_languageSelector.php'),
    'i18n' => require (__DIR__ . '/_i18n.php'),    
    'assetManager' => [
        'class' => 'app\components\asset\AssetManager',
        'resource' => [
            'path' => 'resources' . DIRECTORY_SEPARATOR,
            'tempPath' => 'temp' . DIRECTORY_SEPARATOR,
            'thumbnailPath' => 'thumbs' . DIRECTORY_SEPARATOR
        ]
    ],
    'storageFactory' => [
        'class' => 'app\components\storage\StorageFactory',
        'local' => [
            'class' => 'app\components\storage\FileStorage',
            'connector' => require (__DIR__ . '/_localStorage.php')
        ],
        'network' => [
            'class' => 'app\components\storage\FileStorage',
            'connector' => require (__DIR__ . '/_localStorage.php')
        ]
    ]
]?>

_mongodb.php file _mongodb.php文件

return [
    'class' => 'yii\mongodb\Connection',
    'dsn' => 'mongodb://localhost:27017/genie'
];

i tried in google nothing found 我在谷歌尝试没有找到

Several classes of the Yii framework use the db component and expect it to be of type yii\\db\\Connection , this is why you get the error. Yii框架的几个类都使用db组件,并且期望它的类型为yii\\db\\Connection ,这就是为什么会出现错误的原因。

Make sure to check the docs on the MongoDb Connection class , here you can see how to properly initialize the mongodb component. 确保检查MongoDb Connection类上文档 ,在这里您可以看到如何正确初始化mongodb组件。 Configure a new property like this: mongodb => require(__DIR__ . '/mongodb.php') 像这样配置一个新属性: mongodb => require(__DIR__ . '/mongodb.php')

Now you can use it by referencing to it with Yii::$app->mongodb 现在您可以通过使用Yii::$app->mongodb引用它来使用它

Furthermore you just leave out the db component if you don't need to work with a relational database. 此外,如果不需要使用关系数据库,则只需删除db组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM