root/trunk/application/models/ignitedrecord/ignitedrecord.php

105110
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
	}