Yii2创建用户表

zy1mlcev  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(239)

我安装了Yii 2与先进的应用程序模板到我的localhost一切都很好,除了命令migrate当我使用cmd来创建用户表时,它不做任何事情.我的代码在main-local:“您可以通过以下方式访问此服务器:
是:

return [
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost:8585;dbname=db001name001',
            'username' => 'db001user001',
            'password' => 'yii2db001',
            'charset' => 'utf8',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@common/mail',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
    ],
];

我确实将localhost:8585更改为127.0.0.1:8585,但仍有相同的问题,我使用的是phpmyadmin,服务器是Uwamp
init命令运行良好,但当我尝试执行时:php yii migrate或yii migrate没有任何事情发生.这是命令

它显示所有内容,数据库中没有创建任何内容
我确实下载了“Yii 2与高级应用程序模板”之后,我确实打开了cmd并运行了php init

I:\zzerver\Uwamp -port 84\uw-cms-p8585\www\yii1>php init
Yii Application Initialization Tool v1.0

Which environment do you want the application to be initialized in?

  [0] Development
  [1] Production

  Your choice [0-1, or "q" to quit] 0

  Initialize the application under 'Development' environment? [yes|no] y

  Start initialization ...

      exist backend/config/main-local.php
            ...overwrite? [Yes|No|All|Quit] All
  overwrite backend/config/main-local.php
  unchanged backend/config/params-local.php
  unchanged backend/config/test-local.php
  unchanged backend/web/index-test.php
  unchanged backend/web/index.php
  unchanged backend/web/robots.txt
  overwrite common/config/main-local.php
  unchanged common/config/params-local.php
  unchanged common/config/test-local.php
  unchanged console/config/main-local.php
  unchanged console/config/params-local.php
  overwrite frontend/config/main-local.php
  unchanged frontend/config/params-local.php
  unchanged frontend/config/test-local.php
  unchanged frontend/web/index-test.php
  unchanged frontend/web/index.php
  unchanged frontend/web/robots.txt
  unchanged yii
  unchanged yii_test
  unchanged yii_test.bat
   generate cookie validation key in backend/config/main-local.php
   generate cookie validation key in frontend/config/main-local.php
      chmod 0777 backend/runtime
      chmod 0777 backend/web/assets
      chmod 0777 frontend/runtime
      chmod 0777 frontend/web/assets
      chmod 0755 yii
      chmod 0755 yii_test

  ... initialization completed.

I:\zzerver\Uwamp -port 84\uw-cms-p8585\www\yii1>

我创建的数据库名称:数据库001名称001,用户:数据库名称001,密码:Yii2DB001,
我将信息添加到common\config\main-local.php中
我再次打开cmd并运行php yii migrate

I:\zzerver\Uwamp -port 84\uw-cms-p8585\www\yii1>php yii migrate
Yii Migration Tool (based on Yii v2.0.13-dev)

但是没有发生任何事情没有为用户创建表,我想为用户创建表以开始创建注册表单
common\config\main-local.php

<?php
return [
        'components' => [
            'db' => [
                'class' => 'yii\db\Connection',
                'dsn' => 'mysql:host=localhost:8585;dbname=db001name001',
                'username' => 'db001user001',
                'password' => 'yii2db001',
                'charset' => 'utf8',
            ],
            'mailer' => [
                'class' => 'yii\swiftmailer\Mailer',
                'viewPath' => '@common/mail',
                // send all mails to a file by default. You have to set
                // 'useFileTransport' to false and configure a transport
                // for the mailer to send real emails.
                'useFileTransport' => true,
            ],
        ],
    ];

php文件中的文件名:

<?php
    return [
        'aliases' => [
            '@bower' => '@vendor/bower-asset',
            '@npm'   => '@vendor/npm-asset',
        ],
        'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
        'components' => [
            'cache' => [
                'class' => 'yii\caching\FileCache',
            ],
        ],
    ];

我的控制台\config\main.php”

<?php
$params = array_merge(
    require __DIR__ . '/../../common/config/params.php',
    require __DIR__ . '/../../common/config/params-local.php',
    require __DIR__ . '/params.php',
    require __DIR__ . '/params-local.php'
);

return [
    'id' => 'app-console',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'console\controllers',
    'aliases' => [
        '@bower' => '@vendor/bower-asset',
        '@npm'   => '@vendor/npm-asset',
    ],
    'controllerMap' => [
        'fixture' => [
            'class' => 'yii\console\controllers\FixtureController',
            'namespace' => 'common\fixtures',
          ],
    ],
    'components' => [
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
    ],
    'params' => $params,
];
njthzxwz

njthzxwz1#

根据您提供的信息,我建议您阅读Yii 2迁移here的相关内容。在您的示例中,您没有向Yii提供任何命令,只是检查了是否安装了迁移工具。完成后/如果您已经完成了,请澄清您的问题--向我们显示您尝试运行了哪些命令,以及错误信息(如果有)。
链接的文档有几个示例,包括创建表的代码,这与您要做的是一样的。

5f0d552i

5f0d552i2#

乍一看,您在console/config/main.php中没有设置数据库组件
尝试添加一个适当的数据库连接,例如mysql

<?php
    $params = array_merge(
        require(__DIR__ . '/../../common/config/params.php'),
        require(__DIR__ . '/../../common/config/params-local.php'),
        require(__DIR__ . '/params.php'),
        require(__DIR__ . '/params-local.php')
    );

    return [
        'id' => 'app-console',
        'basePath' => dirname(__DIR__),
        'bootstrap' => ['log'],
        'controllerNamespace' => 'console\controllers',
        'components' => [
            'log' => [
                'targets' => [
                    [
                        'class' => 'yii\log\FileTarget',
                        'levels' => ['error', 'warning'],
                    ],
                ],
            ],
            'db' => [

                'class' => 'yii\db\Connection',
                'dsn' => 'mysql:host=localhost;dbname=my_db_name',
                'username' => 'my_root',
                'password' => 'my_pwd',
                'charset' => 'utf8',
                'enableSchemaCache' => true, 

            ],

      ],
      'params' => $params,
  ];

然后尝试运行迁移命令

相关问题