original_string: final public function create() {
          global $DB, $USER;
  
          if ($this->raw_get('id')) {
              // The validation methods rely on the ID to know if we're updating or not, the ID should be
              // falsy whenever we are creating an object.
              throw new coding_exception('Cannot create an object that has an ID defined.');
          }
  
          if (!$this->is_valid()) {
              throw new invalid_persistent_exception($this->get_errors());
          }
  
          // Before create hook.
          $this->before_create();
  
          // We can safely set those values bypassing the validation because we know what we're doing.
          $now = time();
          $this->raw_set('timecreated', $now);
          $this->raw_set('timemodified', $now);
          $this->raw_set('usermodified', $USER->id);
  
          $record = $this->to_record();
          unset($record->id);
  
          $id = $DB->insert_record(static::TABLE, $record);
          $this->raw_set('id', $id);
  
          // We ensure that this is flagged as validated.
          $this->validated = true;
  
          // After create hook.
          $this->after_create();
  
          return $this;
      }
code: final public function create() {
          global $DB, $USER;
  
          if ($this->raw_get('id')) {
              // The validation methods rely on the ID to know if we're updating or not, the ID should be
              // falsy whenever we are creating an object.
              throw new coding_exception('Cannot create an object that has an ID defined.');
          }
  
          if (!$this->is_valid()) {
              throw new invalid_persistent_exception($this->get_errors());
          }
  
          // Before create hook.
          $this->before_create();
  
          // We can safely set those values bypassing the validation because we know what we're doing.
          $now = time();
          $this->raw_set('timecreated', $now);
          $this->raw_set('timemodified', $now);
          $this->raw_set('usermodified', $USER->id);
  
          $record = $this->to_record();
          unset($record->id);
  
          $id = $DB->insert_record(static::TABLE, $record);
          $this->raw_set('id', $id);
  
          // We ensure that this is flagged as validated.
          $this->validated = true;
  
          // After create hook.
          $this->after_create();
  
          return $this;
      }
code_tokens: final public function create ( ) { global $ DB , $ USER ; if ( $ this -> raw_get ( 'id' ) ) { // The validation methods rely on the ID to know if we're updating or not, the ID should be // falsy whenever we are creating an object. throw new coding_exception ( 'Cannot create an object that has an ID defined.' ) ; } if ( ! $ this -> is_valid ( ) ) { throw new invalid_persistent_exception ( $ this -> get_errors ( ) ) ; } // Before create hook. $ this -> before_create ( ) ; // We can safely set those values bypassing the validation because we know what we're doing. $ now = time ( ) ; $ this -> raw_set ( 'timecreated' , $ now ) ; $ this -> raw_set ( 'timemodified' , $ now ) ; $ this -> raw_set ( 'usermodified' , $ USER -> id ) ; $ record = $ this -> to_record ( ) ; unset ( $ record -> id ) ; $ id = $ DB -> insert_record ( static :: TABLE , $ record ) ; $ this -> raw_set ( 'id' , $ id ) ; // We ensure that this is flagged as validated. $ this -> validated = true ; // After create hook. $ this -> after_create ( ) ; return $ this ; }
docstring: Insert a record in the DB.
  
  @return persistent
docstring_tokens: Insert a record in the DB .