En este artículo veremos Cómo crear un crud con el framework yii2 , usando migraciones y la herramienta de línea de comandos.
¿Cómo crear migraciones en yii2 para el crud?
Primero crearemos la tabla con el uso de migraciones
// create migration
php yii migrate/create Country
Ahora hemos generado un archivo con formato de fecha migrations / m210725_210438_Country.php
use yii\db\Migration;
// you need to add Schema for this way to work with migrations
use yii\db\Schema;
/**
* Class m210725_210438_Country
*/
class m210725_210438_Country extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
echo "m210725_210438_Country cannot be reverted.\n";
return false;
}
// Use up()/down() to run migration code without a transaction.
public function up()
{
$this->createTable('country', [
'id' => Schema::TYPE_PK,
'country' => Schema::TYPE_STRING . ' NOT NULL',
]);
}
public function down()
{
// echo "m210725_210438_Country cannot be reverted.\n";
// return false;
$this->dropTable('country');
}
}
¿Cómo ejecutar migraciones en yii2?
Ahora ejecutamos las migraciones, en este caso solo tenemos 1 donde llamaremos a la función up() para crear una tabla con la función createTable
// we execute migrations
php yii migrate
En el curso completo de yii aprenderás desde cero como trabajar con este framework.