Download the PHP package onlyphp/codeigniter3-model without Composer
On this page you can find all versions of the php package onlyphp/codeigniter3-model. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download onlyphp/codeigniter3-model
More information about onlyphp/codeigniter3-model
Files in onlyphp/codeigniter3-model
Package codeigniter3-model
Short Description Advanced model class for CodeIgniter 3 with eager loading and pagination capabilities
License MIT
Informations about the package codeigniter3-model
Advanced MY_Model for CodeIgniter 3 🚀
A powerful extension of CodeIgniter 3's base Model class that brings modern ORM features to your CI3 applications. This package introduces Laravel-style eloquent features, advanced query capabilities, and robust database interaction layers while maintaining CodeIgniter's simplicity.
⚠️ Warning
DO NOT USE THIS PACKAGE IN PRODUCTION
This package is under active development and may contain critical bugs. It is primarily intended for personal use and testing. The current version has not undergone rigorous testing and may be unstable.
📝 Requirements
- PHP >= 8.0
- CodeIgniter 3.x
MySQL
Database
Add this line in composer.json
before install or update.
🔧 Installation
✨ Key Features
- 🔄 Laravel Eloquent-style Query Builder: Write expressive and chainable database queries like laravel
- 🔗 Smart Relationship Handling: Define and manage model relationships effortlessly
- 🚀 Eager Loading: Solve the N+1 query problem with efficient data loading
- 🛡️ Security Layer: XSS protection and output escaping
- ♻️ Soft Deletes: Safely handle record deletion with recovery options
- ✅ Automatic Validation: Built-in validation when creating or updating records
- 📝 Advanced Pagination: Flexible pagination with AJAX support
- 🎯 Raw Query Support: Execute complex custom SQL when needed
- 📦 Batch Operations: Efficient handling of multiple records
📚 Basic Model Configuration
Here's a complete example of how to set up your model with all available configurations:
📚 Usage Examples
Basic Query Operations
Soft Deletes
🔄 Relationships and Eager Loading
Defining Relationships
📄 Pagination Examples
Basic Pagination
Ajax DataTables Integration
🔒 Security Features
XSS Protection
Validation
🔍 Advanced Query Examples
📄 License
This project is licensed under the MIT License.
🏷️ Changelog
Click to view changelog
### v1.0.0 (2025-01-01) - Initial release - Basic query builder functionality - Soft delete implementation - Basic relationship handling - Security layer implementation ### v1.0.9 (2025-04-04) - Introduce the lazy(), cursor(), filter(), pluck(), contains(), exists(), doesntExist(), sortBy(), sortByMultiple() method. - Fixed issue with eager load. - Improved performanced. ### v1.1.1 (2025-04-08) - Introduce the useIndex(), forceIndex(), ignoreIndex() & suggestIndex() method.📫 Support
For bugs and feature requests, please use the GitHub Issues page.
🙏 Acknowledgments
- Inspired by Laravel's Eloquent ORM
- Built on CodeIgniter 3's solid foundation
📄 Basic Documentation
Collection Methods
Function | Description |
---|---|
chunk() |
Process data in chunks to handle large datasets efficiently. Similar to Laravel's chunk() . |
cursor() |
Returns a generator that lazily iterates over query results one record at a time with chunk size 1000. Similar to Laravel's cursor() . |
lazy() |
Returns a lazy collection of results, loading items as needed to conserve memory. Similar to Laravel's lazy() method. |
filter() |
Filters the collection using a callback function, keeping only items that pass the given truth test. Similar to Laravel's collection filter() . |
pluck() |
Retrieves all values for a given key from the collection. Similar to Laravel's collection pluck() method. |
contains() |
Determines if the collection contains a given item or if a given key-value pair exists in the collection. Similar to Laravel's collection contains() method. |
get() |
Retrieves all data from the database based on the specified criteria. |
fetch() |
Retrieves a single record from the database based on the specified criteria. |
first() |
Retrieves the first record based on the query. |
last() |
Retrieves the last record based on the query. |
count() |
Counts the number of records matching the specified criteria. |
find() |
Finds a record by its primary key (ID). |
toSql() |
Returns the SQL query string (without eager loading query). |
exists() |
Determines if the collection has at least one item. Returns true if the collection has one or more items. Similar to Laravel's collection exists() . |
doesntExist() |
Determines if the collection is empty. Returns true if the collection has no items. Similar to Laravel's collection doesntExist() . |
sortBy() |
Sorts the collection by the given key. Similar to Laravel's collection sortBy() method. |
sortByMultiple() |
Sorts the collection by multiple keys. Allows you to specify an array of keys and their corresponding sort directions. Similar to Laravel's sortBy() but with multiple criteria. |
Query Functions
Function | Description |
---|---|
rawQuery() |
Execute raw SQL queries directly. Useful for complex queries not supported by the query builder. |
table() |
Specifies the database table for the query. |
select() |
Defines the columns to retrieve, with auto-prefixing, intelligent formatting, and safety against SQL injection. Similar to Laravel’s select(). |
selectRaw() |
Defines raw SQL columns or expressions for selection. Use this for advanced use cases like aggregates, functions, and subqueries. |
where() |
Adds a basic WHERE clause to the query. Similar to Laravel's where() . |
orWhere() |
Adds an OR WHERE clause. Similar to Laravel's orWhere() . |
whereNull() |
Adds a WHERE clause to check for NULL values. Similar to Laravel's whereNull() . |
orWhereNull() |
Adds an OR WHERE clause to check for NULL values. Similar to Laravel's orWhereNull() . |
whereNotNull() |
Adds a WHERE clause to check for non-NULL values. Similar to Laravel's whereNotNull() . |
orWhereNotNull() |
Adds an OR WHERE clause to check for non-NULL values. Similar to Laravel's orWhereNotNull() . |
whereExists() |
Adds a WHERE EXISTS clause. Similar to Laravel's whereExists() . |
orWhereExists() |
Adds an OR WHERE EXISTS clause. Similar to Laravel's orWhereExists() . |
whereNotExists() |
Adds a WHERE NOT EXISTS clause. Similar to Laravel's whereNotExists() . |
orWhereNotExists() |
Adds an OR WHERE NOT EXISTS clause. Similar to Laravel's orWhereNotExists() . |
whereNot() |
Adds a WHERE NOT clause for negating conditions. Similar to Laravel's whereNot() . |
orWhereNot() |
Adds an OR WHERE NOT clause for negating conditions. Similar to Laravel's orWhereNot() . |
whereTime() |
Adds a WHERE clause for a time comparison. Similar to Laravel's whereTime() . |
orWhereTime() |
Adds an OR WHERE clause for a time comparison. Similar to Laravel's orWhereTime() . |
whereDate() |
Adds a WHERE clause for a date comparison. Similar to Laravel's whereDate() . |
orWhereDate() |
Adds an OR WHERE clause for a date comparison. Similar to Laravel's orWhereDate() . |
whereDay() |
Adds a WHERE clause for a specific day. Similar to Laravel's whereDay() . |
orWhereDay() |
Adds an OR WHERE clause for a specific day. Similar to Laravel's orWhereDay() . |
whereYear() |
Adds a WHERE clause for a specific year. Similar to Laravel's whereYear() . |
orWhereYear() |
Adds an OR WHERE clause for a specific year. Similar to Laravel's orWhereYear() . |
whereMonth() |
Adds a WHERE clause for a specific month. Similar to Laravel's whereMonth() . |
orWhereMonth() |
Adds an OR WHERE clause for a specific month. Similar to Laravel's orWhereMonth() . |
whereIn() |
Adds a WHERE IN clause. Similar to Laravel's whereIn() . |
orWhereIn() |
Adds an OR WHERE IN clause. Similar to Laravel's orWhereIn() . |
whereNotIn() |
Adds a WHERE NOT IN clause. Similar to Laravel's whereNotIn() . |
orWhereNotIn() |
Adds an OR WHERE NOT IN clause. Similar to Laravel's orWhereNotIn() . |
whereBetween() |
Adds a WHERE BETWEEN clause. Similar to Laravel's whereBetween() . |
orWhereBetween() |
Adds an OR WHERE BETWEEN clause. Similar to Laravel's orWhereBetween() . |
whereNotBetween() |
Adds a WHERE NOT BETWEEN clause. Similar to Laravel's whereNotBetween() . |
orWhereNotBetween() |
Adds an OR WHERE NOT BETWEEN clause. Similar to Laravel's orWhereNotBetween() . |
when() |
Applies a conditional callback to modify the query only when a given value or expression is true. |
join() |
Adds an INNER JOIN to the query. Similar to CodeIgniter’s join() . |
rightJoin() |
Adds a RIGHT JOIN to the query. Similar to Laravel's rightJoin() . |
leftJoin() |
Adds a LEFT JOIN to the query. Similar to Laravel's leftJoin() . |
innerJoin() |
Adds an INNER JOIN to the query. Same as join() . |
outerJoin() |
Adds an OUTER JOIN to the query. Similar to Laravel's outerJoin() . |
limit() |
Limits the number of records returned. Similar to CodeIgniter's limit() . |
offset() |
Skips a number of records before starting to return records. Similar to CodeIgniter's offset() . |
orderBy() |
Adds an ORDER BY clause. Similar to Laravel's orderBy() . |
groupBy() |
Adds a GROUP BY clause. Similar to Laravel's groupBy() . |
groupByRaw() |
Adds a raw GROUP BY clause. Similar to Laravel's groupByRaw() . |
having() |
Adds a HAVING clause. Similar to Laravel's having() . |
havingRaw() |
Adds a raw HAVING clause. Similar to Laravel's havingRaw() . |
withTrashed() |
Retrieves both soft deleted and non-deleted records from the database. When using this method, the results include records that have been soft deleted (i.e., those with a deleted_at timestamp) alongside active records. |
onlyTrashed() |
Retrieves only the records that have been soft deleted (i.e., records with a deleted_at timestamp). This method excludes active (non-deleted) records from the query results. |
Pagination Functions
Function | Description |
---|---|
setPaginateFilterColumn() |
Sets the filter conditions for pagination. If not set, all columns from the main table are queried. |
paginate() |
Custom pagination method that works without the datatable library. Allows paginating results based on the specified criteria. |
paginate_ajax() |
Pagination method specifically designed to work with AJAX requests and integrate with datatables. |
paginate_select_input() |
Custom method for handling pagination with the select input (e.g., input for changing the number of results per page). |
Relationship Functions (in model only)
Function | Description |
---|---|
hasMany() |
Defines a one-to-many relationship. Similar to Laravel's hasMany() . |
hasOne() |
Defines a one-to-one relationship. Similar to Laravel's hasOne() . |
Eager Load Functions
Function | Description |
---|---|
with() |
Eager loads related models to prevent N+1 query performance issues. Similar to Laravel's with() . |
withCount() |
Adds a count of related models as an additional attribute. |
withSum() |
Calculates the sum of a specific column from related models. |
withMax() |
Retrieves the maximum value of a specific column from related models. |
withMin() |
Retrieves the minimum value of a specific column from related models. |
withAvg() |
Retrieves the average value of a specific column from related models. |
whereHas() |
Filters models based on the existence of a related model that meets a specific condition. |
orWhereHas() |
Adds an OR condition to filter models based on the existence of a related model that meets a specific condition. |
whereDoesntHave() |
Filters models that do not have any related models matching a specific condition. |
orWhereDoesntHave() |
Adds an OR condition to filter models that do not have any related models matching a specific condition. |
whereRelation() |
Adds a condition on a related model’s column, similar to a where clause on the relation. |
orWhereRelation() |
Adds an OR condition on a related model’s column. |
whereBetweenRelation() |
Adds a WHERE BETWEEN condition on a related model’s column. |
orWhereBetweenRelation() |
Adds an OR WHERE BETWEEN condition on a related model’s column. |
whereInRelation() |
Adds a WHERE IN condition on a related model’s column. |
orWhereInRelation() |
Adds an OR WHERE IN condition on a related model’s column. |
whereNullRelation() |
Adds a WHERE IS NULL condition on a related model’s column. |
orWhereNullRelation() |
Adds an OR WHERE IS NULL condition on a related model’s column. |
whereNotNullRelation() |
Adds a WHERE IS NOT NULL condition on a related model’s column. |
orWhereNotNullRelation() |
Adds an OR WHERE IS NOT NULL condition on a related model’s column. |
whereDateRelation() |
Filters related model’s date column by a specific date. |
orWhereDateRelation() |
Adds an OR condition to filter related model’s date column by a specific date. |
whereMonthRelation() |
Filters related model’s date column by month. |
orWhereMonthRelation() |
Adds an OR condition to filter related model’s date column by month. |
whereYearRelation() |
Filters related model’s date column by year. |
orWhereYearRelation() |
Adds an OR condition to filter related model’s date column by year. |
whereHasRelation() |
Filters models where a relation exists and meets a condition (shorthand for nested where on relations). |
orWhereHasRelation() |
Adds an OR version of whereHasRelation() . |
whereDoesntHaveRelation() |
Filters models where the relation doesn’t exist or doesn’t meet the condition. |
orWhereDoesntHaveRelation() |
Adds an OR version of whereDoesntHaveRelation() . |
CRUD Functions
Function | Description |
---|---|
create() |
Inserts a single new record in the database based on the provided data (will return the last inserted id). |
batchCreate() |
Inserts a multiple new record in the database based on the provided data in one operation. |
patch() |
Updates a specific record by its primary key (ID) set at $primaryKey property in model. |
patchAll() |
Updates multiple existing records based on specified conditions in one operation. |
batchPatch() |
Updates a multiple existing record by using specific column/primarykey (does not required any where condition). |
destroy() |
Deletes a specific record by its primary key (ID) set at the $primaryKey property in the model. If soft delete is enabled ($softDelete = true), the record is not permanently removed but flagged as deleted by setting a deleted_at or $_deleted_at_field property to timestamp. |
destroyAll() |
Deletes multiple records based on specified conditions. If soft delete is enabled ($softDelete = true), the record is not permanently removed but flagged as deleted by setting a deleted_at or $_deleted_at_field property to timestamp. |
forceDestroy() |
Permanently deletes a specific record by its primary key (ID) set at the $primaryKey property in the model, bypassing the soft delete mechanism if it is enabled. This method removes the record entirely from the database. |
insertOrUpdate() |
Determines whether to insert or update a record based on given conditions. Similar to Laravel's updateOrInsert() . |
restore() |
Restores a soft-deleted record by removing the deleted_at timestamp, making the record active again. This method only applies to records that have been soft deleted (i.e., those with a non-null deleted_at timestamp). If soft deletes are enabled ($softDelete = true), the restore() function allows you to undo the soft delete and recover the record. |
toSqlPatch() |
Returns the SQL query string for updating data. |
toSqlCreate() |
Returns the SQL query string for inserting single data. |
toSqlDestroy() |
Returns the SQL query string for deleting single data. |
CRUD Validation Functions
Function | Description |
---|---|
skipValidation() |
Ignores all validation rules for inserts and updates. |
setValidationRules() |
Sets or overrides existing validation rules for the model on the fly. |
setCustomValidationRules() |
Adds or changes existing validation rules that are already set in the model. |
setCreateValidationRules() |
Adds or changes existing validation rules for create operation only. |
setPatchValidationRules() |
Adds or changes existing validation rules for update operation only. |
Security Functions
Function | Description |
---|---|
safeOutput() |
Escapes output to prevent XSS attacks. All data, including eager loaded and appended data, will be filtered. |
safeOutputWithException() |
Same as safeOutput() , but allows specific fields to be excluded from escaping. |
Helper Functions
Function | Description |
---|---|
toArray() |
Converts the result set to an array format (Default). |
toObject() |
Converts the result set to an object format. |
toJson() |
Converts the result set to JSON format. |
showColumnHidden() |
Displays hidden columns by removing the $hidden property temporarily. |
setColumnHidden() |
Dynamically sets columns to be hidden, similar to Laravel's $hidden model property. |
setAppends() |
Dynamically appends custom attributes to the result set, similar to Laravel's $appends model property. |