bertugfahriozer/ci4commonmodel

With the codeigniter 4 database builder, I combined the general methods I used for myself into a single model.

1.1.11 2024-09-15 21:41 UTC

This package is auto-updated.

Last update: 2025-02-15 22:41:39 UTC


README

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.

Features

Installation

To use CommonModel in your CodeIgniter 4 project, follow these steps:

  1. Install with Composer into your project:

    composer require bertugfahriozer/ci4commonmodel
  2. Load the model in your controller:

    use ci4commonmodel\Models\CommonModel;
    
    class ExampleController extends BaseController
    {
        protected $commonModel;
    
        public function __construct()
        {
            $this->commonModel = new CommonModel();
        }
    }

Usage

1. Retrieving Records (lists)

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' => 'john@example.com',
    'status' => 1
];

$insertId = $this->commonModel->create('users', $data);

2. Inserting Records (create)

Insert a single record into the database and return the newly inserted ID.

$data = [
    'name' => 'John Doe',
    'email' => 'john@example.com',
    'status' => 1
];

$insertId = $this->commonModel->create('users', $data);

3. Batch Insert (createMany)

Insert multiple records at once with the createMany method.

$data = [
    ['name' => 'Alice', 'email' => 'alice@example.com'],
    ['name' => 'Bob', 'email' => 'bob@example.com']
];

$this->commonModel->createMany('users', $data);

4. Updating Records (edit)

Update existing records by specifying the WHERE conditions and the new data.

$data = ['status' => 2];
$where = ['id' => 1];

$this->commonModel->edit('users', $data, $where);

5. Deleting Records (remove)

Delete records from a table based on WHERE conditions.

$where = ['id' => 1];
$this->commonModel->remove('users', $where);

6. Count Records (count)

Count the number of records that match a given condition.

$where = ['status' => 1];
$count = $this->commonModel->count('users', $where);

7. Check Record Existence (isHave)

Check whether a record exists in a table with a specified condition.

$where = ['id' => 1];
$isExist = $this->commonModel->isHave('users', $where);

8. Complex Queries (research)

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);

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Author

Bertuğ Fahri ÖZER