DS Log
In my blog, I delve into the world of programming web technologies, Linux, Unix-like, and graphic design using free tools on Linux.
KINGCODE
KingCode Editor (ex Texty Editor) is my project developed using Java Swing. Project is still in development and in beta version. I plan to add additional features focused for PYTHON, PHP, JAVA, C, JS and BASH.
Read more ↗
VUE on Linux
In this guide, I'll walk you through the step-by-step process of setting up Vue.js on your Linux system, empowering you to create dynamic and interactive web applications. Let's harness the power of Vue.js together on the Linux platform!
Read more ↗
Symfony PHP
Dive into the world of Symfony PHP with this comprehensive introduction. In this guide, you'll learn the essential steps to create and manage posts and users, empowering you to build dynamic web applications with ease.
Read more ↗
Trying Linux from Windows
How to set up a PHP development server on Ubuntu 22.04
Text editors
List of text editors for developers.
Read more ↗
Fonts
Important fonts everyone needs to know.
Read more ↗
Try Linux from Windows
Here are some quick videos I made showing how to try out Linux Mint on Windows.
Read more ↗
Saturday, July 13, 2024
CodeIgniter - migration
"Migration" is the name for the process in which we load the migration file via the CLI command and, based on the loaded settings, create tables/columns in the database. The point is that when we transfer the project to another computer, we can create the entire database skeleton with a CLI command.
Let's prepare the project
If I don't want to write about how to prepare the project, you can find everything
here.
To create a migration file:
php spark make:migration main
With this we get the file:
./app/Database/Migrations/2022-08-07-084359_Main.php
With content:
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class Main extends Migration
{
public function up()
{
//
}
public function down()
{
//
}
}
With the "up" method we enter data into the database, and with the "down" method we delete the entered data. The "up" method with the entered data looks like this:
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'INT',
'constraint' => 5,
'unsigned' => true,
'auto_increment' => true,
],
'name' => [
'type' => 'VARCHAR',
'constraint' => '256',
'null' => false
],
'updated_at' => [
'type' => 'datetime',
'null' => true,
],
'created_at datetime default current_timestamp',
]);
$this->forge->addPrimaryKey('id');
$this->forge->createTable('products');
}
After entering the data in the "up" method in the terminal, we run:
php spark migrate
...and now we see that the table and columns are in the database:
Rollback
This command executes what is in the "down" method of the migration file. Here we specify that we will delete the "products" table:
public function down()
{
$this->forge->dropTable('products');
}
The CLI command looks like this:
php spark migrate:rollback
Multiple tables in one migration
If we want to have more tables in one migration file, we simply add them one below the other in the "up" and "down" methods.
public function up()
{
$this->forge->addField([
...
$this->forge->createTable('products');
$this->forge->addField([
...
$this->forge->createTable('users');
}
public function down()
{
$this->forge->dropTable('products');
$this->forge->dropTable('users');
}
Foreign keys
For example, we want the "userId" column to be connected to the "id" column in the "users" table.
$this->forge->addField([
...
'userId' => [
'type' => 'INT',
'unsigned' => true,
'null' => false,
],
...
]);
...
$this->forge->addForeignKey('userId', 'users', 'id', 'CASCADE', 'CASCADE');
That's it!