示例#1
0
 /**
  * Checks if the class provided matches against the filter
  * @param SCAR_Generic $klass
  * @return boolean
  **/
 public function filterMatch($klass)
 {
     if (!$this->_filter) {
         return true;
     }
     $relation = $this->getRelation($this->_filter->getName());
     $match = true;
     foreach ($relation['keys'] as $local_key => $foreign_key) {
         if ($this->_filter->{$foreign_key} != $klass->{$local_key}) {
             $match = false;
         }
     }
     return $match;
 }
示例#2
0
 public function initialize()
 {
     if ($this->is_initialized) {
         return true;
     }
     $this->is_initialized = true;
     $db = array();
     $properties = array();
     // no config path, no db list, include default config
     if ($this->config_path === null && count($this->db_list) == 0) {
         include SCAR_BASE . 'config.php';
     } elseif (count($this->db_list) > 0) {
         $db = $this->db_list;
         $properties = $this->properties;
     } else {
         include $this->config_path;
     }
     $keymap = array();
     foreach ($db as $database) {
         $tables = call_user_func_array(array($this->SCAR, 'showTables'), array($database));
         foreach ($tables as $table) {
             // make object
             $klass_name = Inflector::classify($table);
             $scar = new SCAR_Generic();
             $scar->name($klass_name);
             $scar->dsn($database);
             $scar->table($table);
             // get columns
             $columns = call_user_func_array(array($this->SCAR, 'showColumns'), array($database, $table));
             $scar->primaryKey($columns['pk']);
             $scar->fields($columns['columns']);
             // look for a foriegn key in here
             foreach ($columns['columns'] as $col => $type) {
                 if (strpos($col, '_id') === false) {
                     continue;
                 }
                 $keymap[] = array('class' => $klass_name, 'relates' => Inflector::camelize(substr($col, 0, -3)));
             }
             // store
             $this->scars[$klass_name] = $scar;
         }
     }
     // every object is loaded. now handle the relationships
     foreach ($keymap as $key) {
         // skip invalids
         if (!isset($this->scars[$key['class']]) || !isset($this->scars[$key['relates']])) {
             continue;
         }
         // class has one relates
         $o = $this->scars[$key['class']];
         $p = $this->scars[$key['relates']];
         $o->hasOne(Inflector::classify($p->getTable()));
         // converse is also true
         $p->hasMany(Inflector::singularize(Inflector::classify($o->getTable())));
     }
     // add properties
     foreach ($properties as $klass => $props) {
         $scar =& $this->scars[$klass];
         foreach ($props as $type => $params) {
             switch ($type) {
                 case ACTS_AS_MATERIALIZED_PATH:
                     $scar->actsAsMaterializedPath($params[0]);
                     break;
             }
         }
     }
 }