Table Model. More...
Public Member Functions | |
__construct ($_name=NULL, $dateformat= '"d.m.Y - H:i"', $_enforce_field_types=TRUE) | |
__construct More... | |
initialize ($_dateformat="d.m.Y - H:i", $_enforce_field_types=FALSE) | |
initialize More... | |
is_valid_index ($testvar) | |
Check whether test var is a valid db index. More... | |
get_types () | |
Get types array. More... | |
get_fields () | |
Get fields array. More... | |
get_name () | |
Get name string. More... | |
is_valid_data ($data) | |
Check whether data array is valid db array. More... | |
is_valid_subdata ($data) | |
Check whether data array is valid db array subset. More... | |
get_data ($query_string=FALSE) | |
Get data from db. More... | |
complete_data ($subdata, $query_string=FALSE) | |
Complete sub-data array with data from db. More... | |
insert_data ($data=FALSE) | |
Insert data to db. More... | |
update_data ($data=FALSE) | |
Update data in db. More... | |
delete_data ($ids=FALSE) | |
Delete data in db. More... | |
Data Fields | |
$results = array() | |
$insert_id = FALSE | |
Protected Attributes | |
$enforce_field_types = TRUE | |
$dateformat = "Y.m.d - H:i:s" | |
$name | |
$types = array() | |
$fields = array() | |
Table Model.
The model can be used by all higher models and contains basic info on the tables used and it writes and reads all tables and contains functions therefore. Results are stored in the "results" array and structured as described by the "*_fields" and "*_types" arrays. Since the table name can only be set upon creation, class instances are unrevocably linked to the given table, so is the reference to the db instance of the model it works for.
Copyright (c) 2015 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
__construct | ( | $_name = NULL , |
|
$dateformat = '"d.m.Y - H:i"' , |
|||
$_enforce_field_types = TRUE |
|||
) |
__construct
Inits some vars needed by class, two are required: the table's name to act upon and the ci reference.
_name | string: table name of the table to act upon |
dateformat | string: date format to format dates in, e.g. "d.m.Y - H:i" |
_enforce_field_types | bool: setting whether to enforce field types in PHP by cast |
complete_data | ( | $subdata, | |
$query_string = FALSE |
|||
) |
Complete sub-data array with data from db.
Function that either executes db->get() if "query_string" is FALSE (executing the currently set query) OR executes db->query($query_string) if "query_string" is set. The "query_string" or preset query should select exactly one table row, the one to be used for completion of the given subdata array. If more than one row is returned, only the first row is used. The function gets the complete result array of the selected row from the table and completes the given data array with its values and keys: it inserts (only) missing keys with its values. The "data" param has to be a valid subdata array and it is checked therefore. Returns array if successful and empty array if no results and FALSE on error.
Use the get_types function to build a valid data array easily.
subdata | array: associative array with db field names as keys and values as values as data in results var. Checked if valid subdata. |
query_string | string: query string to be executed using the CI "query" function instead of just using the CI "get()" function. When FALSE "get()" is used. |
delete_data | ( | $ids = FALSE | ) |
Delete data in db.
Function that executes db->delete($this->name) either without id array if "ids" param is FALSE (executing the (pre)set query) OR executes db->delete($this->name, $ids) if "ids" param is set. If handed over, the "ids" param has to be a valid subdata array and it is checked therefore - more specifically it'll mostly contain only the ids to delete: array('id_field_name' => $id1, 'id_field_name' => $id2, ...)
Returns TRUE if delete successful and FALSE if otherwise.
Use the get_types function to build a valid data array easily.
ids | array: associative array with db field names as keys and values as values as data in results var. Checked if valid subdata. |
get_data | ( | $query_string = FALSE | ) |
Get data from db.
Function that either executes db->get() if "query_string" is FALSE (executing the currently set query) OR executes db->query($query_string) if "query_string" is given and returns data as "result_array" as well as puts them in the results var.
Use it to retrieve data (rows) from db. If you dont supply a query string set query specifics before call using CI functions like db->select, db->from, db->where,... Use the get_types function to find out about the table structure.
Returns FALSE on error, array otherwise
query_string | string: query string to be executed using the CI "query" function instead of just using the CI "get()" function. When FALSE "get()" is used. |
get_fields | ( | ) |
get_name | ( | ) |
get_types | ( | ) |
initialize | ( | $_dateformat = "d.m.Y - H:i" , |
|
$_enforce_field_types = FALSE |
|||
) |
initialize
Initializes values for this class. Resets the results array to maintain consistency of field types.
_dateformat | string: format to display dates in |
_enforce_field_types | bool: setting whether to enforce field types in PHP by cast |
insert_data | ( | $data = FALSE | ) |
Insert data to db.
Function that executes db->insert($this->name) either without data array if "data" param is FALSE (executing the (pre)set query) OR executes db->insert($this->name, $data) if "data" param is set. If handed over, the "data" param has to be a valid subdata array and it is checked therefore. Fields missing have to have default values in the db. Also sets insert_id, sets it to FALSE on failure. Returns TRUE if insert successful and FALSE otherwise.
Use the get_types function to build a valid data array easily.
data | array: associative array with db field names as keys and values as values as data in results var. Checked if valid subdata. |
is_valid_data | ( | $data | ) |
Check whether data array is valid db array.
Function checking whether the supplied data array is equal to the ones returned from the given table this base model is connected to. Therefore it has to be an associative array containing all the same table field names as keys as the result arrays returned by CI.
data | array: associative array with table field names as keys to be checked if it is equal to result arrays returned by CI from this model's table |
is_valid_index | ( | $testvar | ) |
Check whether test var is a valid db index.
Function checking whether the supplied var is a valid db index. Therefore it has to be (convertible to) a positive integer or zero.
testvar | int: var to check if it is valid index number |
is_valid_subdata | ( | $data | ) |
Check whether data array is valid db array subset.
Function checking whether the supplied data array is a partial data from the given table this base model is connected to. Therefore it has to be an associative array containing one or more table field names as keys but no keys that are not table field names. Since it does not have to contain ALL the data fields it is only a partial- or subdata array.
data | array: associative array with table field names as keys to be checked if it is a subdata from this model's table |
update_data | ( | $data = FALSE | ) |
Update data in db.
Function that executes db->update($this->name) either without data array if "data" param is FALSE (executing the (pre)set query) OR executes db->update($this->name, $data) if "data" param is set. If handed over, the "data" param has to be a valid subdata array and it is checked therefore. NOTE: in both cases a correct "db->where" query HAS TO BE SET BEFORE CALLING! The row to update is specified by this where query and NOT by the "data" array! Returns TRUE if update successful and FALSE if otherwise.
Use the get_types function to build a valid data array easily.
data | array: associative array with db field names as keys and values as values as data in results var. Checked if valid subdata. |