{"id":15046413,"url":"https://github.com/bertugfahriozer/ci4commonmodel","last_synced_at":"2025-10-08T13:21:16.221Z","repository":{"id":40721472,"uuid":"507971946","full_name":"bertugfahriozer/ci4commonModel","owner":"bertugfahriozer","description":"A flexible and reusable CommonModel for CodeIgniter 4. Simplifies all database operations—select, insert, update, delete, join, batch, and advanced queries—plus table and database management.","archived":false,"fork":false,"pushed_at":"2025-07-25T07:42:38.000Z","size":81,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-30T20:16:50.294Z","etag":null,"topics":["batch-insert","codeigniter-crud","codeigniter-library","codeigniter-model","codeigniter4","crud","database","database-management","join","library","like-query","migration","model","open-source","php","query-builder","table-management","where"],"latest_commit_sha":null,"homepage":"https://bertugfahriozer.github.io/ci4commonModel/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bertugfahriozer.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-06-27T15:52:44.000Z","updated_at":"2025-09-24T23:48:14.000Z","dependencies_parsed_at":"2024-09-13T12:44:04.557Z","dependency_job_id":"8dd052c7-28ea-49d3-96d1-3629c20e062d","html_url":"https://github.com/bertugfahriozer/ci4commonModel","commit_stats":{"total_commits":15,"total_committers":2,"mean_commits":7.5,"dds":"0.19999999999999996","last_synced_commit":"aec5e289f56b48dd3ae9653ceee829aa2f3a1f91"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"purl":"pkg:github/bertugfahriozer/ci4commonModel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bertugfahriozer%2Fci4commonModel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bertugfahriozer%2Fci4commonModel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bertugfahriozer%2Fci4commonModel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bertugfahriozer%2Fci4commonModel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bertugfahriozer","download_url":"https://codeload.github.com/bertugfahriozer/ci4commonModel/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bertugfahriozer%2Fci4commonModel/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278949791,"owners_count":26074099,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-08T02:00:06.501Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["batch-insert","codeigniter-crud","codeigniter-library","codeigniter-model","codeigniter4","crud","database","database-management","join","library","like-query","migration","model","open-source","php","query-builder","table-management","where"],"created_at":"2024-09-24T20:53:04.928Z","updated_at":"2025-10-08T13:21:16.215Z","avatar_url":"https://github.com/bertugfahriozer.png","language":"PHP","readme":"# CommonModel Library for CodeIgniter 4\n\n`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.\n\n## Features\n\n- Multiple Database Connection Groups\n- **Select records** with flexible conditions (`WHERE`, `OR WHERE`, `LIKE`)\n- **Insert single or multiple records** into the database\n- **Update and delete** records based on conditions\n- **Join tables** for more complex queries\n- Supports **ordering** and **pagination**\n- Easy **counting** and **existence checks** for records\n- Built-in support for **like** queries and **batch operations**\n- Table and column management (add, remove, modify)\n- Database creation and deletion\n\n## Table of Contents\n\n- [CommonModel Library for CodeIgniter 4](#commonmodel-library-for-codeigniter-4)\n  - [Features](#features)\n  - [Table of Contents](#table-of-contents)\n  - [Installation](#installation)\n  - [Usage](#usage)\n    - [1. Retrieving Records (`lists`)](#1-retrieving-records-lists)\n    - [2. Inserting Records (`create`)](#2-inserting-records-create)\n    - [3. Batch Insert (`createMany`)](#3-batch-insert-createmany)\n    - [4. Updating Records (`edit`)](#4-updating-records-edit)\n    - [5. Deleting Records (`remove`)](#5-deleting-records-remove)\n    - [6. Count Records (`count`)](#6-count-records-count)\n    - [7. Check Record Existence (`isHave`)](#7-check-record-existence-ishave)\n    - [8. Complex Queries (`research`)](#8-complex-queries-research)\n    - [9. Table and Database Management](#9-table-and-database-management)\n      - [Get Table List](#get-table-list)\n      - [Create a New Table](#create-a-new-table)\n      - [Remove a Table](#remove-a-table)\n      - [Add Column to Table](#add-column-to-table)\n      - [Remove Column from Table](#remove-column-from-table)\n      - [Rename Table](#rename-table)\n      - [Modify Column Info](#modify-column-info)\n      - [Truncate Table](#truncate-table)\n      - [Get Table Fields](#get-table-fields)\n      - [Create a New Database](#create-a-new-database)\n      - [Remove a Database](#remove-a-database)\n      - [Drop Primary Key](#drop-primary-key)\n      - [Drop Key](#drop-key)\n      - [Drop Foreign Key](#drop-foreign-key)\n  - [Troubleshooting: Multiple Database Connection Groups](#troubleshooting-multiple-database-connection-groups)\n    - [Root Cause](#root-cause)\n    - [Solution](#solution)\n  - [License](#license)\n  - [Author](#author)\n\n## Installation\n\nTo use `CommonModel` in your CodeIgniter 4 project, follow these steps:\n\n1. Install with Composer:\n    ```bash\n    composer require bertugfahriozer/ci4commonmodel\n    ```\n\n2. Load the model in your controller:\n    ```php\n    use ci4commonmodel\\Models\\CommonModel;\n\n    class ExampleController extends BaseController\n    {\n        protected $commonModel;\n\n        public function __construct()\n        {\n            $this-\u003ecommonModel = new CommonModel();\n        }\n    }\n    ```\n\n## Usage\n\n### 1. Retrieving Records (`lists`)\nFetch records from a database table with flexible filters such as `WHERE`, `OR WHERE`, `LIKE`, `JOIN`, and ordering. Supports limit and pagination.\n\n```php\n// Simple usage\n$users = $this-\u003ecommonModel-\u003elists('users', '*', ['status' =\u003e 1], 'id DESC', 10);\n\n// Advanced usage with JOIN and LIKE\n$joins = [\n    ['table' =\u003e 'roles', 'cond' =\u003e 'users.role_id = roles.id', 'type' =\u003e 'left']\n];\n$like = ['name' =\u003e 'John'];\n$users = $this-\u003ecommonModel-\u003elists('users', 'users.*, roles.name as role', ['status' =\u003e 1], 'users.id DESC', 10, 0, $like, [], $joins);\n```\n\n### 2. Inserting Records (`create`)\nInsert a single record into the database and return the newly inserted ID.\n\n```php\n$data = [\n    'name' =\u003e 'John Doe',\n    'email' =\u003e 'john@example.com',\n    'status' =\u003e 1\n];\n\n$insertId = $this-\u003ecommonModel-\u003ecreate('users', $data);\n```\n\n### 3. Batch Insert (`createMany`)\nInsert multiple records at once.\n\n```php\n$data = [\n    ['name' =\u003e 'Alice', 'email' =\u003e 'alice@example.com'],\n    ['name' =\u003e 'Bob', 'email' =\u003e 'bob@example.com']\n];\n\n$this-\u003ecommonModel-\u003ecreateMany('users', $data);\n```\n\n### 4. Updating Records (`edit`)\nUpdate existing records by specifying the WHERE conditions and the new data.\n\n```php\n$data = ['status' =\u003e 2];\n$where = ['id' =\u003e 1];\n\n$this-\u003ecommonModel-\u003eedit('users', $data, $where);\n```\n\n### 5. Deleting Records (`remove`)\nDelete records from a table based on WHERE conditions.\n\n```php\n$where = ['id' =\u003e 1];\n$this-\u003ecommonModel-\u003eremove('users', $where);\n```\n\n### 6. Count Records (`count`)\nCount the number of records that match a given condition.\n\n```php\n$where = ['status' =\u003e 1];\n$count = $this-\u003ecommonModel-\u003ecount('users', $where);\n```\n\n### 7. Check Record Existence (`isHave`)\nCheck whether a record exists in a table with a specified condition.\n\n```php\n$where = ['id' =\u003e 1];\n$isExist = $this-\u003ecommonModel-\u003eisHave('users', $where);\n```\n\n### 8. Complex Queries (`research`)\nSearch records using LIKE queries and filtering by conditions.\n\n```php\n$like = ['name' =\u003e 'John'];\n$where = ['status' =\u003e 1];\n\n$results = $this-\u003ecommonModel-\u003eresearch('users', $like, '*', $where);\n```\n\n### 9. Table and Database Management\n\n#### Get Table List\n```php\n$tables = $this-\u003ecommonModel-\u003egetTableList();\n```\n\n#### Create a New Table\n```php\n$fields = [\n    'id' =\u003e [\n        'type' =\u003e 'INT',\n        'constraint' =\u003e 5,\n        'unsigned' =\u003e true,\n        'auto_increment' =\u003e true,\n    ],\n    'title' =\u003e [\n        'type' =\u003e 'VARCHAR',\n        'constraint' =\u003e '100',\n        'unique' =\u003e true,\n    ]\n];\n$this-\u003ecommonModel-\u003enewTable('blog', $fields);\n```\n\n#### Remove a Table\n```php\n$this-\u003ecommonModel-\u003eremoveTable('blog');\n```\n\n#### Add Column to Table\n```php\n$fields = [\n    'preferences' =\u003e ['type' =\u003e 'TEXT', 'after' =\u003e 'email'],\n];\n$this-\u003ecommonModel-\u003eaddColumnToTable('users', $fields);\n```\n\n#### Remove Column from Table\n```php\n$this-\u003ecommonModel-\u003eremoveColumnFromTable('users', ['preferences']);\n```\n\n#### Rename Table\n```php\n$this-\u003ecommonModel-\u003eupdateTableName('old_table', 'new_table');\n```\n\n#### Modify Column Info\n```php\n$fields = [\n    'old_name' =\u003e [\n        'name' =\u003e 'new_name',\n        'type' =\u003e 'TEXT',\n        'null' =\u003e false,\n    ],\n];\n$this-\u003ecommonModel-\u003emodifyColumnInfos('users', $fields);\n```\n\n#### Truncate Table\n```php\n$this-\u003ecommonModel-\u003eemptyTableDatas('users');\n```\n\n#### Get Table Fields\n```php\n$fields = $this-\u003ecommonModel-\u003egetTableFields('users');\n```\n\n#### Create a New Database\n```php\n$this-\u003ecommonModel-\u003enewDatabase('new_db');\n```\n\n#### Remove a Database\n```php\n$this-\u003ecommonModel-\u003eremoveDatabase('old_db');\n```\n\n#### Drop Primary Key\n```php\n$this-\u003ecommonModel-\u003edrpPrimaryKey('users');\n```\n\n#### Drop Key\n```php\n$this-\u003ecommonModel-\u003edrpKey('users', 'my_key_name');\n```\n\n#### Drop Foreign Key\n```php\n$this-\u003ecommonModel-\u003edrpForeignKey('orders', 'fk_orders_users');\n```\n\n## Troubleshooting: Multiple Database Connection Groups\n\nWhen 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`:\n\n```ini\ndatabase.default.hostname=127.0.0.1\ndatabase.default.database=giritliadali\ndatabase.default.username=root\ndatabase.default.password=root\ndatabase.default.DBDriver=MySQLi\ndatabase.default.port=3306\n\ndatabase.secondDB.hostname=127.0.0.1\ndatabase.secondDB.database=giritliadali_bac\ndatabase.secondDB.username=root\ndatabase.secondDB.password=root\ndatabase.secondDB.DBDriver=MySQLi\ndatabase.secondDB.port=3306\n```\n\nAttempting to connect using:\n\n```php\n$this-\u003edb = \\Config\\Database::connect('secondDB');\n```\n\nWill result in the error:\n\n```\n\"secondDB\" is not a valid database connection group.\n```\n\n### Root Cause\n\nCodeIgniter 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.\n\n### Solution\n\nTo fix this, add a new public array in your `app/Config/Database.php`:\n\n```php\npublic array $secondDB = [\n        'DSN'          =\u003e '',\n        'hostname'     =\u003e 'localhost',\n        'username'     =\u003e '',\n        'password'     =\u003e '',\n        'database'     =\u003e '',\n        'DBDriver'     =\u003e 'MySQLi',\n        'DBPrefix'     =\u003e '',\n        'pConnect'     =\u003e false,\n        'DBDebug'      =\u003e true,\n        'charset'      =\u003e 'utf8mb4',\n        'DBCollat'     =\u003e 'utf8mb4_general_ci',\n        'swapPre'      =\u003e '',\n        'encrypt'      =\u003e false,\n        'compress'     =\u003e false,\n        'strictOn'     =\u003e false,\n        'failover'     =\u003e [],\n        'port'         =\u003e 3306,\n        'numberNative' =\u003e false,\n        'foundRows'    =\u003e false,\n        'dateFormat'   =\u003e [\n            'date'     =\u003e 'Y-m-d',\n            'datetime' =\u003e 'Y-m-d H:i:s',\n            'time'     =\u003e 'H:i:s',\n        ],\n    ];\n```\n\nOnce added, `\\Config\\Database::connect('secondDB')` will work properly.\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](https://opensource.org/license/mit) file for more details.\n\n## Author\n\n[Bertuğ Fahri ÖZER](https://github.com/bertugfahriozer)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbertugfahriozer%2Fci4commonmodel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbertugfahriozer%2Fci4commonmodel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbertugfahriozer%2Fci4commonmodel/lists"}