original_string: public function save($data = array())
      {
          // 1. Merges data from parameters
          $data && $this->fromArray($data);
  
          // 2.1 Saves single record
          if (!$this->isColl) {
  
              // 2.1.1 Returns when record has been destroy to avoid store dirty data
              if ($this->isDestroyed) {
                  return $this;
              }
  
              // Deletes the record when it's waiting to remove from database
              if ($this->detached) {
                  $this->db->delete($this->table, array($this->primaryKey => $this->data[$this->primaryKey]));
                  $this->isDestroyed = true;
                  return $this;
              }
  
              // 2.1.2 Triggers before callbacks
              $isNew = $this->isNew;
              $this->triggerCallback('beforeSave');
              $this->triggerCallback($isNew ? 'beforeCreate' : 'beforeUpdate');
  
              // 2.1.3.1 Inserts new record
              if ($isNew) {
                  // Removes primary key value when it's empty to avoid SQL error
                  if (array_key_exists($this->primaryKey, $this->data) && !$this->data[$this->primaryKey]) {
                      unset($this->data[$this->primaryKey]);
                  }
  
                  $this->db->insert($this->table, $this->data);
                  $this->isNew = false;
  
                  // Receives primary key value when it's empty
                  if (!isset($this->data[$this->primaryKey]) || !$this->data[$this->primaryKey]) {
                      // Prepare sequence name for PostgreSQL
                      $sequence = sprintf('%s_%s_seq', $this->fullTable, $this->primaryKey);
                      $this->data[$this->primaryKey] = $this->db->lastInsertId($sequence);
                  }
                  // 2.1.3.2 Updates existing record
              } else {
                  if ($this->isChanged) {
                      $data = array_intersect_key($this->data, $this->changedData);
                      $this->db->update($this->table, $data, array(
                          $this->primaryKey => $this->data[$this->primaryKey]
                      ));
                  }
              }
  
              // 2.1.4 Reset changed data and changed status
              $this->changedData = array();
              $this->isChanged = false;
  
              // 2.1.5. Triggers after callbacks
              $this->triggerCallback($isNew ? 'afterCreate' : 'afterUpdate');
              $this->triggerCallback('afterSave');
              // 2.2 Loop and save collection records
          } else {
              foreach ($this->data as $record) {
                  $record->save();
              }
          }
  
          return $this;
      }
code: public function save($data = array())
      {
          // 1. Merges data from parameters
          $data && $this->fromArray($data);
  
          // 2.1 Saves single record
          if (!$this->isColl) {
  
              // 2.1.1 Returns when record has been destroy to avoid store dirty data
              if ($this->isDestroyed) {
                  return $this;
              }
  
              // Deletes the record when it's waiting to remove from database
              if ($this->detached) {
                  $this->db->delete($this->table, array($this->primaryKey => $this->data[$this->primaryKey]));
                  $this->isDestroyed = true;
                  return $this;
              }
  
              // 2.1.2 Triggers before callbacks
              $isNew = $this->isNew;
              $this->triggerCallback('beforeSave');
              $this->triggerCallback($isNew ? 'beforeCreate' : 'beforeUpdate');
  
              // 2.1.3.1 Inserts new record
              if ($isNew) {
                  // Removes primary key value when it's empty to avoid SQL error
                  if (array_key_exists($this->primaryKey, $this->data) && !$this->data[$this->primaryKey]) {
                      unset($this->data[$this->primaryKey]);
                  }
  
                  $this->db->insert($this->table, $this->data);
                  $this->isNew = false;
  
                  // Receives primary key value when it's empty
                  if (!isset($this->data[$this->primaryKey]) || !$this->data[$this->primaryKey]) {
                      // Prepare sequence name for PostgreSQL
                      $sequence = sprintf('%s_%s_seq', $this->fullTable, $this->primaryKey);
                      $this->data[$this->primaryKey] = $this->db->lastInsertId($sequence);
                  }
                  // 2.1.3.2 Updates existing record
              } else {
                  if ($this->isChanged) {
                      $data = array_intersect_key($this->data, $this->changedData);
                      $this->db->update($this->table, $data, array(
                          $this->primaryKey => $this->data[$this->primaryKey]
                      ));
                  }
              }
  
              // 2.1.4 Reset changed data and changed status
              $this->changedData = array();
              $this->isChanged = false;
  
              // 2.1.5. Triggers after callbacks
              $this->triggerCallback($isNew ? 'afterCreate' : 'afterUpdate');
              $this->triggerCallback('afterSave');
              // 2.2 Loop and save collection records
          } else {
              foreach ($this->data as $record) {
                  $record->save();
              }
          }
  
          return $this;
      }
code_tokens: public function save ( $ data = array ( ) ) { // 1. Merges data from parameters $ data && $ this -> fromArray ( $ data ) ; // 2.1 Saves single record if ( ! $ this -> isColl ) { // 2.1.1 Returns when record has been destroy to avoid store dirty data if ( $ this -> isDestroyed ) { return $ this ; } // Deletes the record when it's waiting to remove from database if ( $ this -> detached ) { $ this -> db -> delete ( $ this -> table , array ( $ this -> primaryKey => $ this -> data [ $ this -> primaryKey ] ) ) ; $ this -> isDestroyed = true ; return $ this ; } // 2.1.2 Triggers before callbacks $ isNew = $ this -> isNew ; $ this -> triggerCallback ( 'beforeSave' ) ; $ this -> triggerCallback ( $ isNew ? 'beforeCreate' : 'beforeUpdate' ) ; // 2.1.3.1 Inserts new record if ( $ isNew ) { // Removes primary key value when it's empty to avoid SQL error if ( array_key_exists ( $ this -> primaryKey , $ this -> data ) && ! $ this -> data [ $ this -> primaryKey ] ) { unset ( $ this -> data [ $ this -> primaryKey ] ) ; } $ this -> db -> insert ( $ this -> table , $ this -> data ) ; $ this -> isNew = false ; // Receives primary key value when it's empty if ( ! isset ( $ this -> data [ $ this -> primaryKey ] ) || ! $ this -> data [ $ this -> primaryKey ] ) { // Prepare sequence name for PostgreSQL $ sequence = sprintf ( '%s_%s_seq' , $ this -> fullTable , $ this -> primaryKey ) ; $ this -> data [ $ this -> primaryKey ] = $ this -> db -> lastInsertId ( $ sequence ) ; } // 2.1.3.2 Updates existing record } else { if ( $ this -> isChanged ) { $ data = array_intersect_key ( $ this -> data , $ this -> changedData ) ; $ this -> db -> update ( $ this -> table , $ data , array ( $ this -> primaryKey => $ this -> data [ $ this -> primaryKey ] ) ) ; } } // 2.1.4 Reset changed data and changed status $ this -> changedData = array ( ) ; $ this -> isChanged = false ; // 2.1.5. Triggers after callbacks $ this -> triggerCallback ( $ isNew ? 'afterCreate' : 'afterUpdate' ) ; $ this -> triggerCallback ( 'afterSave' ) ; // 2.2 Loop and save collection records } else { foreach ( $ this -> data as $ record ) { $ record -> save ( ) ; } } return $ this ; }
docstring: Save the record or data to database
  
  @param array $data
  @return $this
docstring_tokens: Save the record or data to database