Skip to content
Jeff Johns edited this page Feb 4, 2014 · 3 revisions
Model Extends Table Path
Plain_Model CI_Model N/A /application/core/Plain_Model.php

This model is the base model that all other app models extend. It contains a bunch or core functionality and methods that all other models can access.

Loading the model

No need, the child models call it and the custom auto-loader, loads it. Just relax and know it is there ready for you.

Child Models

Since all children extend this model, all models have access to all the below methods and properties.

Properties

Property Visibility Default Value Description
$data_types Public array The data types to validate against.
$delimiter Protected string Delimiter used for CONCAT queries.
$dont_cache Protected boolean Set to true to not cache the query results, false to cache them.
$id_column Protected string The id column of the current table.
$read_method Protected string The read method to use after a record has been updated.
$sort Public string The current sort order.
$sort_options Public array The sort options that can be used (Used for API)
$table Public string The current database table to be used

Methods

__construct - Public

Called automatically which in turn calls the parent constructor. It also does the following:

  • Reflects on the child model loading it
  • Finds the child model name
  • Sets the current table name
  • Sets the current table id column name

count - Public

Returns back a count of rows specified in your where variable.

Arguments

Variable Type Default Required Description
$where string NULL No The where clause to use if any to extract a count from.
$join string NULL No If you need to add a join statement before the where clause, do that here.

Examples

// Find total accounts
$this->load->model('marks_model', 'marks');
$total_marks = $this->marks->count();

delete - Public

Used to mark records at inactive. Nothing is being deleted.

Arguments

Variable Type Default Required Description
$where mixed N/A Yes If numeric the where clause will be filled in with the id submitted and the correct column, otherwise it will just use the where clause you submitted.

Example

// Turn user_to_mark_id of 77 for user id of 44 inactive
$this->load->model('users_to_marks_model', 'user_marks');
$mark = $this->user_marks->delete("user_to_mark_id = '77' AND user_id = '44'");

if (isset($mark->active) && $mark->active == '0') {
    // Good to go
}

getCacheKey - Protected

Not used at this time.


getTotals - Public

Used to get the correct total count of marks for queries.

Arguments

Variable Type Default Required Description
$where string N/A Yes The where statement to use to get totals for.
$page integer 1 No The current page of records to return. Used as an offset in the LIMIT statement.
$limit integer 1 No The max records to return.
$data array array() No The current $data array to add onto and return.
$join string N/A No If using a join or joins, add it here.

Example

// Get Totals
$this->load->model('users_to_marks_model', 'user_marks');
$marks = $this->user_marks->readComplete($where, $this->limit, $page);
$marks = $this->user_marks->getTotals($where, $page, $this->limit, $marks);

read - Public

Used to read records from tables. It's built and can be extended to be pretty flexible. One thing to note here is if you submit a limit of 1 record (default) the object returned will just be that object, if you return more than 1 record, you will have multiple objects that you need to loop through. This way for fetching one record you never need to loop shit. Example will be below.

Arguments

Variable Type Default Required Description
$where mixed N/A Yes If numeric the where clause will be filled in with the id submitted and the correct column, otherwise it will just use the where clause you submitted.
$limit integer 1 No The max records to return.
$page integer 1 No The current page of records to return. Used as an offset in the LIMIT statement.

Example

// Reutrn user informatio for user id of 14
$this->load->model('users_model', 'users');
$user = $this->user->read(14);
if (isset($user->user_id)) {
    // Good to go
}
else {
    // No so much
}

removeCacheKey - Protected

Not used at this time.


sendException - Protected

Not used at this time.


stripSlashes - Protected

Automatically removes any slashes from data returned from database. Since we are using complex queries and not active record, results are not automatically stripped of slashes. Well now they are again.

Arguments

Variable Type Default Required Description
$result mixed N/A Yes Any array or object with the information you want to strip slashes from.

Example

// From with the self::read method
$result = self::stripSlashes($q->result());

update - Public

Used to update a record and return the updated object or an error.

Arguments

Variable Type Default Required Description
$where mixed N/A Yes If numeric the where clause will be filled in with the id submitted and the correct column, otherwise it will just use the where clause you submitted.
$options array array() Yes An associative array that contains the column names and the values for the record to be created. Array keys are the column names for each value.

Example

// Update the email address of account 14
$user = $this->user->update(14, array(
    'email'    => 'john@doe.org'
));

// Use the where statement
$user = $this->user->update("email = 'oldemail@site.com' AND active = '1'", array('email' => 'john@doe.org'));

if (isset($user->email)) {
    // good to go
}