MY_Model =============== Toolwatch Base Model The Base model implements standard CRUD functions that can be used and overriden by module models. This helps to maintain a standard interface to program to, and makes module creation faster. * Class name: MY_Model * Namespace: * Parent class: [CI_Model](CI_Model.md) Properties ---------- ### $error public string $error = '' Stores custom errors that can be used in UI error reporting. * Visibility: **public** ### $table_name public string $table_name = '' The name of the db table this model primarily uses. * Visibility: **public** ### $key protected string $key = 'id' The primary key of the table. Used as the 'id' throughout. * Visibility: **protected** ### $created_field protected string $created_field = 'created_on' Field name to use for the created time column in the DB table if $set_created is enabled * Visibility: **protected** ### $modified_field protected string $modified_field = 'modified_on' Field name to use for the modified time column in the DB table if $set_modified is enabled * Visibility: **protected** ### $deleted_field protected string $deleted_field = 'deleted' Field name to use for the deleted column in the DB table if $soft_deletes is enabled * Visibility: **protected** ### $set_created protected boolean $set_created = false Whether or not to auto-fill the $created_field on inserts. * Visibility: **protected** ### $set_modified protected boolean $set_modified = false Whether or not to auto-fill the $modified_field on updates. * Visibility: **protected** ### $date_format protected string $date_format = 'datetime' The type of date/time field used for $created_field and $modified_field Valid values are 'int', 'datetime', 'date' * Visibility: **protected** ### $soft_deletes protected boolean $soft_deletes = false If false, the delete() method will perform a delete of that row. If true, the value in $deleted_field will be set to 1. * Visibility: **protected** ### $selects protected string $selects = '' Stores any selects here for use by the find* functions. * Visibility: **protected** ### $escape protected boolean $escape = true If false, the select() method will not try to protect your field or table names with backticks. This is useful if you need a compound select statement. * Visibility: **protected** ### $db_con protected mixed $db_con = '' DB Connection details (string or array) * Visibility: **protected** ### $before_insert protected array $before_insert = array() Observer Arrays Each array can contain the names of callback functions within the extending model which will be called during each event. $before_insert = array('set_created', 'validate_fields'); $before_insert contains the names of callback functions within the extending model which will be called before the insert method. * Visibility: **protected** ### $after_insert protected array $after_insert = array() Contains the names ozf callback functions within the extending model which will be called after the insert method * Visibility: **protected** ### $before_update protected array $before_update = array() Contains the names of callback functions within the extending model which will be called before the update method * Visibility: **protected** ### $after_update protected array $after_update = array() Contains the names of callback functions within the extending model which will be called after the update method * Visibility: **protected** ### $before_find protected array $before_find = array() Contains the names of callback functions within the extending model which will be called before the find method * Visibility: **protected** ### $after_find protected array $after_find = array() Contains the names of callback functions within the extending model which will be called after the find method * Visibility: **protected** ### $before_union_all protected array $before_union_all = array() Contains the names of callback functions within the extending model which will be called before the union_all method * Visibility: **protected** ### $after_union_all protected array $after_union_all = array() Contains the names of callback functions within the extending model which will be called after the union_all method * Visibility: **protected** ### $before_delete protected array $before_delete = array() Contains the names of callback functions within the extending model which will be called before the delete method * Visibility: **protected** ### $after_delete protected array $after_delete = array() Contains the names of callback functions within the extending model which will be called after the delete method * Visibility: **protected** ### $empty_validation_rules protected array $empty_validation_rules = array() Contains the names of callback functions within the extending model which will be called if $validation_rules is empty (or not an array) when requested via the get_validation_rules() method. Note: These methods should not add $insert_validation_rules, as they are added to the $validation_rules after these functions return. * Visibility: **protected** ### $protected_attributes protected array $protected_attributes = array() Protected, non-modifiable attributes * Visibility: **protected** ### $return_type protected string $return_type = 'object' By default, we return items as objects. You can change this for the entire class by setting this value to 'array' instead of 'object'. Alternatively, you can do it on a per-instance basis using the 'as_array()' and 'as_object()' methods. * Visibility: **protected** ### $temp_return_type protected string $temp_return_type = null Holds the return type temporarily when using the as_array() and as_object() methods * Visibility: **protected** ### $validation_rules protected Array $validation_rules = array() * Visibility: **protected** ### $insert_validation_rules protected Array $insert_validation_rules = array() * Visibility: **protected** ### $skip_validation protected boolean $skip_validation = false * Visibility: **protected** ### $return_insert_id protected boolean $return_insert_id = true * Visibility: **protected** ### $field_info protected Array $field_info = array() * Visibility: **protected** ### $pending_selects protected array $pending_selects = array() This array will be populated with selects and mainly use for union based query. * Visibility: **protected** Methods ------- ### __construct void CI_Model::__construct() Class constructor * Visibility: **public** * This method is defined by [CI_Model](CI_Model.md) ### find mixed MY_Model::find(string $id) Search for a single row in the database. * Visibility: **public** #### Arguments * $id **string** - <p>The primary key of the record to search for.</p> ### find_all mixed MY_Model::find_all() Returns all records in the table. By default, there is no 'where' clause, but the where clause can be set by using either CodeIgniter's Active Record functions before calling this function, or through method chaining with the model's where() method. * Visibility: **public** ### find_union_all mixed MY_Model::find_union_all() Returns all of unions, the $pending_selects value has to be populated trhought the union conveniant method * Visibility: **public** ### find_all_by boolean|mixed MY_Model::find_all_by(mixed $field, mixed $value, string $type) A convenience method that combines a where() and find_all() call into a single call. * Visibility: **public** #### Arguments * $field **mixed** - <p>The table field to search in.</p> * $value **mixed** - <p>The value that field should be.</p> * $type **string** - <p>The type of where clause to create: 'and' or 'or'.</p> ### find_by boolean|mixed MY_Model::find_by(string $field, string $value, string $type) Returns the first result that matches the field/values passed. * Visibility: **public** #### Arguments * $field **string** - <p>Either a string or an array of fields to match against. If an array is passed it, the $value parameter is ignored since the array is expected to have key/value pairs in it.</p> * $value **string** - <p>The value to match on the $field. Only used when $field is a string.</p> * $type **string** - <p>The type of where clause to create: 'and' or 'or'.</p> ### insert boolean|mixed MY_Model::insert(array $data) Insert a row of data into the database. * Visibility: **public** #### Arguments * $data **array** - <p>an array of key/value pairs to insert.</p> ### insert_batch boolean MY_Model::insert_batch(array $data) Perform a batch insert of data into the database. * Visibility: **public** #### Arguments * $data **array** - <p>an array of key/value pairs to insert.</p> ### update_table boolean MY_Model::update_table(mixed $where, array $data, $table) Update an existing row in the database. * Visibility: **public** #### Arguments * $where **mixed** - <p>The primary_key value of the row to update, or an array to use for the where clause.</p> * $data **array** - <p>An array of key/value pairs to update.</p> * $table **mixed** ### update boolean MY_Model::update(mixed $where, array $data) Update an existing row in the database. * Visibility: **public** #### Arguments * $where **mixed** - <p>The primary_key value of the row to update, or an array to use for the where clause.</p> * $data **array** - <p>An array of key/value pairs to update.</p> ### increment boolean MY_Model::increment(mixed $where, $field) Increment a field. Mostly use for views, likes and so on. * Visibility: **public** #### Arguments * $where **mixed** - <p>The primary_key value of the row to update, or an array to use for the where clause.</p> * $field **mixed** ### decrement boolean MY_Model::decrement(mixed $where, $field) Decrement a field. Mostly use for views, likes and so on. * Visibility: **public** #### Arguments * $where **mixed** - <p>The primary_key value of the row to update, or an array to use for the where clause.</p> * $field **mixed** ### update_where boolean MY_Model::update_where(string $field, string $value, array $data) A convenience method that allows use of any field/value pair as the 'where' portion of an update. * Visibility: **public** #### Arguments * $field **string** - <p>The field to match on.</p> * $value **string** - <p>The value to search the $field for.</p> * $data **array** - <p>An array of key/value pairs to update.</p> ### update_batch boolean MY_Model::update_batch(array $data, string $index) Updates a batch of existing rows in the database. * Visibility: **public** #### Arguments * $data **array** - <p>An array of key/value pairs to update.</p> * $index **string** - <p>The name of the db column to use as the where key</p> ### delete boolean MY_Model::delete(mixed $id) Delete the record with the specified primary key value. If $this->soft_deletes is true, it will attempt to set $this->deleted_field on the specified record to '1', to allow the data to remain in the database. * Visibility: **public** #### Arguments * $id **mixed** - <p>The primary_key value to match against.</p> ### delete_where boolean MY_Model::delete_where($where) Delete the record(s) specified by the given field/value pair(s). Performs a delete using the field/value pair(s) as the 'where' portion of the delete statement. If $this->soft_deletes is true, it will attempt to set $this->deleted_field on the specified record(s) to '1', to allow the data to remain in the database. * Visibility: **public** #### Arguments * $where **mixed** ### is_unique boolean MY_Model::is_unique(string $field, string $value) Check whether a field/value pair exists within the table. * Visibility: **public** #### Arguments * $field **string** - <p>The name of the field to search</p> * $value **string** - <p>The value to match $field against.</p> ### count_by boolean|integer MY_Model::count_by(string $field, string $value) Return the number of elements that match the field/value pair. * Visibility: **public** #### Arguments * $field **string** - <p>The field to search for.</p> * $value **string** - <p>The value to match $field against.</p> ### get_field boolean|mixed MY_Model::get_field(mixed $id, string $field) A convenience method to return only a single field of the specified row. * Visibility: **public** #### Arguments * $id **mixed** - <p>The primary_key value to match against.</p> * $field **string** - <p>The field to search for.</p> ### format_dropdown array MY_Model::format_dropdown() A convenience method to return options for form dropdown menus. Can pass either Key ID and Label Table names or Just Label Table name. * Visibility: **public** ### where \FF_Model MY_Model::where(mixed $field, string $value, $escape) Sets the where portion of the query in a chainable format. * Visibility: **public** #### Arguments * $field **mixed** - <p>The field to search the db on. Can be either a string with the field name to search, or an associative array of key/value pairs.</p> * $value **string** - <p>The value to match the field against. If $field is an array, this value is ignored.</p> * $escape **mixed** ### order_by \FF_Model MY_Model::order_by(mixed $field, string $order) Inserts a chainable order_by method from either a string or an array of field/order combinations. If the $field value is an array, it should look like: array( 'field1' => 'asc', 'field2' => 'desc' ); * Visibility: **public** #### Arguments * $field **mixed** - <p>The field to order the results by, or an array of field/order pairs.</p> * $order **string** - <p>The direction to order the results ('asc' or 'desc')</p> ### soft_delete \FF_Model MY_Model::soft_delete(boolean $val) Set the value of the soft deletes flag. $this->my_model->soft_delete(true)->delete($id); * Visibility: **public** #### Arguments * $val **boolean** - <p>If true, will temporarily use soft_deletes.</p> ### as_array \FF_Model MY_Model::as_array() Temporarily set the return type to an array. * Visibility: **public** ### as_object \FF_Model MY_Model::as_object() Temporarily sets the return type to an object. * Visibility: **public** ### as_json \FF_Model MY_Model::as_json() Temporarily sets the object return to a json object. * Visibility: **public** ### return_insert_id \FF_Model MY_Model::return_insert_id(Bool $return) Sets the value of the return_insert_id flag * Visibility: **public** #### Arguments * $return **Bool** - <p>(optional) whether insert will return the ID</p> ### skip_validation \FF_Model MY_Model::skip_validation(Bool $skip) Sets the value of the skip_validation flag * Visibility: **public** #### Arguments * $skip **Bool** - <p>(optional) whether to skip validation in the model</p> ### created_on array MY_Model::created_on(array $row) Sets the created date for the row Uses the current date/time, the model's date_format, and places the value in the model's created_field. Will not overwrite existing. * Visibility: **public** #### Arguments * $row **array** - <p>The array of data to be inserted</p> ### modified_on array MY_Model::modified_on(array $row) Sets the modified date for the row Uses the current date/time, the model's date_format, and places the value in the model's modified_field. Will not overwrite existing. * Visibility: **public** #### Arguments * $row **array** - <p>The array of data to be inserted</p> ### trigger mixed MY_Model::trigger(string $event, mixed $data) Triggers a model-specific event and calls each of its observers. * Visibility: **public** #### Arguments * $event **string** - <p>The name of the event to trigger</p> * $data **mixed** - <p>The data to be passed to the callback functions.</p> ### get_validation_rules array MY_Model::get_validation_rules(String $type) Get the validation rules for the model * Visibility: **public** #### Arguments * $type **String** - <p>The type of validation rules to retrieve: 'update' or 'insert'. If 'insert', appends rules set in $insert_validation_rules.</p> ### validate \array/bool MY_Model::validate(array $data, string $type) Validate the $data passed into it Uses the form_validation rules setup in $this->validation_rules. If $type == 'insert', any additional rules in $insert_validation_rules for that field will be added to the rules. * Visibility: **public** #### Arguments * $data **array** - <p>An array of data to validate</p> * $type **string** - <p>Either 'update' or 'insert'.</p> ### protect_attributes mixed MY_Model::protect_attributes(\object/array $row) Protect attributes by removing them from $row array. Useful for removing the primary key, or submit button names if $_POST is thrown at the model. * Visibility: **public** #### Arguments * $row **object/array** - <p>The value pair item to remove.</p> ### set_date integer|null|string MY_Model::set_date(mixed $user_date) A utility function to allow child models to use the type of date/time format that they prefer. This is primarily used for setting created_on and modified_on values, but can be used for other fields as well. The available time formats are: * 'int' - Stores the date as an integer timestamp. * 'datetime' - Stores the date and time in the SQL datetime format. * 'date' - Stores teh date (only) in the SQL date format. * Visibility: **protected** #### Arguments * $user_date **mixed** - <p>An optional PHP timestamp to be converted.</p> ### _return_type string MY_Model::_return_type($multi) Return the method name for the current return type * Visibility: **protected** #### Arguments * $multi **mixed** ### get_db_error_message string MY_Model::get_db_error_message() Retrieve error messages from the database * Visibility: **protected** ### set_table void MY_Model::set_table(string $table) Allows setting the table to use for all methods during runtime. * Visibility: **public** #### Arguments * $table **string** - <p>The table name to use (do not include the prefix!)</p> ### get_table string MY_Model::get_table() Get the table name * Visibility: **public** ### get_key string MY_Model::get_key() Get the table's primary key * Visibility: **public** ### get_created_by_field String MY_Model::get_created_by_field() Get the name of the created by field * Visibility: **public** ### get_created_field String MY_Model::get_created_field() Get the name of the created field * Visibility: **public** ### get_deleted_field String MY_Model::get_deleted_field() Get the name of the deleted field * Visibility: **public** ### get_deleted_by_field String MY_Model::get_deleted_by_field() Get the name of the deleted by field * Visibility: **public** ### get_field_info array MY_Model::get_field_info() Get the metadata for the model's database fields Returns the model's database field metadata stored in $this->field_info if set, else it tries to retrieve the metadata from $this->db->field_data($this->table_name); * Visibility: **public** ### get_modified_by_field String MY_Model::get_modified_by_field() Get the name of the modified by field * Visibility: **public** ### get_modified_field String MY_Model::get_modified_field() Get the name of the modified field * Visibility: **public** ### set_date_format boolean MY_Model::set_date_format(string $format) Sets the date_format to use for setting created_on and modified_on values. * Visibility: **public** #### Arguments * $format **string** - <p>String describing format. Valid values are: 'int', 'datetime', 'date'</p> ### set_modified boolean MY_Model::set_modified(boolean $modified) Sets whether to auto-create modified_on dates in the update method. * Visibility: **public** #### Arguments * $modified **boolean** ### set_soft_deletes boolean MY_Model::set_soft_deletes(boolean $soft) Sets whether soft deletes are used by the delete method. * Visibility: **public** #### Arguments * $soft **boolean** ### logit mixed MY_Model::logit(string $message, string $level) Logs an error to the Console (if loaded) and to the log files. * Visibility: **protected** #### Arguments * $message **string** - <p>The string to write to the logs.</p> * $level **string** - <p>The log level, as per CI log_message method.</p> ### prep_data Array MY_Model::prep_data(Array $post_data) Extracts the model's fields (except the key and those handled by Observers) from the $post_data and returns an array of name => value pairs * Visibility: **public** #### Arguments * $post_data **Array** - <p>The post data, usually $this->input->post() when called from the controller</p> ### raw_sql mixed MY_Model::raw_sql($sql) * Visibility: **public** #### Arguments * $sql **mixed** ### select mixed MY_Model::select($select, $escape) * Visibility: **public** #### Arguments * $select **mixed** * $escape **mixed** ### select_max mixed MY_Model::select_max($select, $alias) * Visibility: **public** #### Arguments * $select **mixed** * $alias **mixed** ### select_min mixed MY_Model::select_min($select, $alias) * Visibility: **public** #### Arguments * $select **mixed** * $alias **mixed** ### select_avg mixed MY_Model::select_avg($select, $alias) * Visibility: **public** #### Arguments * $select **mixed** * $alias **mixed** ### select_sum mixed MY_Model::select_sum($select, $alias) * Visibility: **public** #### Arguments * $select **mixed** * $alias **mixed** ### distinct mixed MY_Model::distinct($val) * Visibility: **public** #### Arguments * $val **mixed** ### from mixed MY_Model::from($from) * Visibility: **public** #### Arguments * $from **mixed** ### join mixed MY_Model::join($table, $cond, $type) * Visibility: **public** #### Arguments * $table **mixed** * $cond **mixed** * $type **mixed** ### union mixed MY_Model::union($select) * Visibility: **public** #### Arguments * $select **mixed** ### or_where mixed MY_Model::or_where($key, $value, $escape) * Visibility: **public** #### Arguments * $key **mixed** * $value **mixed** * $escape **mixed** ### where_in mixed MY_Model::where_in($key, $values) * Visibility: **public** #### Arguments * $key **mixed** * $values **mixed** ### or_where_in mixed MY_Model::or_where_in($key, $values) * Visibility: **public** #### Arguments * $key **mixed** * $values **mixed** ### where_not_in mixed MY_Model::where_not_in($key, $values) * Visibility: **public** #### Arguments * $key **mixed** * $values **mixed** ### or_where_not_in mixed MY_Model::or_where_not_in($key, $values) * Visibility: **public** #### Arguments * $key **mixed** * $values **mixed** ### like mixed MY_Model::like($field, $match, $side) * Visibility: **public** #### Arguments * $field **mixed** * $match **mixed** * $side **mixed** ### nto_like mixed MY_Model::nto_like($field, $match, $side) * Visibility: **public** #### Arguments * $field **mixed** * $match **mixed** * $side **mixed** ### or_like mixed MY_Model::or_like($field, $match, $side) * Visibility: **public** #### Arguments * $field **mixed** * $match **mixed** * $side **mixed** ### or_not_like mixed MY_Model::or_not_like($field, $match, $side) * Visibility: **public** #### Arguments * $field **mixed** * $match **mixed** * $side **mixed** ### group_by mixed MY_Model::group_by($by) * Visibility: **public** #### Arguments * $by **mixed** ### having mixed MY_Model::having($key, $value, $escape) * Visibility: **public** #### Arguments * $key **mixed** * $value **mixed** * $escape **mixed** ### or_having mixed MY_Model::or_having($key, $value, $escape) * Visibility: **public** #### Arguments * $key **mixed** * $value **mixed** * $escape **mixed** ### limit mixed MY_Model::limit($value, $offset) * Visibility: **public** #### Arguments * $value **mixed** * $offset **mixed** ### offset mixed MY_Model::offset($offset) * Visibility: **public** #### Arguments * $offset **mixed** ### set mixed MY_Model::set($key, $value, $escape) * Visibility: **public** #### Arguments * $key **mixed** * $value **mixed** * $escape **mixed** ### affected_rows mixed MY_Model::affected_rows() * Visibility: **public** ### last_query mixed MY_Model::last_query() * Visibility: **public** ### truncate mixed MY_Model::truncate() * Visibility: **public** ### inserted_id mixed MY_Model::inserted_id() * Visibility: **public** ### __get mixed CI_Model::__get(string $key) __get magic Allows models to access CI's loaded classes using the same syntax as controllers. * Visibility: **public** * This method is defined by [CI_Model](CI_Model.md) #### Arguments * $key **string**