示例#1
0
文件: db.php 项目: MenZil-Team/cms
 /**
  * Class constructor
  *
  * @param  array   $config  Configuration [Optional]
  * @param  string  $id      Session id [Optional]
  */
 public function __construct(array $config = NULL, $id = NULL)
 {
     if (!isset($config['group'])) {
         // Use the default group
         $config['group'] = 'default';
     }
     // Load the database
     $this->_db = Database::instance($config['group']);
     if (isset($config['table'])) {
         // Set the table name
         $this->_table = (string) $config['table'];
     }
     if (isset($config['gc'])) {
         // Set the gc chance
         $this->_gc = (int) $config['gc'];
     }
     if (isset($config['columns'])) {
         // Overload column names
         $this->_columns = $config['columns'];
     }
     $this->_user_id = 0;
     parent::__construct($config, $id);
     if (mt_rand(0, $this->_gc) === $this->_gc) {
         // Run garbage collection
         // This will average out to run once every X requests
         $this->_gc();
     }
 }
示例#2
0
 /**
  * Constructs the model, taking a Database connection as the first and only
  * parameter
  *
  * @param Kohana_Database $db Database connection to use
  */
 public function __construct($db = NULL)
 {
     if ($db == NULL) {
         $db = \Gleez\Database\Database::instance(NULL);
     }
     $this->_db = $db;
     $this->_table = Config::get('migration.table', 'migrations');
 }
示例#3
0
 /**
  * Prepares the model database connection, determines the table name,
  * and loads column information.
  *
  * @return void
  */
 protected function _initialize()
 {
     // Set the object name and plural name
     $this->_object_name = strtolower(substr(get_class($this), 6));
     // Check if this model has already been initialized
     if (!($init = Arr::get(ORM::$_init_cache, $this->_object_name, FALSE))) {
         $init = array('_belongs_to' => array(), '_has_one' => array(), '_has_many' => array(), '_has_many_keys' => array());
         // Set the object plural name if none predefined
         if (!isset($this->_object_plural)) {
             $init['_object_plural'] = Inflector::plural($this->_object_name);
         }
         if (!$this->_errors_filename) {
             $init['_errors_filename'] = $this->_object_name;
         }
         if (!is_object($this->_db)) {
             // Get database instance
             $init['_db'] = Database::instance($this->_db_group);
         }
         if (empty($this->_table_name)) {
             // Table name is the same as the object name
             $init['_table_name'] = $this->_object_name;
             if ($this->_table_names_plural === TRUE) {
                 // Make the table name plural
                 $init['_table_name'] = Arr::get($init, '_object_plural', $this->_object_plural);
             }
         }
         if (!empty($this->_ignored_columns)) {
             // Optimize for performance
             $init['_ignored_columns'] = array_combine($this->_ignored_columns, $this->_ignored_columns);
         }
         $defaults = array();
         foreach ($this->_belongs_to as $alias => $details) {
             $defaults['model'] = $alias;
             $defaults['foreign_key'] = $alias . $this->_foreign_key_suffix;
             $init['_belongs_to'][$alias] = array_merge($defaults, $details);
         }
         foreach ($this->_has_one as $alias => $details) {
             $defaults['model'] = $alias;
             $defaults['foreign_key'] = $this->_object_name . $this->_foreign_key_suffix;
             $init['_has_one'][$alias] = array_merge($defaults, $details);
         }
         foreach ($this->_has_many as $alias => $details) {
             $defaults['model'] = Inflector::singular($alias);
             $defaults['foreign_key'] = $this->_object_name . $this->_foreign_key_suffix;
             $defaults['through'] = NULL;
             $defaults['far_key'] = Inflector::singular($alias) . $this->_foreign_key_suffix;
             $init['_has_many'][$alias] = array_merge($defaults, $details);
         }
         foreach ($this->_has_many_keys as $alias => $details) {
             $defaults['model'] = Inflector::singular($alias);
             $defaults['foreign_key'] = array($this->_object_name . $this->_foreign_key_suffix);
             $defaults['far_key'] = array(Inflector::singular($alias) . $this->_foreign_key_suffix);
             $init['_has_many_keys'][$alias] = array_merge($defaults, $details);
         }
         ORM::$_init_cache[$this->_object_name] = $init;
     }
     // Assign initialized properties to the current object
     foreach ($init as $property => $value) {
         $this->{$property} = $value;
     }
     // Load column information
     $this->reload_columns();
     // Clear initial model state
     $this->clear();
 }
示例#4
0
 /**
  * Load the active modules
  *
  * This is called at bootstrap time
  *
  * @param  boolean  $reset  Reset true to clear the cache.
  *
  * @uses   Cache::get
  * @uses   Log::add
  * @uses   Arr::merge
  */
 public static function load_modules($reset = TRUE)
 {
     self::$modules = array();
     self::$active = array();
     $kohana_modules = array();
     $cache = Cache::instance('modules');
     $data = $cache->get('load_modules', FALSE);
     if ($reset === FALSE and $data and isset($data['kohana_modules'])) {
         // db has to be initiated @todo fix this bug
         Database::instance(NULL);
         // use data from cache
         self::$modules = $data['modules'];
         self::$active = $data['active'];
         $kohana_modules = $data['kohana_modules'];
         unset($data);
     } else {
         $modules = ORM::factory('module')->order_by('weight', 'ASC')->order_by('name', 'ASC')->find_all();
         $_cache_modules = $_cache_active = array();
         foreach ($modules as $module) {
             self::$modules[$module->name] = $module;
             $_cache_modules[$module->name] = $module->as_array();
             if (!$module->active) {
                 continue;
             }
             //fix for old installations, where gleez exists in db
             if ($module->name != 'gleez') {
                 self::$active[$module->name] = $module;
                 $_cache_active[$module->name] = $module->as_array();
                 // try to get module path from db if it set
                 if (!empty($module->path) and is_dir($module->path)) {
                     $kohana_modules[$module->name] = $module->path;
                 } else {
                     $kohana_modules[$module->name] = MODPATH . $module->name;
                 }
             }
         }
         // set the cache for performance in production
         if (Kohana::$environment === Kohana::PRODUCTION) {
             $data = array();
             $data['modules'] = $_cache_modules;
             $data['active'] = $_cache_active;
             $data['kohana_modules'] = $kohana_modules;
             $cache->set('load_modules', $data, Date::DAY);
             unset($data, $_cache_modules, $_cache_active);
         }
     }
     Kohana::modules(Arr::merge($kohana_modules, Kohana::modules()));
 }
示例#5
0
文件: DB.php 项目: MenZil-Team/cms
 public static function prefix()
 {
     return Database::instance(NULL, self::$_config)->table_prefix();
 }
示例#6
0
 public static function version()
 {
     return Database::instance(NULL, self::$_config)->version();
 }
示例#7
0
 /**
  * Gets a database connection for running the migrations
  *
  * @param  string $db_group Database connection group name
  * @return Database  Database connection
  */
 protected function _get_db_instance($db_group)
 {
     // If this isn't a dry run then just use a normal database connection
     if (!$this->_dry_run) {
         return Database::instance($db_group);
     }
     return Migration_Database::faux_instance($db_group);
 }