Пример #1
0
 /**
  * Save the record.
  *
  * The record can either be deleted, updated
  * or inserted based on record ID and current
  * state.  Only performs these actions if actually
  * necessary.
  *
  * @param boolean $bulk Bulk flag which gets passed to inserts and updates
  * @return mr_db_record
  * @throws coding_exception
  */
 public function save($bulk = false)
 {
     // Check for delete
     if ($this->is_delete()) {
         // Delete the record and reset _record so we don't try to delete again
         $this->_table->delete($this->_record->id);
         $this->_record = new stdClass();
         // Check for update
     } else {
         if ($this->is_update()) {
             $this->_change->id = $this->_record->id;
             $this->_table->update_record($this->_change, $bulk);
             // Check for insert
         } else {
             if ($this->is_insert()) {
                 $this->_record->id = $this->_table->insert_record($this->_record, true, $bulk);
             }
         }
     }
     // Reset change state
     $this->_change = new stdClass();
     return $this;
 }
Пример #2
0
 /**
  * Mostly copied from mysqli_native_moodle_database.  Main change
  * is that it defaults the numeric value to the table's default
  * value instead of zero
  *
  * @throws dml_write_exception
  * @param mr_db_table $table
  * @param $column
  * @param $value
  * @return int|null|string
  */
 protected function normalise_value(mr_db_table $table, $column, $value)
 {
     if (is_bool($value)) {
         // Always, convert boolean to int
         $value = (int) $value;
     } else {
         if ($value === '') {
             if ($column->meta_type == 'I' or $column->meta_type == 'F' or $column->meta_type == 'N') {
                 $value = $table->get_column_default($column);
                 // prevent '' problems in numeric fields
             }
             // Any float value being stored in varchar or text field is converted to string to avoid
             // any implicit conversion by MySQL
         } else {
             if (is_float($value) and ($column->meta_type == 'C' or $column->meta_type == 'X')) {
                 $value = "{$value}";
             }
         }
     }
     // workaround for problem with wrong enums in mysql - TODO: Out in Moodle 2.1
     if (!empty($column->enums)) {
         if (is_null($value) and !$column->not_null) {
             // ok - nulls allowed
         } else {
             if (!in_array((string) $value, $column->enums)) {
                 throw new dml_write_exception('Enum value ' . s($value) . ' not allowed in field ' . $column->name . '.');
             }
         }
     }
     return $value;
 }