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π« 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
Query Functions
Function | Description |
---|---|
rawQuery() |
Execute raw SQL queries directly. Useful for complex queries not supported by active record. |
table() |
Specifies the database table for the query. |
select() |
Defines the columns to retrieve in a query. Similar to CodeIgniterβs select() . |
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() . |
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() . |
chunk() |
Process data in chunks to handle large datasets efficiently. Similar to Laravel's chunk() . |
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). |
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. |
toSql() |
Returns the SQL query string (without eager loading query). |
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. |
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 avoid the N+1 query issue. Similar to Laravel's with() . |
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. |