示例#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);
 }
示例#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};
 }
示例#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;
     }
 }
示例#4
0
 public function testIn()
 {
     $in = NimbleQuery::in('`id`', range(1, 10));
     $this->assertEquals('`id` IN(1,2,3,4,5,6,7,8,9,10)', $in);
 }
 /**
  * Performs a find for has_many->as (polymorphic)
  * @param NimbleRecord $class
  * @param string $name - association name
  * @param array $options
  */
 protected static function has_many_polymorphic_find($class, $name, $options = array())
 {
     $id = $class->row[NimbleRecord::$primary_key_field];
     $model = static::model($name);
     $polymorphic_column_type = $options['as'] . '_type';
     $polymorphic_column_id = $options['as'] . '_id';
     $class = strtolower(static::class_as_string($class));
     $conditions = implode(' AND ', array(NimbleQuery::condition($polymorphic_column_type, $class), NimbleQuery::condition($polymorphic_column_id, $id)));
     if (strpos($id, 'IN') !== false) {
         $conditions = implode(' AND ', array(NimbleQuery::condition($polymorphic_column_type, $class), $polymorphic_column_id . $id));
     }
     $merg_conditions = array($conditions);
     if (!is_null($options['conditions'])) {
         $merg_conditions[] = $options['conditions'];
     }
     $options['conditions'] = implode(' AND ', $merg_conditions);
     return call_user_func("{$model}::find_all", $options);
 }