CommonModel is a versatile and reusable model for CodeIgniter 4, designed to simplify common database operations such as selecting, inserting, updating, and deleting records. This library provides methods that support common SQL features like JOINs, WHERE conditions, LIKE filters, ordering, and more.
- Multiple Database Connection Groups
- Select records with flexible conditions (
WHERE,OR WHERE,LIKE) - Insert single or multiple records into the database
- Update and delete records based on conditions
- Join tables for more complex queries
- Supports ordering and pagination
- Easy counting and existence checks for records
- Built-in support for like queries and batch operations
- Table and column management (add, remove, modify)
- Database creation and deletion
- CommonModel Library for CodeIgniter 4
- Features
- Table of Contents
- Installation
- Usage
- Troubleshooting: Multiple Database Connection Groups
- License
- Author
To use CommonModel in your CodeIgniter 4 project, follow these steps:
-
Install with Composer:
composer require bertugfahriozer/ci4commonmodel
-
Load the model in your controller:
use ci4commonmodel\Models\CommonModel; class ExampleController extends BaseController { protected $commonModel; public function __construct() { $this->commonModel = new CommonModel(); } }
Fetch records from a database table with flexible filters such as WHERE, OR WHERE, LIKE, JOIN, and ordering. Supports limit and pagination.
// Simple usage
$users = $this->commonModel->lists('users', '*', ['status' => 1], 'id DESC', 10);
// Advanced usage with JOIN and LIKE
$joins = [
['table' => 'roles', 'cond' => 'users.role_id = roles.id', 'type' => 'left']
];
$like = ['name' => 'John'];
$users = $this->commonModel->lists('users', 'users.*, roles.name as role', ['status' => 1], 'users.id DESC', 10, 0, $like, [], $joins);Insert a single record into the database and return the newly inserted ID.
$data = [
'name' => 'John Doe',
'email' => '[email protected]',
'status' => 1
];
$insertId = $this->commonModel->create('users', $data);Insert multiple records at once.
$data = [
['name' => 'Alice', 'email' => '[email protected]'],
['name' => 'Bob', 'email' => '[email protected]']
];
$this->commonModel->createMany('users', $data);Update existing records by specifying the WHERE conditions and the new data.
$data = ['status' => 2];
$where = ['id' => 1];
$this->commonModel->edit('users', $data, $where);Delete records from a table based on WHERE conditions.
$where = ['id' => 1];
$this->commonModel->remove('users', $where);Count the number of records that match a given condition.
$where = ['status' => 1];
$count = $this->commonModel->count('users', $where);Check whether a record exists in a table with a specified condition.
$where = ['id' => 1];
$isExist = $this->commonModel->isHave('users', $where);Search records using LIKE queries and filtering by conditions.
$like = ['name' => 'John'];
$where = ['status' => 1];
$results = $this->commonModel->research('users', $like, '*', $where);$tables = $this->commonModel->getTableList();$fields = [
'id' => [
'type' => 'INT',
'constraint' => 5,
'unsigned' => true,
'auto_increment' => true,
],
'title' => [
'type' => 'VARCHAR',
'constraint' => '100',
'unique' => true,
]
];
$this->commonModel->newTable('blog', $fields);$this->commonModel->removeTable('blog');$fields = [
'preferences' => ['type' => 'TEXT', 'after' => 'email'],
];
$this->commonModel->addColumnToTable('users', $fields);$this->commonModel->removeColumnFromTable('users', ['preferences']);$this->commonModel->updateTableName('old_table', 'new_table');$fields = [
'old_name' => [
'name' => 'new_name',
'type' => 'TEXT',
'null' => false,
],
];
$this->commonModel->modifyColumnInfos('users', $fields);$this->commonModel->emptyTableDatas('users');$fields = $this->commonModel->getTableFields('users');$this->commonModel->newDatabase('new_db');$this->commonModel->removeDatabase('old_db');$this->commonModel->drpPrimaryKey('users');$this->commonModel->drpKey('users', 'my_key_name');$this->commonModel->drpForeignKey('orders', 'fk_orders_users');When working with multiple database connections in CodeIgniter 4, defining them in the .env file is not sufficient on its own. Here's an example configuration in .env:
database.default.hostname=127.0.0.1
database.default.database=giritliadali
database.default.username=root
database.default.password=root
database.default.DBDriver=MySQLi
database.default.port=3306
database.secondDB.hostname=127.0.0.1
database.secondDB.database=giritliadali_bac
database.secondDB.username=root
database.secondDB.password=root
database.secondDB.DBDriver=MySQLi
database.secondDB.port=3306Attempting to connect using:
$this->db = \Config\Database::connect('secondDB');Will result in the error:
"secondDB" is not a valid database connection group.
CodeIgniter 4 does not auto-load database groups other than default from the .env file. You must explicitly define custom groups like secondDB in the app/Config/Database.php file.
To fix this, add a new public array in your app/Config/Database.php:
public array $secondDB = [
'DSN' => '',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => '',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug' => true,
'charset' => 'utf8mb4',
'DBCollat' => 'utf8mb4_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 3306,
'numberNative' => false,
'foundRows' => false,
'dateFormat' => [
'date' => 'Y-m-d',
'datetime' => 'Y-m-d H:i:s',
'time' => 'H:i:s',
],
];Once added, \Config\Database::connect('secondDB') will work properly.
This project is licensed under the MIT License. See the LICENSE file for more details.