How to use Yii2 schema builder for migration?
According to the documentation
I can use the below code to create a migration that creates a new table. Just wondering what am I missing in order to use this new feature? By new function, I mean this line in the documentation.
Many thanks!
"Since 2.0.5 a schema builder that provides a more convenient way to define the schema of a column has been introduced, so the migration above could be written like this:"
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');
}
}
But when I try, I get an error.
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
source to share
1) Check composer.json
for a valid Yii2 version, for example:
"yiisoft/yii2": ">=2.0.6",
Also check the application type used, see this issue and samdark's answer:
Modern Advanced is a new migration style that will only be available in version 2.0.6. Basic doesn't work.
2) Just update the framework to the latest version by running:
composer update
source to share