Database.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Database Model base class.
  4. *
  5. * @package Kohana/Database
  6. * @category Models
  7. * @author Kohana Team
  8. * @copyright (c) Kohana Team
  9. * @license https://koseven.ga/LICENSE.md
  10. */
  11. abstract class Kohana_Model_Database extends Model {
  12. /**
  13. * Create a new model instance. A [Database] instance or configuration
  14. * group name can be passed to the model. If no database is defined, the
  15. * "default" database group will be used.
  16. *
  17. * $model = Model::factory($name);
  18. *
  19. * @param string $name model name
  20. * @param mixed $db Database instance object or string
  21. * @return Model
  22. */
  23. public static function factory($name, $db = NULL)
  24. {
  25. // Add the model prefix
  26. $class = 'Model_'.$name;
  27. return new $class($db);
  28. }
  29. // Database instance
  30. protected $_db;
  31. /**
  32. * Loads the database.
  33. *
  34. * $model = new Foo_Model($db);
  35. *
  36. * @param mixed $db Database instance object or string
  37. * @return void
  38. */
  39. public function __construct($db = NULL)
  40. {
  41. if ($db)
  42. {
  43. // Set the instance or name
  44. $this->_db = $db;
  45. }
  46. elseif ( ! $this->_db)
  47. {
  48. // Use the default name
  49. $this->_db = Database::$default;
  50. }
  51. if (is_string($this->_db))
  52. {
  53. // Load the database
  54. $this->_db = Database::instance($this->_db);
  55. }
  56. }
  57. } // End Model