migration codeigniter spark
migration codeigniter spark
php spark make:migration create_categories_table
<?php namespace App\Database\Migrations; use CodeIgniter\Database\Migration; class CreateCategoriesTable extends Migration { public function up() { $this->forge->addField([ 'id' => [ 'type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true ], 'name' => [ 'type' => 'VARCHAR', 'constraint' => '255', ], 'svg' => [ 'type' => 'TEXT', ], 'created_at' => [ 'type' => 'TIMESTAMP', 'default' => 'current_timestamp()', ], 'updated_at' => [ 'type' => 'TIMESTAMP', 'default' => 'current_timestamp()', ] ]); $this->forge->addKey('id', true); $this->forge->createTable('categories'); } public function down() { $this->forge->dropTable('categories'); } }
php spark make:migration create_etiquetas_table
<?php namespace App\Database\Migrations; use CodeIgniter\Database\Migration; class CreateEtiquetasTable extends Migration { public function up() { $this->forge->addField([ 'id' => [ 'type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true ], 'name' => [ 'type' => 'VARCHAR', 'constraint' => '255', ], 'svg' => [ 'type' => 'TEXT', ], 'created_at' => [ 'type' => 'TIMESTAMP', 'default' => 'current_timestamp()', ], 'updated_at' => [ 'type' => 'TIMESTAMP', 'default' => 'current_timestamp()', ] ]); $this->forge->addKey('id', true); $this->forge->createTable('etiquetas'); } public function down() { $this->forge->dropTable('etiquetas'); } }
php spark make:migration create_post_etiquetas_table
<?php namespace App\Database\Migrations; use CodeIgniter\Database\Migration; class CreatePostEtiquetasTable extends Migration { public function up() { $this->forge->addField([ 'post_id' => [ 'type' => 'INT', 'constraint' => 11, 'unsigned' => true, ], 'etiqueta_id' => [ 'type' => 'INT', 'constraint' => 11, 'unsigned' => true, ], 'created_at' => [ 'type' => 'TIMESTAMP', 'default' => 'current_timestamp()', ], 'updated_at' => [ 'type' => 'TIMESTAMP', 'default' => 'current_timestamp()', ] ]); $this->forge->addKey('post_id', true); $this->forge->addKey('etiqueta_id', true); // Claves Foráneas $this->forge->addForeignKey('post_id', 'posts', 'id', 'CASCADE', 'CASCADE'); $this->forge->addForeignKey('etiqueta_id', 'etiquetas', 'id', 'CASCADE', 'CASCADE'); $this->forge->createTable('post_etiquetas'); } public function down() { $this->forge->dropTable('post_etiquetas'); } }
php spark make:migration create_posts_table
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreatePostsTable extends Migration
{
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'INT',
'constraint' => 11,
'unsigned' => true,
'auto_increment' => true
],
'title' => [
'type' => 'VARCHAR',
'constraint' => '255',
],
'category_id' => [
'type' => 'INT',
'constraint' => 11,
'unsigned' => true,
],
// ... otras columnas ...
'created_at' => [
'type' => 'TIMESTAMP',
'default' => 'current_timestamp()',
],
'updated_at' => [
'type' => 'TIMESTAMP',
'default' => 'current_timestamp()',
]
]);
$this->forge->addKey('id', true);
$this->forge->addForeignKey('category_id', 'categories', 'id', 'CASCADE', 'CASCADE');
$this->forge->createTable('posts');
}
public function down()
{
$this->forge->dropTable('posts');
}
}
i finalment:
php spark migrate