Changeset 110
Author: m4rw3r
(2008/08/31 13:33) Over 3 years ago
Made IgnitedQuery the default SQL-builder Models are now stored in an internal array with the table they are mapped to as key, which makes the model option in relations settings obsolete The related model must be set to define a non-default foreign key automatically (using modelname+_id instead of singular(tablename)+_id)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');* Created on 2008 Jun 28* Created on 2008 Jul 9* @{* @{* A wrapper for the CodeIgniter ActiveRecord class.* Load IgnitedQuery.class IR_base{if( ! class_exists('IgnitedQuery')){require_once APPPATH.'libraries/ignitedquery.php';}/*** Define the global var to store the models.*/$GLOBALS['IR_MODELS'] = array();/*** PHP 4 base for IgnitedRecord.*/class IR_base extends IgnitedQuery{* Constructor.* Returns the model which is mapped to the table $table.** Can be called statically.** @param $table A table name* @return object or falsefunction IR_base()function &get_model($table){{$CI =& get_instance();if( ! isset($GLOBALS['IR_MODELS'][$table]))$this->db =& $CI->db;{$false = false;return $false;}return $GLOBALS['IR_MODELS'][$table];* Resets the current query, enabling you to restart the building process* Adds a model which is mapped to the $table to the internal array.* @return void* Can be called statically.** @param $object The model object* @param $table The tablenamefunction reset()function add_model(&$object, $table){{$this->db->_reset_select();$GLOBALS['IR_MODELS'][$table] =& $object;// --------------------------------------------------------------------function &dbprefix($table = ''){return $this->db->dbprefix($table);}function &select($select = '*', $protect_identifiers = TRUE){$this->db->select($select, $protect_identifiers);return $this;}function &select_max($select = '', $alias=''){$this->db->select_max($select, $alias);return $this;}function &select_min($select = '', $alias=''){$this->db->select_min($select, $alias);return $this;}function &select_avg($select = '', $alias=''){$this->db->select_avg($select, $alias);return $this;}function &select_sum($select = '', $alias=''){$this->db->select_sum($select, $alias);return $this;}function &distinct($val = TRUE){$this->db->distinct($val);return $this;}function &from($from){$this->db->from($from);return $this;}function &join($table, $cond, $type = ''){$this->db->join($table, $cond, $type);return $this;}function &where($key, $value = NULL, $escape = TRUE){$this->db->where($key, $value, $escape);return $this;}function &or_where($key, $value = NULL, $escape = TRUE){$this->db->or_where($key, $value, $escape);return $this;}function &orwhere($key, $value = NULL, $escape = TRUE){$this->db->orwhere($key, $value, $escape);return $this;}function &where_in($key = NULL, $values = NULL){$this->db->where_in($key, $values);return $this;}function &or_where_in($key = NULL, $values = NULL){$this->db->or_where_in($key, $values);return $this;}function &where_not_in($key = NULL, $values = NULL){$this->db->where_not_in($key, $values);return $this;}function &or_where_not_in($key = NULL, $values = NULL){$this->db->or_where_not_in($key, $values);return $this;}function &like($field, $match = '', $side = 'both'){$this->db->like($field, $match, $side);return $this;}function ¬_like($field, $match = '', $side = 'both'){$this->db->not_like($field, $match, $side);return $this;}function &or_like($field, $match = '', $side = 'both'){$this->db->or_like($field, $match, $side);return $this;}function &or_not_like($field, $match = '', $side = 'both'){$this->db->or_not_like($field, $match, $side);return $this;}function &orlike($field, $match = '', $side = 'both'){$this->db->orlike($field, $match, $side);return $this;}function &group_by($by){$this->db->group_by($by);return $this;}function &groupby($by){$this->db->groupby($by);return $this;}function &having($key, $value = '', $escape = TRUE){$this->db->having($key, $value, $escape);return $this;}function &orhaving($key, $value = '', $escape = TRUE){$this->db->orhaving($key, $value, $escape);return $this;}function &or_having($key, $value = '', $escape = TRUE){$this->db->or_having($key, $value, $escape);return $this;}function &order_by($orderby, $direction = ''){$this->db->order_by($orderby, $direction);return $this;}function &orderby($orderby, $direction = ''){$this->db->orderby($orderby, $direction);return $this;}function &limit($value, $offset = ''){$this->db->limit($value, $offset);return $this;}function &offset($offset){$this->db->offset($offset);return $this;}function db_set($key, $value = '', $escape = TRUE){return $this->db->set($key, $value, $escape);}function db_get($table = '', $limit = null, $offset = null){return $this->db->get($table, $limit, $offset);}function count_all_results($table = ''){return $this->db->count_all_results($table);}function db_get_where($table = '', $where = null, $limit = null, $offset = null){return $this->db->get_where($table, $where, $limit, $offset);}function db_insert($table = '', $set = NULL){return $this->db->insert($table, $set);}function db_update($table = '', $set = NULL, $where = NULL, $limit = NULL){return $this->db->update($table, $set, $where, $limit);}function db_empty_table($table = ''){return $this->db->empty_table($table);}function db_truncate($table = ''){return $this->db->truncate($table);}function db_delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE){return $this->db->delete($table, $where, $limit, $reset_data);}function db_use_table($table){return $this->db->use_table($table);}function &start_cache(){$this->db->start_cache();return $this;}function &stop_cache(){$this->db->stop_cache();return $this;}function &flush_cache(){$this->db->flush_cache();return $this;}* Include the wrapper for ActiveRecord.* Include the wrapper for IgnitedQuery, and the version specific code.if(defined('IR_USE_IQ') && IR_USE_IQ === true)if(defined('IR_USE_AR') && IR_USE_AR == true){{// IgnitedQuery based wrapperrequire_once 'base_ar.php';require_once 'base_iq.php';elseif(floor(phpversion()) < 5){{// CodeIgniter's ActiveRecord based wrapper{$CI->load->helper('inflector');$CI->load->helper('inflector');// NORMALIZE RELATIONS$this->_normalize_rel_properties();// -----------------------$types = array('has_many' => true,'has_one' => false,'belongs_to' => false,'habtm' => true,'has_and_belongs_to_many' => true);foreach($types as $rel => $plural){// if there is no data, skip here for speedif( ! count($this->$rel)){$this->$rel = array(); // reset, just in casecontinue;}// only a string, then make it the relation nameif(is_string($this->$rel))$this->$rel = array('name' => $this->$rel);// is it a single relation? then encapsulate it in an array$var =& $this->$rel;if(isset($var['table']) OR isset($var['name']))$this->$rel = array($this->$rel);$tmp = $this->$rel;$this->$rel = array(); // reset, so nothing may survive and cause a messforeach($tmp as $data){if(is_string($data))$data = array('name' => $data);if( ! (isset($data['name']) OR isset($data['table']))){show_error('IgnitedRecord: No tablename and/or relation name was specified when defining a relation of type "'.$rel.'".');}// get the right relation nameif(isset($data['name'])){// if we have a name, use it$name = $data['name'];}elseif($plural){// we must have a name or a table, then table is plural and suitable$name = $data['table'];}elseif(isset($data['model'])){// already a model?$name = $data['model'];}else{// nope, use singular of tablename$name = singular($data['table']);}// call the relation specifying function$this->$rel($name, $data);}}{{$this->hook('pre_get');$this->hook('pre_get');$query = $this->db_get($this->table,1);$query = parent::get($this->table,1);{{$query = $this->db_get_where($this->table,$where,1);$query = parent::get_where($this->table,$where,1);{{$this->hook('pre_find_by',array(&$where));$this->hook('pre_find_by',array(&$where));$query = $this->db_get_where($this->table, $where, 1);$query = parent::get_where($this->table, $where, 1);{{{{$this->hook('pre_find_all');$this->hook('pre_find_all');$query = $this->db_get($this->table);$query = parent::get($this->table);{{$this->hook('pre_find_by',array(&$where));$this->hook('pre_find_by',array(&$where));$query = $this->db_get_where($this->table, $where);$query = parent::get_where($this->table, $where);{{if(($model = $rel->get_model()) != '')if($model =& IR_base::get_model($rel->get_table())){{$CI =& get_instance();$model =& $model;$model =& $CI->$model;if(($model = $data->get_model()) != '')if($model =& IR_base::get_model($data->get_table())){{$CI =& get_instance();$rel_query->model_inst =& $model;$rel_query->model_inst =& $CI->$model;if(($model = $data->get_model()) != '')if($model =& IR_base::get_model($data->get_table())){{$CI =& get_instance();$rel_query->model_inst =& $model;$rel_query->model_inst =& $CI->$model;if(isset($obj->{$data->get_fk()}))if(isset($obj->{$data->get_fk()})){{if(($model = $data->get_model()) != '')if($model =& IR_base::get_model($data->get_table())){{$CI =& get_instance();$rel_query->model_inst =& $model;$rel_query->model_inst =& $CI->$model;$rel_query->where($rel_query->model_inst->id_col, $obj->{$data->get_fk()});$rel_query->where($model->id_col, $obj->{$data->get_fk()});{{if($data->get_model() != '')if($model =& IR_base::get_model($data->get_table())){{$CI =& get_instance();$rel_query->model_inst =& $model;$rel_query->model_inst =& $CI->{$data->get_model()};$id = $rel_query->model_inst->id_col;$id = $model->id_col;{{{$types = array('has_many' => true,{{$this->$rel = array('name' => $this->$rel);{$data = array('name' => $data);{show_error('IgnitedRecord: No tablename and/or relation name was specified when defining a relation of type "'.{{{function _get_modelname(){{{{{$name = IR_RelProperty::_get_modelname($this->__table);if( ! isset($CI->$name))if($model = IR_base::get_model($this->__table)){{log_message('error','The model linked to the table "'.$this->__instance =& $model;$this->__table.'" was not found, resulting in a read only object.');{{$this->__instance =& $CI->$name;log_message('error','The model linked to the table "'.$this->__table.'" was not found, resulting in a read only object.');** The model name to use when creating related objects.*/var $model;/**{{if(isset($settings['model'])){$this->model = $settings['model'];}{{{{{{if( ! isset($this->model))$this->table = plural($singular);$this->model = $singular;* Sets the model name for this relation.** @param $model The modelname* @return $this*/function &model($model){$this->model = $model;return $this;}// --------------------------------------------------------------------/**{{return isset($this->table) ? $this->table : plural($this->model);return $this->table;* Returns the model used by this relation.** @return string*/function get_model(){if(isset($this->model))return $this->model;$this->model = $this->_get_modelname($this->table);return $this->model;}// --------------------------------------------------------------------/**{{: ($this->get_model() == false: (($name = $this->_get_modelname())? singular($this->get_table())? $name: $this->get_model(): singular($this->get_table())* Returns the modelname of the tablename.* Returns the modelname of model linked to $tablename.* Tries if a model with $tablename exists, then tries with singular form of the $tablename. \n* Called if no model is defined for a table.* @param $tablename The tablename to find a modelname of.* @return The modelname or '' if no model is found* @return The modelname or singlualr($tablename) if no model is found{{$model = false;// check the CI props for an initialized IR model with that nameif($model =& IR_base::get_model($tablename))$CI =& get_instance();foreach(array(singular($tablename), $tablename, singular($tablename).'_model', $tablename.'_model') as $name){{if(isset($CI->$name) && (is_a($CI->$name,'IgnitedRecord') OR is_a($CI->$name,'Model')))$name = $model->_get_modelname();{$model = $name;return empty($name) ? singular($tablename) : $name;break;}if(file_exists(APPPATH.'/models/'.$name.EXT)){$model = $name;break;}elsereturn $model;{return singular($tablename);}class IR_RelProperty_has extends IR_RelProperty{class IR_RelProperty_has extends IR_RelProperty{* The model that utilizes this object.{{return isset($this->fk) ? $this->fk : $this->parent_model->get_modelname().'_id';? $this->fk: (($name = $this->parent_model->_get_modelname())? $name: singular($this->table)) . '_id';{{foreach(array('related_foreign_key', 'related_fk', 'r_fk') as $key)foreach(array('related_foreign_key', 'related_fk', 'r_fk') as $key){{{{return isset($this->join_table) ? $this->join_table :(strcmp($this->parent_table, $this->get_table()) < 0 ? $this->parent_table.'_'.$this->get_table() : $this->get_table().'_'.$this->parent_table);? $this->join_table: (strcmp($this->parent_table, $this->get_table()) < 0? $this->parent_table.'_'.$this->get_table(): $this->get_table().'_'.$this->parent_table);{{: ($this->get_model() == false: (($name = $this->_get_modelname())? singular($this->get_table())? $name: $this->get_model(): singular($this->get_table())$file = file_get_contents('../../CodeIgniter/system/database/DB_active_rec.php');$file = file_get_contents('../../CodeIgniter/system/database/DB_active_rec.php');$private = array('set','get','get_where','insert','update','empty_table','delete','truncate','use_table');$private = array('set','insert','update','empty_table','delete','truncate','use_table');echo "<pre>\nclass IR_base{\n\t".echo '<pre>/**'function IR_base()* Define the global var to store the models.*/$GLOBALS[\'IR_MODELS\'] = array();/*** PHP 4 base for IgnitedRecord with CI\'s ActiveRecord*/class IR_base{/*** Returns the model which is mapped to the table $table.** Can be called statically.** @param $table A table name* @return object or false*/function &get_model($table){{$CI =& get_instance();if( ! isset($GLOBALS[\'IR_MODELS\'][$table]))$this->db =& $CI->db;{$false = false;return $false;}return $GLOBALS[\'IR_MODELS\'][$table];{elseif(in_array($function[1], array('get', 'get_where')){