The Database Forge class in CodeIgniter 4 provides a powerful collection of tools for working with database structures and schema alterations. The Database Forge class allows you to conduct a wide range of database operations within the CodeIgniter framework, such as creating tables, updating columns, adding indexes, and much more.
In this in-depth lesson, we will look at the Database Forge class in CodeIgniter 4. We will walk you through the step-by-step method of using this class to easily construct and alter database structures.
Read More: How To Integrate ChatGPT API in CodeIgniter 4 Tutorial
The Database Forge class will be your go-to tool whether you need to create a new database from start or make changes to an existing schema.
Let’s get started.
CodeIgniter 4 Installation
To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.
composer create-project codeigniter4/appstarter codeigniter-4
Assuming you have successfully installed application into your local system.
What is Database Forge Class?
The Database Forge class in CodeIgniter 4 provides a strong set of tools for modifying database structures and schema.
It contains methods that help you manage your database.
Load the Forge Class as follows:
$forge = \Config\Database::forge();
If the database you wish to manage is not the default one, you may alternatively supply another database group name to the DB Forge loader:
$this->forge = \Config\Database::forge('other_db');
How To Create Database Using Forge Object
We can use createDatabase() method of database forge to create database.
Here, is the call to create it:
$forge->createDatabase('myDb')
It returns true/false based on success or failure.
If the optional second parameter is set to true, an IF EXISTS statement will be added or a database will be checked for existence before being created (depending on the DBMS).
$forge->createDatabase('myDb', true);
How To Drop Database Using Forge Object
To drop a schema, you can use dropDatabase() method of it.
Here, is the way to call it.
$forge->dropDatabase('myDb');
It also returns true/false based on success or failure.
Read More: How To Cache Database Query Using CodeIgniter Cache?
How To Create Table Using Forge Object
When building tables, you may want to do a few things. Change the columns, add fields, and add keys to the table. CodeIgniter has a way for doing this.
We will use these following methods to create a table with defined fields and also with keys.
$forge->addField();
$forge->addPrimaryKey();
$forge->addUniqueKey();
$forge->createTable();
Here, is the complete code by which you can create a table structure in database.
<?php namespace App\Database\Migrations; use CodeIgniter\Database\Migration; class CreatePostsTable extends Migration { public function up() { $this->forge->addField([ "id" => [ "type" => "INT", "constraint" => 5, "auto_increment" => true, "unsigned" => true, ], "title" => [ "type" => "VARCHAR", "constraint" => 80, "null" => false, ], "content" => [ "type" => "TEXT", "null" => true, ], "created_at datetime default current_timestamp", "updated_at datetime default current_timestamp", ]); $this->forge->addPrimaryKey("id"); $this->forge->createTable("posts"); } public function down() { $this->forge->dropTable("posts"); } }
That’s it.
We hope this article helped you to learn about About Database Forge Class of CodeIgniter 4 Tutorial in a very detailed way.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.