PHPackages                             slaxweb/ci-basemodel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. slaxweb/ci-basemodel

ActiveLibrary

slaxweb/ci-basemodel
====================

BaseModel for CodeIgniter

0.4.2(11y ago)81903[4 issues](https://github.com/slax0rr/BaseModel/issues)MITPHPPHP &gt;=5.3.0

Since Sep 5Pushed 10y ago4 watchersCompare

[ Source](https://github.com/slax0rr/BaseModel)[ Packagist](https://packagist.org/packages/slaxweb/ci-basemodel)[ RSS](/packages/slaxweb-ci-basemodel/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (2)Versions (18)Used By (0)

BaseModel
=========

[](#basemodel)

[![Build Status](https://camo.githubusercontent.com/552fba5f650ba24469f8ec2fd199250831f5eea93d14dbc935aacc5bf03c8d1f/68747470733a2f2f7472617669732d63692e6f72672f736c61783072722f426173654d6f64656c2e7376673f6272616e63683d6665617475726525324677686572654275696c646572)](https://travis-ci.org/slax0rr/BaseModel)

Base model for CodeIgniter, helps you with your database operations in the model. it auto guesses the table name from the model class name, saves you the hassle of soft deletes and more. BaseModel is also used by [BaseController](https://github.com/slax0rr/BaseModel).

The idea for the BaseModel came from Jamie Rumbelows [base model](https://github.com/jamierumbelow/codeigniter-base-model), with some additions and changes. If you run into issues or have questions/ideas, please submit a ticket here on [GitHub](https://github.com/slax0rr/BaseModel/issues).

This is still in development phase, but is production ready. It has only been tested with mySQL, and at the time will probably work only with mySQL, although other drivers support has been added, but is EXPERIMENTAL.

Table of contents
=================

[](#table-of-contents)

- [BaseModel](https://github.com/slax0rr/BaseModel/blob/master/README.md#basemodel)
- [Table of contents](https://github.com/slax0rr/BaseModel/blob/master/README.md#table-of-contents)
- [Install](https://github.com/slax0rr/BaseModel/blob/master/README.md#install)
    - [Use the BaseModel](https://github.com/slax0rr/BaseModel/blob/master/README.md#use-the-basemodel)
- [Properties](https://github.com/slax0rr/BaseModel/blob/master/README.md#properties)
- [Table name](https://github.com/slax0rr/BaseModel/blob/master/README.md#table-name)
    - [Guessing the table name](https://github.com/slax0rr/BaseModel/blob/master/README.md#guessing-the-table-name)
- [Config constants](https://github.com/slax0rr/BaseModel/blob/master/README.md#config-constants)
- [Database operations](https://github.com/slax0rr/BaseModel/blob/master/README.md#database-operations)
    - [Inserting data](https://github.com/slax0rr/BaseModel/blob/master/README.md#inserting-data)
    - [Getting data](https://github.com/slax0rr/BaseModel/blob/master/README.md#getting-data)
    - [Updating data](https://github.com/slax0rr/BaseModel/blob/master/README.md#updating-data)
    - [Deleting data](https://github.com/slax0rr/BaseModel/blob/master/README.md#deleting-data)
- [Joining tables](https://github.com/slax0rr/BaseModel/blob/master/README.md#joining-tables)
- [Building WHERE statements](https://github.com/slax0rr/BaseModel/blob/master/README.md#building-where-statements)
    - [Conditional operators](https://github.com/slax0rr/BaseModel/blob/master/README.md#conditional-operators)
    - [Comparison operators](https://github.com/slax0rr/BaseModel/blob/master/README.md#comparison-operators)
    - [Prefix columns with table names](https://github.com/slax0rr/BaseModel/blob/master/README.md#prefix-columns-with-table-names)
    - [Grouping WHERE expressions](https://github.com/slax0rr/BaseModel/blob/master/README.md#grouping-where-expressions)
    - [DEPRECATED - Conditional operators](https://github.com/slax0rr/BaseModel/blob/master/README.md#deprecated---conditional-operators)
    - [DEPRECATED - Comparison operators](https://github.com/slax0rr/BaseModel/blob/master/README.md#deprecated---comparison-operators)
    - [DEPRECATED - Grouping WHERE expressions](https://github.com/slax0rr/BaseModel/blob/master/README.md#deprecated---grouping-where-expressions)
- [SQL clauses](https://github.com/slax0rr/BaseModel/blob/master/README.md#sql-clauses)
    - [GROUP BY](https://github.com/slax0rr/BaseModel/blob/master/README.md#group-by)
    - [ORDER BY](https://github.com/slax0rr/BaseModel/blob/master/README.md#order-by)
    - [LIMIT](https://github.com/slax0rr/BaseModel/blob/master/README.md#limit)
- [Validation](https://github.com/slax0rr/BaseModel/blob/master/README.md#validation)
    - [Run validation manually](https://github.com/slax0rr/BaseModel/blob/master/README.md#run-validation-manually)
    - [Skipping validation](https://github.com/slax0rr/BaseModel/blob/master/README.md#skipping-validation)
- [Queries on soft deleted rows](https://github.com/slax0rr/BaseModel/blob/master/README.md#queries-on-soft-deleted-rows)
- [Results](https://github.com/slax0rr/BaseModel/blob/master/README.md#results)
    - [Getting column data](https://github.com/slax0rr/BaseModel/blob/master/README.md#getting-column-data)
    - [Number of rows](https://github.com/slax0rr/BaseModel/blob/master/README.md#number-of-rows)
    - [Get all rows](https://github.com/slax0rr/BaseModel/blob/master/README.md#get-all-rows)
    - [Traversing through rows](https://github.com/slax0rr/BaseModel/blob/master/README.md#traversing-through-rows)
- [Errors](https://github.com/slax0rr/BaseModel/blob/master/README.md#errors)
    - [Initialization of the Error class](https://github.com/slax0rr/BaseModel/blob/master/README.md#initialization-of-the-error-class)
    - [Add an error](https://github.com/slax0rr/BaseModel/blob/master/README.md#add-an-error)
    - [Has errors and count errors](https://github.com/slax0rr/BaseModel/blob/master/README.md#has-errors-and-count-errors)
    - [Getting errors](https://github.com/slax0rr/BaseModel/blob/master/README.md#getting-errors)
    - [Traversing through errors](https://github.com/slax0rr/BaseModel/blob/master/README.md#traversing-through-errors)
- [Thank you!](https://github.com/slax0rr/BaseModel/blob/master/README.md#thank-you)
- [ChangeLog](https://github.com/slax0rr/BaseModel/blob/master/README.md#changelog)

Install
=======

[](#install)

The easiest way to install at the moment is to use [composer](https://getcomposer.org/), or by installing the [BaseController](https://github.com/slax0rr/BaseModel), version 0.2+, which has BaseModel listed as requirement. Simply create composer.json file in your project root:

```
{
  "require": {
    "slaxweb/ci-basemodel": "~0.3"
  }
}

```

Then run **composer.phar install**. When finished, enable composer autoloader in *application/config/config.php* configuration file.

Use the BaseModel
-----------------

[](#use-the-basemodel)

The BaseModel is meant to be extended by your models, so, instead of extending from *CI\_Model*, extend from **\\SlaxWeb\\BaseModel\\Model**:

```
class Some_model extends \SlaxWeb\BaseModel\Model
```

With this, your BaseModel is ready to use.

As an alternative, you can also extend from *MY\_Model* and have MY\_Model extending BaseModel.

Properties
==========

[](#properties)

BaseModel provides you with the next set of properties you can set:

- **table** - The table of the model, although, BaseModel does auto-guess the table name from its own name.
- **tablePrefix** - The table prefix, if you use a special prefix, per model
- **primaryKey** - Defaults to "id", if you use something else as primary key, set it here
- **keyType** - Type of the key, Auto-Increment(default), UUID key, or a specific PHP or custom function you use to generate a primary key
- **keyFunc** - If you use a function to generate a primary key, assign it here, accepts type callable
- **keyFuncParams** - Parameters used by keyFunc function
- **softDelete** - Use soft delete, "hard" delete is default. Has 3 options, Soft delete status column, soft delete mark column, hard deletes
- **deleteCol** - If using soft delete mark column, define it here, default is "deleted"
- **statusCol** - If using soft delete status column, define the column here, default is "status"
- **deleteStatus** - Set the deleted status value for status column, default is "deleted"
- **rules** - Validation rules
- **where** - A custom where string
- **whereBinds** - If using a custom where string, and you want to bind your parameters into the where string, you can add those parameters to thir property as an array

At the moment BaseModel provides a single callback **beforeInit**, this callback is invoked before initialization of the BaseModel.

Table name
==========

[](#table-name)

There are 3 ways of defining a table name, first is to define a public property **table** in your model, passing the table name at construction time to the BaseModel *\_\_construct* method, or just let BaseModel try and figure out your table name.

Guessing the table name
-----------------------

[](#guessing-the-table-name)

When guessing the table name, BaseModel takes the class name of your model, removes the *\_model* or *\_m* suffixes, pluralizes the remainder, and converts everything to lower case letters. So in example **User\_model** becomes **users**.

Config constants
================

[](#config-constants)

BaseModel provides a \\SlaxWeb\\BaseModal\\Constants class for configuration of soft deletes and primary key types. To use in your controller, the best way is to *use* the class before declaration of your model.

```
