summaryrefslogtreecommitdiff
path: root/system/libraries/Model.php
blob: 01d16fddbcd3bef2e23c01cf25f412b7829fe170 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Model base class.
 *
 * $Id: Model.php 4679 2009-11-10 01:45:52Z isaiah $
 *
 * @package    Core
 * @author     Kohana Team
 * @copyright  (c) 2007-2009 Kohana Team
 * @license    http://kohanaphp.com/license
 */
class Model_Core {

	/**
	 * Creates and returns a new model.
	 *
	 * @param   string   model name
	 * @param   mixed    constructor arguments
	 * @param   boolean  construct the model with multiple arguments
	 * @return  Model
	 */
	public static function factory($name, $args = NULL, $multiple = FALSE)
	{
		// Model class name
		$class = ucfirst($name).'_Model';

		if ($args === NULL)
		{
			// Create a new model with no arguments
			return new $class;
		}

		if ($multiple !== TRUE)
		{
			// Create a model with a single argument
			return new $class($args);
		}

		$class = new ReflectionClass($class);

		// Create a model with multiple arguments
		return $class->newInstanceArgs($args);
	}

	// Database object
	protected $db = 'default';

	/**
	 * Loads the database instance, if the database is not already loaded.
	 *
	 * @return  void
	 */
	public function __construct()
	{
		if ( ! is_object($this->db))
		{
			// Load the default database
			$this->db = Database::instance($this->db);
		}
	}

} // End Model