Changeset 110

User picture

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)

Affected files

Updated trunk/application/models/ignitedrecord/base.php Download diff

109110
1
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
1
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
/*
2
/*
3
 * Created on 2008 Jun 28
3
 * Created on 2008 Jul 9
4
 * by Martin Wernstahl <m4rw3r@gmail.com>
4
 * by Martin Wernstahl <m4rw3r@gmail.com>
5
 */
5
 */
6
6
...
...
29
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
 */
31
 */
32
32
/**
33
/**
33
 * @addtogroup IgnitedRecord
34
 * @addtogroup IgnitedRecord
34
 * @{
35
 * @{
35
 */
36
 */
36
/**
37
/**
37
 * A wrapper for the CodeIgniter ActiveRecord class.
38
 * Load IgnitedQuery.
38
 */
39
 */
39
class IR_base{
40
if( ! class_exists('IgnitedQuery'))
41
{
42
	require_once APPPATH.'libraries/ignitedquery.php';
43
}
44
45
/**
46
 * Define the global var to store the models.
47
 */
48
$GLOBALS['IR_MODELS'] = array();
49
50
/**
51
 * PHP 4 base for IgnitedRecord.
52
 */
53
class IR_base extends IgnitedQuery{
54
	
40
	/**
55
	/**
41
	 * Constructor.
56
	 * Returns the model which is mapped to the table $table.
57
	 * 
58
	 * Can be called statically.
59
	 * 
60
	 * @param $table A table name
61
	 * @return object or false
42
	 */
62
	 */
43
	function IR_base()
63
	function &get_model($table)
44
	{
64
	{
45
		$CI =& get_instance();
65
		if( ! isset($GLOBALS['IR_MODELS'][$table]))
46
		$this->db =& $CI->db;
66
		{
67
			$false = false;
68
			return $false;
69
		}
70
		
71
		return $GLOBALS['IR_MODELS'][$table];
47
	}
72
	}
48
	
73
	
49
	
50
	// --------------------------------------------------------------------
74
	// --------------------------------------------------------------------
51
		
75
		
52
	/**
76
	/**
53
	 * Resets the current query, enabling you to restart the building process
77
	 * Adds a model which is mapped to the $table to the internal array.
54
	 * @return void
78
	 * 
79
	 * Can be called statically.
80
	 * 
81
	 * @param $object The model object
82
	 * @param $table The tablename
55
	 */
83
	 */
56
	function reset()
84
	function add_model(&$object, $table)
57
	{
85
	{
58
		$this->db->_reset_select();
86
		$GLOBALS['IR_MODELS'][$table] =& $object;
59
	}
87
	}
60
	
61
	// --------------------------------------------------------------------
62
	
63
	function &dbprefix($table = ''){
64
		return $this->db->dbprefix($table);
65
	}
66
	function &select($select = '*', $protect_identifiers = TRUE){
67
		$this->db->select($select, $protect_identifiers);
68
		return $this;
69
	}
70
	function &select_max($select = '', $alias=''){
71
		$this->db->select_max($select, $alias);
72
		return $this;
73
	}
74
	function &select_min($select = '', $alias=''){
75
		$this->db->select_min($select, $alias);
76
		return $this;
77
	}
78
	function &select_avg($select = '', $alias=''){
79
		$this->db->select_avg($select, $alias);
80
		return $this;
81
	}
82
	function &select_sum($select = '', $alias=''){
83
		$this->db->select_sum($select, $alias);
84
		return $this;
85
	}
86
	function &distinct($val = TRUE){
87
		$this->db->distinct($val);
88
		return $this;
89
	}
90
	function &from($from){
91
		$this->db->from($from);
92
		return $this;
93
	}
94
	function &join($table, $cond, $type = ''){
95
		$this->db->join($table, $cond, $type);
96
		return $this;
97
	}
98
	function &where($key, $value = NULL, $escape = TRUE){
99
		$this->db->where($key, $value, $escape);
100
		return $this;
101
	}
102
	function &or_where($key, $value = NULL, $escape = TRUE){
103
		$this->db->or_where($key, $value, $escape);
104
		return $this;
105
	}
106
	function &orwhere($key, $value = NULL, $escape = TRUE){
107
		$this->db->orwhere($key, $value, $escape);
108
		return $this;
109
	}
110
	function &where_in($key = NULL, $values = NULL){
111
		$this->db->where_in($key, $values);
112
		return $this;
113
	}
114
	function &or_where_in($key = NULL, $values = NULL){
115
		$this->db->or_where_in($key, $values);
116
		return $this;
117
	}
118
	function &where_not_in($key = NULL, $values = NULL){
119
		$this->db->where_not_in($key, $values);
120
		return $this;
121
	}
122
	function &or_where_not_in($key = NULL, $values = NULL){
123
		$this->db->or_where_not_in($key, $values);
124
		return $this;
125
	}
126
	function &like($field, $match = '', $side = 'both'){
127
		$this->db->like($field, $match, $side);
128
		return $this;
129
	}
130
	function &not_like($field, $match = '', $side = 'both'){
131
		$this->db->not_like($field, $match, $side);
132
		return $this;
133
	}
134
	function &or_like($field, $match = '', $side = 'both'){
135
		$this->db->or_like($field, $match, $side);
136
		return $this;
137
	}
138
	function &or_not_like($field, $match = '', $side = 'both'){
139
		$this->db->or_not_like($field, $match, $side);
140
		return $this;
141
	}
142
	function &orlike($field, $match = '', $side = 'both'){
143
		$this->db->orlike($field, $match, $side);
144
		return $this;
145
	}
146
	function &group_by($by){
147
		$this->db->group_by($by);
148
		return $this;
149
	}
150
	function &groupby($by){
151
		$this->db->groupby($by);
152
		return $this;
153
	}
154
	function &having($key, $value = '', $escape = TRUE){
155
		$this->db->having($key, $value, $escape);
156
		return $this;
157
	}
158
	function &orhaving($key, $value = '', $escape = TRUE){
159
		$this->db->orhaving($key, $value, $escape);
160
		return $this;
161
	}
162
	function &or_having($key, $value = '', $escape = TRUE){
163
		$this->db->or_having($key, $value, $escape);
164
		return $this;
165
	}
166
	function &order_by($orderby, $direction = ''){
167
		$this->db->order_by($orderby, $direction);
168
		return $this;
169
	}
170
	function &orderby($orderby, $direction = ''){
171
		$this->db->orderby($orderby, $direction);
172
		return $this;
173
	}
174
	function &limit($value, $offset = ''){
175
		$this->db->limit($value, $offset);
176
		return $this;
177
	}
178
	function &offset($offset){
179
		$this->db->offset($offset);
180
		return $this;
181
	}
182
	function db_set($key, $value = '', $escape = TRUE){
183
		return $this->db->set($key, $value, $escape);
184
	}
185
	function db_get($table = '', $limit = null, $offset = null){
186
		return $this->db->get($table, $limit, $offset);
187
	}
188
	function count_all_results($table = ''){
189
		return $this->db->count_all_results($table);
190
	}
191
	function db_get_where($table = '', $where = null, $limit = null, $offset = null){
192
		return $this->db->get_where($table, $where, $limit, $offset);
193
	}
194
	function db_insert($table = '', $set = NULL){
195
		return $this->db->insert($table, $set);
196
	}
197
	function db_update($table = '', $set = NULL, $where = NULL, $limit = NULL){
198
		return $this->db->update($table, $set, $where, $limit);
199
	}
200
	function db_empty_table($table = ''){
201
		return $this->db->empty_table($table);
202
	}
203
	function db_truncate($table = ''){
204
		return $this->db->truncate($table);
205
	}
206
	function db_delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE){
207
		return $this->db->delete($table, $where, $limit, $reset_data);
208
	}
209
	function db_use_table($table){
210
		return $this->db->use_table($table);
211
	}
212
	function &start_cache(){
213
		$this->db->start_cache();
214
		return $this;
215
	}
216
	function &stop_cache(){
217
		$this->db->stop_cache();
218
		return $this;
219
	}
220
	function &flush_cache(){
221
		$this->db->flush_cache();
222
		return $this;
223
	}
224
}
88
}
225
/**
89
/**
226
 * @}
90
 * @}

Added trunk/application/models/ignitedrecord/base_ar.php

Show contents

Added trunk/application/models/ignitedrecord/base_php5.php

Show contents

Updated trunk/application/models/ignitedrecord/ignitedrecord.php Download diff

109110
57
 */
57
 */
58
58
59
/**
59
/**
60
 * Include the wrapper for ActiveRecord.
60
 * Include the wrapper for IgnitedQuery, and the version specific code.
61
 */
61
 */
62
if(defined('IR_USE_IQ') && IR_USE_IQ === true)
62
if(defined('IR_USE_AR') && IR_USE_AR == true)
63
{
63
{
64
	// IgnitedQuery based wrapper
64
	require_once 'base_ar.php';
65
	require_once 'base_iq.php';
66
}
65
}
67
else
66
elseif(floor(phpversion()) < 5)
68
{
67
{
69
	// CodeIgniter's ActiveRecord based wrapper
70
	require_once 'base.php';
68
	require_once 'base.php';
71
}
69
}
70
else
71
{
72
	require_once 'base_php5.php';
73
}
72
74
73
/**
75
/**
74
 * Include the objects to hold data.
76
 * Include the objects to hold data.
...
...
313
		$CI =& get_instance();
315
		$CI =& get_instance();
314
		$CI->load->helper('inflector');
316
		$CI->load->helper('inflector');
315
		
317
		
318
		// load the db
319
		$this->db =& $CI->db;
320
		
316
		$this->_load_behaviours($this->act_as);
321
		$this->_load_behaviours($this->act_as);
317
		
322
		
318
		// set default classname if not already set
323
		// set default classname if not already set
...
...
327
			$this->table = plural($class);
332
			$this->table = plural($class);
328
		}
333
		}
329
		
334
		
335
		// keep track of this model
336
		IR_base::add_model($this, $this->table);
330
		
337
		
331
		//    NORMALIZE RELATIONS
338
		$this->_normalize_rel_properties();
332
		//  -----------------------
333
		
334
		$types = array('has_many' => true,
335
					   'has_one' => false,
336
					   'belongs_to' => false,
337
					   'habtm' => true,
338
					   'has_and_belongs_to_many' => true);
339
		
340
		foreach($types as $rel => $plural)
341
		{
342
			// if there is no data, skip here for speed
343
			if( ! count($this->$rel))
344
			{
345
				$this->$rel = array(); // reset, just in case
346
				continue;
347
			}
348
			
349
			// only a string, then make it the relation name
350
			if(is_string($this->$rel))
351
				$this->$rel = array('name' => $this->$rel);
352
			
353
			// is it a single relation? then encapsulate it in an array
354
			$var =& $this->$rel;
355
			if(isset($var['table']) OR isset($var['name']))
356
				$this->$rel = array($this->$rel);
357
			
358
			$tmp = $this->$rel;
359
			$this->$rel = array(); // reset, so nothing may survive and cause a mess
360
			
361
			foreach($tmp as $data)
362
			{
363
				if(is_string($data))
364
					$data = array('name' => $data);
365
				
366
				if( ! (isset($data['name']) OR isset($data['table'])))
367
				{
368
					show_error('IgnitedRecord: No tablename and/or relation name was specified when defining a relation of type "'.
369
							$rel.'".');
370
				}
371
				
372
				// get the right relation name
373
				if(isset($data['name']))
374
				{
375
					// if we have a name, use it
376
					$name = $data['name'];
377
				}
378
				elseif($plural)
379
				{
380
					// we must have a name or a table, then table is plural and suitable
381
					$name = $data['table'];
382
				}
383
				elseif(isset($data['model']))
384
				{
385
					// already a model?
386
					$name = $data['model'];
387
				}
388
				else
389
				{
390
					// nope, use singular of tablename
391
					$name = singular($data['table']);
392
				}
393
				
394
				// call the relation specifying function
395
				$this->$rel($name, $data);
396
			}
397
		}
398
	}
339
	}
399
	
340
	
400
	// --------------------------------------------------------------------
341
	// --------------------------------------------------------------------
...
...
670
	function &get()
611
	function &get()
671
	{
612
	{
672
		$this->hook('pre_get');
613
		$this->hook('pre_get');
673
		$query = $this->db_get($this->table,1);
614
		$query = parent::get($this->table,1);
674
		
615
		
675
		if( ! $query->num_rows())
616
		if( ! $query->num_rows())
676
		{
617
		{
...
...
717
		}
658
		}
718
		
659
		
719
		// fetch
660
		// fetch
720
		$query = $this->db_get_where($this->table,$where,1);
661
		$query = parent::get_where($this->table,$where,1);
721
		if( ! $query->num_rows())
662
		if( ! $query->num_rows())
722
		{
663
		{
723
			$false = false;
664
			$false = false;
...
...
759
		$this->hook('pre_find_by',array(&$where));
700
		$this->hook('pre_find_by',array(&$where));
760
		
701
		
761
		// fetch
702
		// fetch
762
		$query = $this->db_get_where($this->table, $where, 1);
703
		$query = parent::get_where($this->table, $where, 1);
763
		if( ! $query->num_rows())
704
		if( ! $query->num_rows())
764
		{
705
		{
765
			$false = false;
706
			$false = false;
...
...
813
	{
754
	{
814
		$arr = array();
755
		$arr = array();
815
		$this->hook('pre_find_all');
756
		$this->hook('pre_find_all');
816
		$query = $this->db_get($this->table);
757
		$query = parent::get($this->table);
817
		
758
		
818
		foreach($query->result_array() as $row)
759
		foreach($query->result_array() as $row)
819
		{
760
		{
...
...
851
		}
792
		}
852
		
793
		
853
		$this->hook('pre_find_by',array(&$where));
794
		$this->hook('pre_find_by',array(&$where));
854
		$query = $this->db_get_where($this->table, $where);
795
		$query = parent::get_where($this->table, $where);
855
		
796
		
856
		foreach($query->result_array() as $row)
797
		foreach($query->result_array() as $row)
857
		{
798
		{
...
...
1257
		empty($columns) AND $columns = $this->db->list_fields($rel->get_table());
1198
		empty($columns) AND $columns = $this->db->list_fields($rel->get_table());
1258
		
1199
		
1259
		// get the id column for the related object
1200
		// get the id column for the related object
1260
		if(($model = $rel->get_model()) != '')
1201
		if($model =& IR_base::get_model($rel->get_table()))
1261
		{
1202
		{
1262
			$CI =& get_instance();
1203
			$model =& $model;
1263
			$model =& $CI->$model;
1264
			$col = $model->id_col;
1204
			$col = $model->id_col;
1265
		}
1205
		}
1266
		else
1206
		else
...
...
1347
		$rel_query->from($data->get_table());
1287
		$rel_query->from($data->get_table());
1348
		$rel_query->multiple = true;
1288
		$rel_query->multiple = true;
1349
		
1289
		
1350
		if(($model = $data->get_model()) != '')
1290
		if($model =& IR_base::get_model($data->get_table()))
1351
		{
1291
		{
1352
			// use that model to instantiate the objects
1292
			// use that model to instantiate the objects
1353
			$CI =& get_instance();
1293
			$rel_query->model_inst =& $model;
1354
			$rel_query->model_inst =& $CI->$model;
1355
			
1294
			
1356
			$rel_query->where($data->get_fk(), $obj->__id);
1295
			$rel_query->where($data->get_fk(), $obj->__id);
1357
		}
1296
		}
...
...
1389
		$rel_query->limit(1); // this is a has ONE relationship
1328
		$rel_query->limit(1); // this is a has ONE relationship
1390
		$rel_query->multiple = false;
1329
		$rel_query->multiple = false;
1391
		
1330
		
1392
		if(($model = $data->get_model()) != '')
1331
		if($model =& IR_base::get_model($data->get_table()))
1393
		{
1332
		{
1394
			// use that model to instantiate the objects
1333
			// use that model to instantiate the objects
1395
			$CI =& get_instance();
1334
			$rel_query->model_inst =& $model;
1396
			$rel_query->model_inst =& $CI->$model;
1397
			
1335
			
1398
			$rel_query->where($data->get_fk(), $obj->__id);
1336
			$rel_query->where($data->get_fk(), $obj->__id);
1399
		}
1337
		}
...
...
1433
		
1371
		
1434
		if(isset($obj->{$data->get_fk()}))
1372
		if(isset($obj->{$data->get_fk()}))
1435
		{
1373
		{
1436
			if(($model = $data->get_model()) != '')
1374
			if($model =& IR_base::get_model($data->get_table()))
1437
			{
1375
			{
1438
				// use that model to instantiate the objects
1376
				// use that model to instantiate the objects
1439
				$CI =& get_instance();
1377
				$rel_query->model_inst =& $model;
1440
				$rel_query->model_inst =& $CI->$model;
1441
				
1378
				
1442
				$rel_query->where($rel_query->model_inst->id_col, $obj->{$data->get_fk()});
1379
				$rel_query->where($model->id_col, $obj->{$data->get_fk()});
1443
			}
1380
			}
1444
			else
1381
			else
1445
			{
1382
			{
...
...
1487
		$rel_query->multiple = true;
1424
		$rel_query->multiple = true;
1488
		
1425
		
1489
		// fetch the id column
1426
		// fetch the id column
1490
		if($data->get_model() != '')
1427
		if($model =& IR_base::get_model($data->get_table()))
1491
		{
1428
		{
1492
			// use that model to instantiate the objects
1429
			// use that model to instantiate the objects
1493
			$CI =& get_instance();
1430
			$rel_query->model_inst =& $model;
1494
			$rel_query->model_inst =& $CI->{$data->get_model()};
1495
			
1431
			
1496
			$id = $rel_query->model_inst->id_col;
1432
			$id = $model->id_col;
1497
		}
1433
		}
1498
		else
1434
		else
1499
		{
1435
		{
...
...
2139
	// --------------------------------------------------------------------
2075
	// --------------------------------------------------------------------
2140
		
2076
		
2141
	/**
2077
	/**
2078
	 * Normalizes the data found in the relation properties,
2079
	 * and creates IR_RelProperty objects for them.
2080
	 */
2081
	function _normalize_rel_properties()
2082
	{
2083
		$types = array('has_many' => true,
2084
					   'has_one' => false,
2085
					   'belongs_to' => false,
2086
					   'habtm' => true,
2087
					   'has_and_belongs_to_many' => true);
2088
		
2089
		foreach($types as $rel => $plural)
2090
		{
2091
			// if there is no data, skip here for speed
2092
			if( ! count($this->$rel))
2093
			{
2094
				$this->$rel = array(); // reset, just in case
2095
				continue;
2096
			}
2097
			
2098
			// only a string, then make it the relation name
2099
			if(is_string($this->$rel))
2100
				$this->$rel = array('name' => $this->$rel);
2101
			
2102
			// is it a single relation? then encapsulate it in an array
2103
			$var =& $this->$rel;
2104
			if(isset($var['table']) OR isset($var['name']))
2105
				$this->$rel = array($this->$rel);
2106
			
2107
			$tmp = $this->$rel;
2108
			$this->$rel = array(); // reset, so nothing may survive and cause a mess
2109
			
2110
			foreach($tmp as $data)
2111
			{
2112
				if(is_string($data))
2113
					$data = array('name' => $data);
2114
				
2115
				if( ! (isset($data['name']) OR isset($data['table'])))
2116
				{
2117
					show_error('IgnitedRecord: No tablename and/or relation name was specified when defining a relation of type "'.
2118
							$rel.'".');
2119
				}
2120
				
2121
				// get the right relation name
2122
				if(isset($data['name']))
2123
				{
2124
					// if we have a name, use it
2125
					$name = $data['name'];
2126
				}
2127
				elseif($plural)
2128
				{
2129
					// we must have a name or a table, then table is plural and suitable
2130
					$name = $data['table'];
2131
				}
2132
				else
2133
				{
2134
					// nope, use singular of tablename
2135
					$name = singular($data['table']);
2136
				}
2137
				
2138
				// call the relation specifying function
2139
				$this->$rel($name, $data);
2140
			}
2141
		} // end foreach($types)
2142
	}
2143
	
2144
	// --------------------------------------------------------------------
2145
		
2146
	/**
2142
	 * Returns the modelname of this object.
2147
	 * Returns the modelname of this object.
2143
	 * 
2148
	 * 
2144
	 * Which is classname or the name of the CI property this object lies in.
2149
	 * Which is classname or the name of the CI property this object lies in.
2145
	 * 
2150
	 * 
2146
	 * @access private
2151
	 * @access private
2152
	 * (accessed by IR_RelProperty)
2147
	 * @return string, or false if not found
2153
	 * @return string, or false if not found
2148
	 */
2154
	 */
2149
	function get_modelname()
2155
	function _get_modelname()
2150
	{
2156
	{
2151
		// check cache
2157
		// check cache
2152
		if(isset($this->model_name))
2158
		if(isset($this->model_name))
...
...
2198
		if(strtolower(substr($class,-6)) == '_model')
2204
		if(strtolower(substr($class,-6)) == '_model')
2199
			$class = substr($class,0,-6);
2205
			$class = substr($class,0,-6);
2200
		
2206
		
2207
		if($class == 'ignitedrecord')
2208
		{
2209
			$class = false;
2210
		}
2211
		
2201
		$this->model_name = $class;
2212
		$this->model_name = $class;
2202
		return $class;
2213
		return $class;
2203
	}
2214
	}

Updated trunk/application/models/ignitedrecord/record.php Download diff

109110
130
	function __wakeup()
130
	function __wakeup()
131
	{
131
	{
132
		$CI =& get_instance();
132
		$CI =& get_instance();
133
		$name = IR_RelProperty::_get_modelname($this->__table);
134
		
133
		
135
		if( ! isset($CI->$name))
134
		if($model = IR_base::get_model($this->__table))
136
		{
135
		{
137
			log_message('error','The model linked to the table "'.
136
			$this->__instance =& $model;
138
				$this->__table.'" was not found, resulting in a read only object.');
139
		}
137
		}
140
		else
138
		else
141
		{
139
		{
142
			$this->__instance =& $CI->$name;
140
			log_message('error','The model linked to the table "'.
141
				$this->__table.'" was not found, resulting in a read only object.');
143
		}
142
		}
144
	}
143
	}
145
	
144
	
...
...
162
	 * 
161
	 * 
163
	 * To prevent from accidentally editing the uid of the row.
162
	 * To prevent from accidentally editing the uid of the row.
164
	 *
163
	 *
164
	 * @deprecated (use the property instead)
165
	 * @since 0.1.1
165
	 * @since 0.1.1
166
	 * @return the value of the id column(s) (false if not in db)
166
	 * @return the value of the id column(s) (false if not in db)
167
	 */
167
	 */
...
...
178
	 * @since 0.2.0
178
	 * @since 0.2.0
179
	 * @access public
179
	 * @access public
180
	 * @param $data The data to be loaded, with column name as key, data as value
180
	 * @param $data The data to be loaded, with column name as key, data as value
181
	 * 
182
	 * @return void
181
	 * @return void
183
	 */
182
	 */
184
	function load_data($data = array())
183
	function load_data($data = array())

Updated trunk/application/models/ignitedrecord/relproperty.php Download diff

109110
49
	var $table;
49
	var $table;
50
	
50
	
51
	/**
51
	/**
52
	 * The model name to use when creating related objects.
53
	 */
54
	var $model;
55
	
56
	/**
57
	 * The column that the other column relates to.
52
	 * The column that the other column relates to.
58
	 */
53
	 */
59
	var $fk;
54
	var $fk;
...
...
71
		{
66
		{
72
			$this->table = $settings['table'];
67
			$this->table = $settings['table'];
73
		}
68
		}
74
		if(isset($settings['model']))
69
		
75
		{
76
			$this->model = $settings['model'];
77
		}
78
		if(isset($settings['foreign_key']))
70
		if(isset($settings['foreign_key']))
79
		{
71
		{
80
			$this->fk = $settings['foreign_key'];
72
			$this->fk = $settings['foreign_key'];
81
		}
73
		}
74
		
82
		if(isset($settings['fk']))
75
		if(isset($settings['fk']))
83
		{
76
		{
84
			$this->fk = $settings['fk'];
77
			$this->fk = $settings['fk'];
...
...
113
	 */
106
	 */
114
	function singular($singular)
107
	function singular($singular)
115
	{
108
	{
116
		if( ! isset($this->model))
109
		$this->table = plural($singular);
117
			$this->model = $singular;
118
	}
110
	}
119
	
111
	
120
	// --------------------------------------------------------------------
112
	// --------------------------------------------------------------------
121
		
113
		
122
	/**
114
	/**
123
	 * Sets the model name for this relation.
124
	 * 
125
	 * @param $model The modelname
126
	 * @return $this
127
	 */
128
	function &model($model)
129
	{
130
		$this->model = $model;
131
		return $this;
132
	}
133
	
134
	// --------------------------------------------------------------------
135
		
136
	/**
137
	 * Sets the table name for this relation.
115
	 * Sets the table name for this relation.
138
	 * 
116
	 * 
139
	 * @param $table The table name
117
	 * @param $table The table name
...
...
183
	 */
161
	 */
184
	function get_table()
162
	function get_table()
185
	{
163
	{
186
		return isset($this->table) ? $this->table : plural($this->model);
164
		return $this->table;
187
	}
165
	}
188
	
166
	
189
	// --------------------------------------------------------------------
167
	// --------------------------------------------------------------------
190
		
168
		
191
	/**
169
	/**
192
	 * Returns the model used by this relation.
193
	 * 
194
	 * @return string
195
	 */
196
	function get_model()
197
	{
198
		if(isset($this->model))
199
			return $this->model;
200
		$this->model = $this->_get_modelname($this->table);
201
		return $this->model;
202
	}
203
	
204
	// --------------------------------------------------------------------
205
		
206
	/**
207
	 * Returns the foreign key used by this relation.
170
	 * Returns the foreign key used by this relation.
208
	 * 
171
	 * 
209
	 * @return string
172
	 * @return string
...
...
212
	{
175
	{
213
		return isset($this->fk)
176
		return isset($this->fk)
214
				? $this->fk
177
				? $this->fk
215
				: ($this->get_model() == false
178
				: (($name = $this->_get_modelname())
216
					? singular($this->get_table())
179
					? $name
217
					: $this->get_model()
180
					: singular($this->get_table())
218
				  ).'_id';
181
				  ).'_id';
219
	}
182
	}
220
	
183
	
221
	// --------------------------------------------------------------------
184
	// --------------------------------------------------------------------
222
		
185
		
223
	/**
186
	/**
224
	 * Returns the modelname of the tablename.
187
	 * Returns the modelname of model linked to $tablename.
225
	 * 
188
	 * 
226
	 * Tries if a model with $tablename exists, then tries with singular form of the $tablename. \n
227
	 * Called if no model is defined for a table.
228
	 * 
189
	 * 
229
	 * @access private
190
	 * @access private
230
	 * @param $tablename The tablename to find a modelname of.
191
	 * @param $tablename The tablename
231
	 * @return The modelname or '' if no model is found
192
	 * @return The modelname or singlualr($tablename) if no model is found
232
	 */
193
	 */
233
	function _get_modelname($tablename)
194
	function _get_modelname($tablename)
234
	{
195
	{
235
		$tablename = strtolower($tablename);
196
		$tablename = strtolower($tablename);
236
		$model = false;
237
		
197
		
238
		// check the CI props for an initialized IR model with that name
198
		if($model =& IR_base::get_model($tablename))
239
		$CI =& get_instance();
240
		
241
		foreach(array(singular($tablename), $tablename, singular($tablename).'_model', $tablename.'_model') as $name)
242
		{
199
		{
243
			if(isset($CI->$name) && (is_a($CI->$name,'IgnitedRecord') OR is_a($CI->$name,'Model')))
200
			$name = $model->_get_modelname();
244
			{
201
			
245
				$model = $name;
202
			return empty($name) ? singular($tablename) : $name;
246
				break;
247
			}
248
			if(file_exists(APPPATH.'/models/'.$name.EXT))
249
			{
250
				$model = $name;
251
				break;
252
			}
253
		}
203
		}
254
		
204
		else
255
		return $model;
205
		{
206
			return singular($tablename);
207
		}
256
	}
208
	}
257
}
209
}
258
210
...
...
266
 */
218
 */
267
class IR_RelProperty_has extends IR_RelProperty{
219
class IR_RelProperty_has extends IR_RelProperty{
268
	/**
220
	/**
269
	 * The mode that utilizes this object.
221
	 * The model that utilizes this object.
270
	 * Used to get the default column name to relate with.
222
	 * Used to get the default column name to relate with.
271
	 */
223
	 */
272
	var $parent_model;
224
	var $parent_model;
...
...
280
	 */
232
	 */
281
	function get_fk()
233
	function get_fk()
282
	{
234
	{
283
		return isset($this->fk) ? $this->fk : $this->parent_model->get_modelname().'_id';
235
		return isset($this->fk)
236
					? $this->fk
237
					: (($name = $this->parent_model->_get_modelname())
238
						? $name
239
						: singular($this->table)
240
					  ) . '_id';
284
	}
241
	}
285
}
242
}
286
243
...
...
317
	function IR_RelProp_habtm($settings = array())
274
	function IR_RelProp_habtm($settings = array())
318
	{
275
	{
319
		parent::IR_RelProp($settings);
276
		parent::IR_RelProp($settings);
277
		
320
		foreach(array('related_foreign_key', 'related_fk', 'r_fk') as $key)
278
		foreach(array('related_foreign_key', 'related_fk', 'r_fk') as $key)
321
		{
279
		{
322
			if(isset($settings[$key]))
280
			if(isset($settings[$key]))
...
...
354
	 */
312
	 */
355
	function get_join_table()
313
	function get_join_table()
356
	{
314
	{
357
		return isset($this->join_table) ? $this->join_table :
315
		return isset($this->join_table)
358
				(strcmp($this->parent_table, $this->get_table()) < 0 ? $this->parent_table.'_'.$this->get_table() : $this->get_table().'_'.$this->parent_table);
316
				? $this->join_table
317
				: (strcmp($this->parent_table, $this->get_table()) < 0
318
					? $this->parent_table.'_'.$this->get_table()
319
					: $this->get_table().'_'.$this->parent_table
320
				  );
359
	}
321
	}
360
	
322
	
361
	// --------------------------------------------------------------------
323
	// --------------------------------------------------------------------
...
...
409
	{
371
	{
410
		return isset($this->related_fk)
372
		return isset($this->related_fk)
411
				? $this->related_fk
373
				? $this->related_fk
412
				: ($this->get_model() == false
374
				: (($name = $this->_get_modelname())
413
					? singular($this->get_table())
375
					? $name
414
					: $this->get_model()
376
					: singular($this->get_table())
415
				  ).'_id';
377
				  ).'_id';
416
	}
378
	}
417
}
379
}

Updated trunk/ar_wrapper_generator.php Download diff

109110
6
$file = file_get_contents('../../CodeIgniter/system/database/DB_active_rec.php');
6
$file = file_get_contents('../../CodeIgniter/system/database/DB_active_rec.php');
7
7
8
// these will be prefixed by db_ and return their real return value
8
// these will be prefixed by db_ and return their real return value
9
$private = array('set','get','get_where','insert','update','empty_table','delete','truncate','use_table');
9
$private = array('set','insert','update','empty_table','delete','truncate','use_table');
10
10
11
$num = preg_match_all(
11
$num = preg_match_all(
12
			'@function(?:\s*)(\S*)\(([^\)]*)\)@'
12
			'@function(?:\s*)(\S*)\(([^\)]*)\)@'
13
				,$file,$matches,PREG_SET_ORDER);
13
				,$file,$matches,PREG_SET_ORDER);
14
14
15
echo "<pre>\nclass IR_base{\n\t".
15
echo '<pre>/**
16
'function IR_base()
16
 * Define the global var to store the models.
17
 */
18
$GLOBALS[\'IR_MODELS\'] = array();
19
20
/**
21
 * PHP 4 base for IgnitedRecord with CI\'s ActiveRecord
22
 */
23
class IR_base{
24
	
25
	/**
26
	 * Returns the model which is mapped to the table $table.
27
	 * 
28
	 * Can be called statically.
29
	 * 
30
	 * @param $table A table name
31
	 * @return object or false
32
	 */
33
	function &get_model($table)
17
	{
34
	{
18
		$CI =& get_instance();
35
		if( ! isset($GLOBALS[\'IR_MODELS\'][$table]))
19
		$this->db =& $CI->db;
36
		{
37
			$false = false;
38
			return $false;
39
		}
40
		
41
		return $GLOBALS[\'IR_MODELS\'][$table];
20
	}
42
	}
21
	
43
	
22
	// --------------------------------------------------------------------
44
	// --------------------------------------------------------------------
23
		
45
		
24
	/**
46
	/**
47
	 * Adds a model which is mapped to the $table to the internal array.
48
	 * 
49
	 * Can be called statically.
50
	 * 
51
	 * @param $object The model object
52
	 * @param $table The tablename
53
	 */
54
	function add_model(&$object, $table)
55
	{
56
		$GLOBALS[\'IR_MODELS\'][$table] =& $object;
57
	}
58
	
59
	// --------------------------------------------------------------------
60
		
61
	/**
25
	 * Resets the current query, enabling you to restart the building process
62
	 * Resets the current query, enabling you to restart the building process
26
	 * @return void
63
	 * @return void
27
	 */
64
	 */
...
...
44
		$function[1] = 'db_'.$function[1];
81
		$function[1] = 'db_'.$function[1];
45
		$is_execute = true;
82
		$is_execute = true;
46
	}
83
	}
84
	elseif(in_array($function[1], array('get', 'get_where'))
85
	{
86
		$is_execute = true;
87
	}
47
	else
88
	else
48
		// add ampersand, to aviod copies
89
		// add ampersand, to aviod copies
49
		$function[1] = '&'.$function[1];
90
		$function[1] = '&'.$function[1];