Exemplo n.º 1
0
 /**
  * Creates a join table for has_and_belongs_to_many relationships
  * @param string $model_one
  * @param string $model_two
  */
 public function create_join_table($model_one, $model_two)
 {
     $table_name = NimbleAssociation::generate_join_table_name(array($model_one, $model_two));
     $table = $this->create_table($table_name, array('id' => false));
     $table->references($model_one);
     $table->references($model_two);
     $table->go();
 }
Exemplo n.º 2
0
 public function testSelectJoinsComditionsAndLimit()
 {
     $join = NimbleAssociation::process_join(new User(), 'photos');
     $query = new NimbleQuery();
     $query->from = NimbleRecord::table_name('User');
     $query->where = '`users`.id = 5';
     $query->limit = '0,5';
     $query->join = $join;
     $this->assertEquals('SELECT * FROM `users` ' . $join . ' WHERE `users`.id = 5 LIMIT 0,5', $query->build());
 }
Exemplo n.º 3
0
 public function testMakesCorrectJoinTableName()
 {
     $name = NimbleAssociation::generate_join_table_name(array('foo', 'bars'));
     $this->assertEquals('bar_foos', $name);
 }
Exemplo n.º 4
0
 public function testJoinsBelongsToStringLowerCaseBelongsTo()
 {
     $out = NimbleAssociation::process_join('user', 'photos');
     $match = 'INNER JOIN `photos` ON (`users`.id = `photos`.user_id)';
     $this->assertEquals($match, $out);
 }
Exemplo n.º 5
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!');
 }
Exemplo n.º 6
0
 public function __construct($class, $type, $arg)
 {
     if (!array_include($type, NimbleAssociation::$types)) {
         throw new NimbleRecordException('Invalid Association Type: ' . $type);
     }
     $this->type = $type;
     $this->class = NimbleAssociation::class_as_string($class);
     $this->name = $arg;
     foreach (static::$options[$this->type] as $var) {
         $this->{$var} = NULL;
     }
     return $this;
 }