-
Notifications
You must be signed in to change notification settings - Fork 194
Model Plain
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.
No need, the child models call it and the custom auto-loader, loads it. Just relax and know it is there ready for you.
Since all children extend this model, all models have access to all the below methods and 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 |
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
Returns back a count of rows specified in your where variable.
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. |
// Find total accounts
$this->load->model('marks_model', 'marks');
$total_marks = $this->marks->count();
Used to mark records at inactive. Nothing is being deleted.
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. |
// 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
}
Not used at this time.
Used to get the correct total count of marks for queries.
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. |
// 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);
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.
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. |
// 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
}
Not used at this time.
Not used at this time.
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.
Variable | Type | Default | Required | Description |
---|---|---|---|---|
$result | mixed | N/A | Yes | Any array or object with the information you want to strip slashes from. |
// From with the self::read method
$result = self::stripSlashes($q->result());
Used to update a record and return the updated object or an error.
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. |
// Update the email address of account 14
$user = $this->user->update(14, array(
'email' => '[email protected]'
));
// Use the where statement
$user = $this->user->update("email = '[email protected]' AND active = '1'", array('email' => '[email protected]'));
if (isset($user->email)) {
// good to go
}