2013-04-13 06:17:56 +00:00
|
|
|
<?php defined('SYSPATH') OR die('No direct script access.');
|
2011-07-20 12:57:07 +00:00
|
|
|
/**
|
|
|
|
* Database Model base class.
|
|
|
|
*
|
|
|
|
* @package Kohana/Database
|
|
|
|
* @category Models
|
|
|
|
* @author Kohana Team
|
2013-04-13 06:17:56 +00:00
|
|
|
* @copyright (c) 2008-2012 Kohana Team
|
2011-07-20 12:57:07 +00:00
|
|
|
* @license http://kohanaframework.org/license
|
|
|
|
*/
|
|
|
|
abstract class Kohana_Model_Database extends Model {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new model instance. A [Database] instance or configuration
|
|
|
|
* group name can be passed to the model. If no database is defined, the
|
|
|
|
* "default" database group will be used.
|
|
|
|
*
|
|
|
|
* $model = Model::factory($name);
|
|
|
|
*
|
2013-04-13 06:17:56 +00:00
|
|
|
* @param string $name model name
|
|
|
|
* @param mixed $db Database instance object or string
|
2011-07-20 12:57:07 +00:00
|
|
|
* @return Model
|
|
|
|
*/
|
|
|
|
public static function factory($name, $db = NULL)
|
|
|
|
{
|
|
|
|
// Add the model prefix
|
|
|
|
$class = 'Model_'.$name;
|
|
|
|
|
|
|
|
return new $class($db);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Database instance
|
2013-04-13 06:17:56 +00:00
|
|
|
protected $_db;
|
2011-07-20 12:57:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the database.
|
|
|
|
*
|
|
|
|
* $model = new Foo_Model($db);
|
|
|
|
*
|
2013-04-13 06:17:56 +00:00
|
|
|
* @param mixed $db Database instance object or string
|
2011-07-20 12:57:07 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct($db = NULL)
|
|
|
|
{
|
2013-04-13 06:17:56 +00:00
|
|
|
if ($db)
|
2011-07-20 12:57:07 +00:00
|
|
|
{
|
2013-04-13 06:17:56 +00:00
|
|
|
// Set the instance or name
|
2011-07-20 12:57:07 +00:00
|
|
|
$this->_db = $db;
|
|
|
|
}
|
2013-04-13 06:17:56 +00:00
|
|
|
elseif ( ! $this->_db)
|
|
|
|
{
|
|
|
|
// Use the default name
|
|
|
|
$this->_db = Database::$default;
|
|
|
|
}
|
2011-07-20 12:57:07 +00:00
|
|
|
|
|
|
|
if (is_string($this->_db))
|
|
|
|
{
|
|
|
|
// Load the database
|
|
|
|
$this->_db = Database::instance($this->_db);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End Model
|