Example #1
0
 /**
  * Removes a version
  */
 public static function delete_version($version)
 {
     $table = NimbleRecord::$adapter->quote_table_name(static::migration_table_name());
     $query = new NimbleQuery(NimbleQuery::DELETE);
     $query->from = $table;
     $query->where = NimbleQuery::condition('version', $version);
     $sql = $query->build();
     Migration::execute($sql);
 }
Example #2
0
 public static function do_method($method, $class, $table, $options = array())
 {
     if (!array_include($method, static::$methods)) {
         throw new NimbleException("{$method} is not a math method");
     }
     $defaults = array('column' => 'id', 'cache' => true, 'conditions' => NULL);
     $options = array_merge($defaults, $options);
     static::check_args_for_math_functions($options);
     $query = new NimbleQuery();
     $query->select = $method . '(' . $table . '.' . $options['column'] . ') AS ' . $method;
     $query->from = $table;
     if (isset($options['joins']) && !empty($options['joins'])) {
         $query->join = $options['joins'];
     }
     if (isset($options['group']) && !empty($options['group'])) {
         $query->group_by = $options['group'];
     }
     if (isset($options['conditions']) && !empty($options['conditions'])) {
         $query->where = NimbleRecord::build_conditions($options['conditions']);
     }
     $sql = $query->build();
     return $class::execute_query($sql, false, $options['cache'])->{$method};
 }
Example #3
0
 public static function update($id, $attributes = array())
 {
     $klass = self::_find($id);
     $old_row = $klass->row;
     $klass->update_mode = true;
     $klass->row = array_merge($klass->row, $attributes);
     if (!static::$is_useing_save) {
         static::verify_input($attributes, $klass);
     }
     static::getErrors($klass);
     if (count($klass->errors) == 0) {
         call_user_func_array(array($klass, 'before_update'), array());
         call_user_func_array(array($klass, 'before_save'), array());
     }
     $updates = array();
     /** Update timestamp if the column exsists */
     $columns = static::columns();
     if (array_include('updated_at', $columns)) {
         $klass->row = self::update_timestamps(array('updated_at'), $klass->row);
     }
     unset($columns);
     // Build query
     $query = new NimbleQuery(NimbleQuery::UPDATE);
     $query->update = self::table_name();
     $query->columns = self::sanatize_input_array(array_keys($klass->row));
     $query->values = self::sanatize_input_array(array_values($klass->row));
     $query->where = "`id` = " . self::sanatize_input_array($id);
     $sql = $query->build();
     unset($query);
     if (count($klass->errors) == 0 && self::execute_insert_query($sql)) {
         array_push(self::$query_log, "UPDATE: {$sql}");
         $klass->id = $id;
         $klass->saved = true;
         $klass->update_mode = false;
         $klass->new_record = false;
         call_user_func_array(array($klass, 'after_update'), array());
         call_user_func_array(array($klass, 'after_save'), array());
         return $klass;
     } else {
         $klass->saved = false;
         $klass->update_mode = false;
         return $klass;
     }
 }
Example #4
0
 public function testBuildDeleteConditions()
 {
     $query = new NimbleQuery(NimbleQuery::DELETE);
     $query->from = NimbleRecord::table_name('User');
     $query->where = "`id` = 5";
     $match = "DELETE FROM `users` WHERE `id` = 5";
     $this->assertEquals($match, $query->build());
 }