简体   繁体   中英

yii2 mongodb migration doesn't work

I am using mongodb in yii2 and I want to use it's migration.

this is my model class .

namespace app\models;

use yii\mongodb\ActiveRecord;

class Company extends ActiveRecord
{


    /**
     * @return array
     */
    public static function collectionName()
    {
        return ['cafegardesh','companies'];
    }


    /**
     * @return array
     */
    public function attributes()
    {
        return ['_id', 'name', 'address', 'status'];
    }


    /**
     * @return array
     */
    public function rules()
    {
        return [

            [['name'], 'required'],

        ];
    }

and this is my migration file :

use yii\mongodb\Migration;

class m160904_053937_create_companies_collection extends Migration
{

    public function up()
    {

    }

    public function down()
    {
        echo "m160904_053937_create_companies_collection cannot be reverted.\n";

        return false;
    }

    /*
    // Use safeUp/safeDown to run migration code within a transaction
    public function safeUp()
    {
    }

    public function safeDown()
    {
    }
    */
}

and this is my console config file :

Yii::setAlias('@tests', dirname(__DIR__) . '/tests/codeception');

$params = require(__DIR__ . '/params.php');
$db = require(__DIR__ . '/db.php');

$config = [
    'id' => 'basic-console',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'app\commands',
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => $db,

        'mongodb' => [
            'class' => \yii\mongodb\Connection::class,
            'dsn' => 'mongodb://cafegardesh:12345@localhost:27017/cafegardesh',
        ]
    ],

    'modules' => [
        'taxonomy-term'=>[
            'class'=> \mhndev\yii2TaxonomyTerm\Module::class
        ]
    ],


    'params' => $params,
    /*
    'controllerMap' => [
        'fixture' => [ // Fixture generation command line.
            'class' => 'yii\faker\FixtureController',
        ],
    ],
    */
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
    ];
}

return $config;

and this is the command I execute :

php yii migrate/up --db=mongodb

and I get following error:

Exception 'yii\base\InvalidConfigException' with message '"mongodb" refers to a yii\mongodb\Connection component. yii\db\Connection is expected.'

in /home/majid/Projects/Gardesh-Tour-Backend/vendor/yiisoft/yii2/di/Instance.php:135

MongoDB component has got it's own migration controller. Use it instead.

In your console configuration add

'controllerMap' => [
    'mongodb-migrate' => 'yii\mongodb\console\controllers\MigrateController'
],

Now you can use php yii mongodb-migrate/up .

It takes mongodb component for db as default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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