简体   繁体   English

Yii2 创建用户表

[英]Yii2 create user table

I installed Yii 2 with advanced application template to my localhost everything is fine except command migrate when I use cmd to create user table it doesn't do any thing.我将带有高级应用程序模板的 Yii 2 安装到我的本地主机上,一切都很好,除了命令 migrate 当我使用 cmd 创建用户表时它没有做任何事情。 My code in main-local : "I:\zzerver\Uwamp -port 84\uw-cms-p8585\www\yii1\common\config\main-local.php"我在 main-local 中的代码:“I:\zzerver\Uwamp -port 84\uw-cms-p8585\www\yii1\common\config\main-local.php”

is :是 :

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,
        ],
    ],
];

I did change localhost:8585 to 127.0.0.1:8585 but still has the same problem, I'm using phpmyadmin, and server is Uwamp我确实将 localhost:8585 更改为 127.0.0.1:8585 但仍然有同样的问题,我使用的是 phpmyadmin,服务器是 Uwamp

the init command work good but when I try : php yii migrate or yii migrate no thing happen. init 命令运行良好,但是当我尝试时:php yii migrate 或 yii migrate 没有任何反应。 this is the cmd这是命令

在此处输入图像描述

that all what it show , and nothing created in database它显示的所有内容,并且没有在数据库中创建任何内容

I did download "Yii 2 with advanced application template" after that I did open cmd and run php init我确实下载了“带有高级应用程序模板的 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>

I created database name : db001name001, user : db001name001, password : yii2db001,我创建了数据库名称:db001name001,用户:db001name001,密码:yii2db001,

I add information to common\config\main-local.php我将信息添加到 common\config\main-local.php

I did open again cmd and run php yii migrate我确实再次打开 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)

but nothing happen no table created for user, I want to create table for user to start creating a signup form但什么也没发生没有为用户创建表,我想为用户创建表以开始创建注册表单

common\config\main-local.php 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,
            ],
        ],
    ];

common\config\main.php:常见\配置\main.php:

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

my console console\config\main.php"我的控制台 console\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,
];

Going by only the information you provided, I'd suggest that you read about Yii2 migrations here .仅根据您提供的信息,我建议您在此处阅读有关 Yii2 迁移的信息。 In your example you did not provide any commands to Yii, just checked if the migration tool is installed.在您的示例中,您没有向 Yii 提供任何命令,只是检查了是否安装了迁移工具。 After/If you've done that, please, clarify your question - show us what commands you've tried to run, and the error messages too, if there's any.之后/如果您这样做了,请澄清您的问题 - 向我们展示您尝试运行的命令,以及错误消息(如果有)。

The linked documentation has a few examples, including code for creating tables, the same thing you want to do.链接的文档有一些示例,包括用于创建表的代码,这与您想要做的事情相同。

at the first look ssems you have not db component seeting in your console/config/main.php乍一看 ssems,你的 console/config/main.php 中没有 db 组件

try add a proper db connect eg for mysql尝试添加适当的数据库连接,例如为 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,
  ];

and then try run you migration command然后尝试运行你的迁移命令

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

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