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, and ordering.
-
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
To use CommonModel
in your CodeIgniter 4 project, follow these steps:
-
Install with Composer into your project:
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(); } }
The lists
method allows you to fetch records from a database table with flexible filters such as WHERE
, OR WHERE
, LIKE
, JOIN
, and ordering. Optionally, limit and pagination can be applied.
$data = [
'name' => 'John Doe',
'email' => '[email protected]',
'status' => 1
];
$insertId = $this->commonModel->create('users', $data);
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 with the createMany method.
$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);
Use research for searching records using LIKE queries and filtering by conditions.
$like = ['name' => 'John'];
$where = ['status' => 1];
$results = $this->commonModel->research('users', $like, '*', $where);
This project is licensed under the MIT License. See the LICENSE file for more details.