如何使用 Yii2 架构构建器进行迁移?

How to use Yii2 schema builder for a migration?

根据 documentation

我可以使用下面的代码创建一个创建新 table 的迁移。 只是想知道使用这个新功能我缺少什么?我所说的新功能是指文档中的这一行。

非常感谢!

"Since 2.0.5 schema builder which provides more convenient way defining column schema was introduced so migration above could be written like the following:"

use yii\db\Schema;
use yii\db\Migration;

class m150101_185401_create_news_table extends \yii\db\Migration
{
    public function up()
    {
        $this->createTable('news', [
            'id' => Schema::primaryKey(),
            'title' => Schema::string()->notNull(),
            'content' => Schema::text(),
        ]);
    }

    public function down()
    {
        $this->dropTable('news');
    }
}

但是当我尝试时收到一条错误消息。

Yii Migration Tool (based on Yii v2.0.5)

Total 1 new migration to be applied:
    m150717_020723_create_news_table

Apply the above migration? (yes|no) [no]:yes
*** applying m150717_020723_create_news_table
PHP Fatal error:  Call to undefined method yii\db\Schema::primaryKey() in

1) 检查您的 composer.json 是否包含有效的 Yii2 版本,例如:

"yiisoft/yii2": ">=2.0.6",

同时检查使用的应用程序类型,参见 this issue 和 samdark 的回答:

Current advanced is using new migrations style which will be available in 2.0.6 only. Basic does not.

2) 只需通过 运行:

将框架更新到最新版本
composer update