示例#1
0
 public function testForeignKeyCustomSuffix()
 {
     $old = 'id';
     NimbleRecord::$foreign_key_suffix = 'foo';
     $key = NimbleAssociation::foreign_key('User');
     $this->assertEquals('user_foo', $key);
     NimbleRecord::$foreign_key_suffix = $old;
 }
示例#2
0
 public function __call($method, $args)
 {
     if (array_include($method, NimbleAssociation::$types)) {
         return $this->set_assoc($method, $args);
     }
     if (isset(static::$magic_method_map[$method])) {
         $method = static::$magic_method_map[$method];
         return call_user_func(array(get_class($this), $method), $this->id);
     }
     /** 
      * count magic
      * Its special
      */
     if (strtolower($method) == 'count') {
         $klass = get_called_class();
         if (count($args) == 0) {
             return call_user_func_array(array('NimbleMath', 'do_method'), array($method, get_class($this), $klass::table_name()));
         } else {
             if (!NimbleAssociation::exists(self::class_name(), 'has_many', $args[0])) {
                 throw new NimbleRecordException('Association does not exsist');
             }
             $class = Inflector::classify($args[0]);
             $key = NimbleAssociation::foreign_key(self::class_name());
             $conditions = array('conditions' => array($key => $this->id));
             if (isset($args[1])) {
                 if (isset($args[1]['conditions'])) {
                     $conditions['conditions'] = static::process_magic_condition_merge($conditions['conditions'], $args[1]['conditions']);
                     unset($args[1]['conditions']);
                 }
                 $conditions = array_merge($conditions, $args[1]);
             }
             return call_user_func_array(array('NimbleMath', 'do_method'), array($method, get_class($this), static::table_name($class), $conditions));
         }
     }
     /**
      * See static::$math_method_map for included methods
      */
     if (isset(static::$interface_map[$method])) {
         $_class = static::$interface_map[$method];
         $klass = get_called_class();
         if (empty($args) || count($args) < 2) {
             throw new NimbleRecordException('You need to pass an association name and column');
         }
         if (!NimbleAssociation::exists(self::class_name(), 'has_many', $args[0])) {
             throw new nimbleRecordException('Association does not exist');
         }
         $class = Inflector::classify($args[0]);
         $key = NimbleAssociation::foreign_key(self::class_name());
         $conditions = array('conditions' => array($key => $this->id), 'column' => $args[1]);
         if (isset($args[2])) {
             if (isset($args[2]['conditions'])) {
                 $conditions['conditions'] = static::process_magic_condition_merge($conditions['conditions'], $args[2]['conditions']);
                 unset($args[2]['conditions']);
             }
             $conditions = array_merge($conditions, $args[2]);
         }
         return call_user_func_array(array($_class, 'do_method'), array($method, $class, $class::table_name(), $conditions));
     }
     if (array_include($method, static::columns())) {
         $this->row[$method] = $args;
     }
     if (strpos($method, 'uniqueness_of') !== false) {
         $args[1]['class'] = get_class($this);
         $args[1]['instance'] = $this;
     }
     if (preg_match('/^validates_([0-9a-z_]+)$/', $method, $matches)) {
         $klass_method = $matches[1];
         if (method_exists('NimbleValidation', $klass_method)) {
             if (!is_array($args[0]) && is_string($args[0])) {
                 $args[0] = array($args[0]);
             }
             foreach ($args[0] as $column) {
                 if (!isset($this->row[$column])) {
                     $value = '';
                 } else {
                     $value = $this->row[$column];
                 }
                 $argss = array('column_name' => $column, 'value' => $value);
                 if (isset($args[1]) && !empty($args[1])) {
                     $argss = array_merge($argss, $args[1]);
                 }
                 $return = call_user_func_array(array('NimbleValidation', $klass_method), array($argss));
                 $this->process_error_return($return);
             }
         }
         return;
     }
     throw new NimbleRecordException('Method: ' . $method . ' does not exist on record!');
 }