CAFPE Docs
  • Class

Classes

  • Article_model
  • Articles
  • Auth
  • Auth_controller
  • Bcrypt
  • Hierarchical_controllers
  • Ion_auth
  • Ion_auth_model
  • MY_Controller
  • MY_Lang
  • MY_Model
  • Pages
  • Private_pages
  • Public_controller
  • Seeder
  • Slug
  • User_sessions
  • Users

Functions

  • action_result
  • actions_widget
  • admin_area_buttons
  • base64_current_url_encode
  • base64_url_decode
  • base64_url_encode
  • current_lang
  • jscript_tag
  • lang_switcher
  • logged_in
  • redirect_lang
  • site_url_lang
  • style_tag

Class MY_Model

how to extend MY_Model: class User_model extends MY_Model { public $table = 'users'; // Set the name of the table for this model. public $primary_key = 'id'; // Set the primary key public $fillable = array(); // You can set an array with the fields that can be filled by insert/update public $protected = array(); // ...Or you can set an array with the fields that cannot be filled by insert/update public function __construct() { $this->database_connection = group_name or array() | OPTIONAL Sets the connection preferences (group name) set up in the database.php. If not trset, it will use the 'default' (the $active_group) database connection. $this->timestamps = TRUE | array('made_at','modified_at','removed_at') If set to TRUE tells MY_Model that the table has 'created_at','updated_at' (and 'deleted_at' if $this->soft_delete is set to TRUE) If given an array as parameter, it tells MY_Model, that the first element is a created_at field type, the second element is a updated_at field type (and the third element is a deleted_at field type) $this->soft_deletes = FALSE; Enables (TRUE) or disables (FALSE) the "soft delete" on records. Default is FALSE $this->timestamps_format = 'Y-m-d H:i:s' You can at any time change the way the timestamp is created (the default is the MySQL standard datetime format) by modifying this variable. You can choose between whatever format is acceptable by the php function date() (default is 'Y-m-d H:i:s'), or 'timestamp' (UNIX timestamp) $this->return_as = 'object' | 'array' Allows the model to return the results as object or as array $this->has_one['phone'] = 'Phone_model' or $this->has_one['phone'] = array('Phone_model','foreign_key','local_key'); $this->has_one['address'] = 'Address_model' or $this->has_one['address'] = array('Address_model','foreign_key','another_local_key'); Allows establishing ONE TO ONE or more ONE TO ONE relationship(s) between models/tables $this->has_many['posts'] = 'Post_model' or $this->has_many['posts'] = array('Posts_model','foreign_key','another_local_key'); Allows establishing ONE TO MANY or more ONE TO MANY relationship(s) between models/tables $this->has_many_pivot['posts'] = 'Post_model' or $this->has_many_pivot['posts'] = array('Posts_model','foreign_primary_key','local_primary_key'); Allows establishing MANY TO MANY or more MANY TO MANY relationship(s) between models/tables with the use of a PIVOT TABLE !ATTENTION: The pivot table name must be composed of the two table names separated by "" the table names having to to be alphabetically ordered (NOT users_posts, but posts_users). Also the pivot table must contain as identifying columns the columns named by convention as follows: table_name_singular + _ + foreign_table_primary_key. For example: considering that a post can have multiple authors, a pivot table that connects two tables (users and posts) must be named posts_users and must have post_id and user_id as identifying columns for the posts.id and users.id tables. $this->cache_driver = 'file' $this->cache_prefix = 'mm' If you know you will do some caching of results without the native caching solution, you can at any time use the MY_Model's caching. By default, MY_Model uses the files to cache result. If you want to change the way it stores the cache, you can change the $cache_driver property to whatever CodeIgniter cache driver you want to use. Also, with $cache_prefix, you can prefix the name of the caches. by default any cache made by MY_Model starts with 'mm' + _ + "name chosen for cache" $this->delete_cache_on_save = FALSE If you use caching often and you don't want to be forced to delete cache manually, you can enable $this->delete_cache_on_save by setting it to TRUE. If set to TRUE the model will auto-delete all cache related to the model's table whenever you write/update/delete data from that table. $this->pagination_delimiters = array('',''); If you know you will use the paginate() method, you can change the delimiters between the pages links $this->pagination_arrows = array('<','>'); You can also change the way the previous and next arrows look like.

parent::__construct(); } }

CI_Model
Extended by MY_Model

Direct known subclasses

Article_model

Located at MY_Model.php

Methods summary

public
# __construct( )
public
# _get_table_fields( )
public
# fillable_fields( )
public
# _prep_before_write( $data )
public
# _prep_after_write( )
public
# _prep_before_read( )
public
# _prep_after_read( $data, $multi = TRUE )
public
# from_form( null $rules = NULL, array $additional_values = NULL, array $row_fields_to_update = array() )

public function from_form($rules = NULL,$additional_values = array(), $row_fields_to_update = array()) Gets data from form, after validating it and waits for an insert() or update() method in the query chain

public function from_form($rules = NULL,$additional_values = array(), $row_fields_to_update = array()) Gets data from form, after validating it and waits for an insert() or update() method in the query chain

Parameters

$rules

Gets the validation rules. If nothing is passed (NULL), will look for the validation rules inside the model $rules public property

$additional_values

Accepts additional fields to be filled, fields that are not to be found inside the form. The values are inserted as an array with "field_name" => "field_value"

$row_fields_to_update

You can mention the fields from the form that can be used to identify the row when doing an update

Returns


$this
public int/array
# insert( $data = NULL )

public function insert($data) Inserts data into table. Can receive an array or a multidimensional array depending on what kind of insert we're talking about.

public function insert($data) Inserts data into table. Can receive an array or a multidimensional array depending on what kind of insert we're talking about.

Parameters

$data

Returns

int/array
Returns id/ids of inserted rows
public
# is_multidimensional( $array )
public str/array
# update( array $data = NULL, array|integer $column_name_where = NULL, boolean $escape = TRUE )

public function update($data) Updates data into table. Can receive an array or a multidimensional array depending on what kind of update we're talking about.

public function update($data) Updates data into table. Can receive an array or a multidimensional array depending on what kind of update we're talking about.

Parameters

$data
$column_name_where
$escape
should the values be escaped or not - defaults to true

Returns

str/array
Returns id/ids of inserted rows
public
# where( null $field_or_array = NULL, null $operator_or_value = NULL, null $value = NULL, boolean $with_or = FALSE, boolean $with_not = FALSE, boolean $custom_string = FALSE )

public function where($field_or_array = NULL, $operator_or_value = NULL, $value = NULL, $with_or = FALSE, $with_not = FALSE, $custom_string = FALSE) Sets a where method for the $this object

public function where($field_or_array = NULL, $operator_or_value = NULL, $value = NULL, $with_or = FALSE, $with_not = FALSE, $custom_string = FALSE) Sets a where method for the $this object

Parameters

$field_or_array
  • can receive a field name or an array with more wheres...
$operator_or_value
  • can receive a database operator or, if it has a field, the value to equal with
$value
  • a value if it received a field name and an operator
$with_or
  • if set to true will create a or_where query type pr a or_like query type, depending on the operator
$with_not
  • if set to true will also add "NOT" in the where
$custom_string
  • if set to true, will simply assume that $field_or_array is actually a string and pass it to the where query

Returns


$this
public
# limit( $limit, integer $offset = 0 )

public function limit($limit, $offset = 0) Sets a rows limit to the query

public function limit($limit, $offset = 0) Sets a rows limit to the query

Parameters

$limit
$offset

Returns


$this
public
# group_by( $grouping_by )

public function group_by($grouping_by) A wrapper to $this->_database->group_by()

public function group_by($grouping_by) A wrapper to $this->_database->group_by()

Parameters

$grouping_by

Returns


$this
public Returns
# delete( $where = NULL )

public function delete($where) Deletes data from table.

public function delete($where) Deletes data from table.

Parameters

$where
Can receive the primary key value or a list of primary keys as array()

Returns

Returns
affected rows or false on failure
public boolean
# force_delete( null $where = NULL )

public function force_delete($where = NULL) Forces the delete of a row if soft_deletes is enabled

public function force_delete($where = NULL) Forces the delete of a row if soft_deletes is enabled

Parameters

$where

Returns

boolean
public boolean
# restore( null $where = NULL )

public function restore($where = NULL) "Un-deletes" a row

public function restore($where = NULL) "Un-deletes" a row

Parameters

$where

Returns

boolean
public boolean
# trashed( null $where = NULL )

public function trashed($where = NULL) Verifies if a record (row) is soft_deleted or not

public function trashed($where = NULL) Verifies if a record (row) is soft_deleted or not

Parameters

$where

Returns

boolean
public
# _get_joined( $requested )
public mixed
# get( null $where = NULL )

public function get() Retrieves one row from table.

public function get() Retrieves one row from table.

Parameters

$where

Returns

mixed
public mixed
# get_all( null $where = NULL )

public function get_all() Retrieves rows from table.

public function get_all() Retrieves rows from table.

Parameters

$where

Returns

mixed
public integer
# count_rows( null $where = NULL )

public function count_rows() Retrieves number of rows from table.

public function count_rows() Retrieves number of rows from table.

Parameters

$where

Returns

integer
public
# with( string $request, array $arguments = array() )

public function with($requests) allows the user to retrieve records from other interconnected tables depending on the relations defined before the constructor

public function with($requests) allows the user to retrieve records from other interconnected tables depending on the relations defined before the constructor

Parameters

$request
$arguments

Returns


$this
protected mixed
# join_temporary_results( $data )

protected function join_temporary_results($data) Joins the subquery results to the main $data

protected function join_temporary_results($data) Joins the subquery results to the main $data

Parameters

$data

Returns

mixed
public obj
# on( $connection_group = NULL )

public function on($connection_group = NULL) Sets a different connection to use for a query

public function on($connection_group = NULL) Sets a different connection to use for a query

Parameters

$connection_group
NULL - connection group in database setup

Returns

obj
public obj
# reset_connection( )

public function reset_connection($connection_group = NULL) Resets the connection to the default used for all the model

public function reset_connection($connection_group = NULL) Resets the connection to the default used for all the model

Returns

obj
public
# trigger( $event, $data = array(), $last = TRUE )

Trigger an event and call its observers. Pass through the event name (which looks for an instance variable $this->event_name), an array of parameters to pass through and an optional 'last in interation' boolean

Trigger an event and call its observers. Pass through the event name (which looks for an instance variable $this->event_name), an array of parameters to pass through and an optional 'last in interation' boolean

public
# with_trashed( )

public function with_trashed() Sets $_trashed to with

public function with_trashed() Sets $_trashed to with

public
# without_trashed( )

public function without_trashed() Sets $_trashed to without

public function without_trashed() Sets $_trashed to without

public
# only_trashed( )

public function with_trashed() Sets $_trashed to only

public function with_trashed() Sets $_trashed to only

public
# fields( $fields = NULL )

public function fields($fields) does a select() of the $fields

public function fields($fields) does a select() of the $fields

Parameters

$fields
fields needed

Returns


$this
public
# order_by( $criteria, string $order = 'ASC' )

public function order_by($criteria, $order = 'ASC' A wrapper to $this->_database->order_by()

public function order_by($criteria, $order = 'ASC' A wrapper to $this->_database->order_by()

Parameters

$criteria
$order

Returns


$this
public
# as_array( )

Return the next call as an array rather than an object

Return the next call as an array rather than an object

public
# as_object( )

Return the next call as an object rather than an array

Return the next call as an object rather than an array

public
# as_dropdown( $field = NULL )
protected
# _get_from_cache( $cache_name = NULL )
protected
# _write_to_cache( $data, $cache_name = NULL )
public
# set_cache( $string, $seconds = 86400 )
public
# delete_cache( $string = NULL )
public
# paginate( $rows_per_page, $total_rows = NULL, $page_number = 1 )
public
# set_pagination_delimiters( $delimiters )
public
# set_pagination_arrows( $arrows )
public
# __call( $method, $arguments )
public
# object_to_array( $object )
protected boolean
# is_assoc( array $array )

Verifies if an array is associative or not

Verifies if an array is associative or not

Parameters

$array

Returns

boolean

Properties summary

protected $_database_connection

Select the database connection from the group names defined inside the database.php configuration file or an array.

Select the database connection from the group names defined inside the database.php configuration file or an array.

# NULL
protected This $_database

one will hold the database connection object

one will hold the database connection object

#
public null $table

Sets table name

Sets table name

# NULL
public null $primary_key

Sets PRIMARY KEY

Sets PRIMARY KEY

# 'id'
public array $table_fields

You can establish the fields of the table. If you won't these fields will be filled by MY_Model (with one query)

You can establish the fields of the table. If you won't these fields will be filled by MY_Model (with one query)

# array()
public array $fillable

Sets fillable fields

Sets fillable fields

# array()
public array $protected

Sets protected fields

Sets protected fields

# array()
protected boolean $timestamps

| array Enables created_at and updated_at fields

| array Enables created_at and updated_at fields

# TRUE
protected string $timestamps_format
# 'Y-m-d H:i:s'
protected $_created_at_field
#
protected $_updated_at_field
#
protected $_deleted_at_field
#
protected boolean $soft_deletes

Enables soft_deletes

Enables soft_deletes

# FALSE
public array $has_one
# array()
public array $has_many
# array()
public array $has_many_pivot
# array()
public boolean $separate_subqueries
# TRUE
public string $cache_driver
# 'file'
public string $cache_prefix
# 'mm'
protected array $_cache
# array()
public boolean $delete_cache_on_save
# FALSE
public $next_page
#
public $previous_page
#
public $all_pages
#
public $pagination_delimiters
#
public $pagination_arrows
#
protected array $before_create

The various callbacks available to the model. Each are simple lists of method names (methods will be run on $this).

The various callbacks available to the model. Each are simple lists of method names (methods will be run on $this).

# array()
protected array $after_create
# array()
protected array $before_update
# array()
protected array $after_update
# array()
protected array $before_get
# array()
protected array $after_get
# array()
protected array $before_delete
# array()
protected array $after_delete
# array()
protected array $before_soft_delete
# array()
protected array $after_soft_delete
# array()
protected array $callback_parameters
# array()
protected string $return_as
# 'object'
protected $return_as_dropdown
# NULL
protected string $_dropdown_field
# ''
CAFPE Docs API documentation generated by ApiGen