src/Database/ORM/Traits/Mapping.php
/**
	 * Saves changes to the row or creates the row if it doesn't exist.
	 */
Saves changes to the row or creates the row if it doesn't exist.
Saves changes to the row or creates the row if it doesn ' t exist .
public function save() {
		$keyName = $this->getPrimaryKey();
		$connection = DB::connection($this->getConnection());
		$changes = array_map(function(ModelField $field) {
			return $field->remoteFormat;
		}, $this->rowFieldsPending);
		if (!$this->hasPrimaryKey()) {
			$builder = $connection->insert()->into($this->getTable())->values($changes);
			$returned = $builder->exec();
			if (!is_array($keyName) && !array_key_exists($keyName, $this->rowFieldsPending)) {
				$this->writeCommittedField($keyName, $returned);
			}
			$connection->getDatabase()->cache()->saveModelInstance($this);
			$this->emit('inserted', $returned);
			$this->initWithData();
		}
		else {
			if (empty($this->rowFieldsPending)) {
				return;
			}
			if (!empty($this->rowFieldsEqualize)) {
				foreach ($this->rowFieldsEqualize as $fieldName) {
					$changes[$fieldName] = ref($fieldName);
				}
			}
			$this->createUpdateQuery()->values($changes)->exec();
			$this->emit('updated');
		}
		foreach ($this->rowFieldsPending as $fieldName => $field) {
			$this->writeCommittedField($fieldName, $field->remoteFormat);
			$this->emit('property', $fieldName, $field->localFormat);
		}
		$this->emit('saved', $changes);
		$this->rowFieldsPending = array();
		$this->rowFieldsEqualize = array();
	}
public function save ( ) { $ keyName = $ this -> getPrimaryKey ( ) ; $ connection = DB :: connection ( $ this -> getConnection ( ) ) ; $ changes = array_map ( function ( ModelField $ field ) { return $ field -> remoteFormat ; } , $ this -> rowFieldsPending ) ; if ( ! $ this -> hasPrimaryKey ( ) ) { $ builder = $ connection -> insert ( ) -> into ( $ this -> getTable ( ) ) -> values ( $ changes ) ; $ returned = $ builder -> exec ( ) ; if ( ! is_array ( $ keyName ) && ! array_key_exists ( $ keyName , $ this -> rowFieldsPending ) ) { $ this -> writeCommittedField ( $ keyName , $ returned ) ; } $ connection -> getDatabase ( ) -> cache ( ) -> saveModelInstance ( $ this ) ; $ this -> emit ( 'inserted' , $ returned ) ; $ this -> initWithData ( ) ; } else { if ( empty ( $ this -> rowFieldsPending ) ) { return ; } if ( ! empty ( $ this -> rowFieldsEqualize ) ) { foreach ( $ this -> rowFieldsEqualize as $ fieldName ) { $ changes [ $ fieldName ] = ref ( $ fieldName ) ; } } $ this -> createUpdateQuery ( ) -> values ( $ changes ) -> exec ( ) ; $ this -> emit ( 'updated' ) ; } foreach ( $ this -> rowFieldsPending as $ fieldName => $ field ) { $ this -> writeCommittedField ( $ fieldName , $ field -> remoteFormat ) ; $ this -> emit ( 'property' , $ fieldName , $ field -> localFormat ) ; } $ this -> emit ( 'saved' , $ changes ) ; $ this -> rowFieldsPending = array ( ) ; $ this -> rowFieldsEqualize = array ( ) ; }
Saves changes to the row or creates the row if it doesn't exist.
Saves changes to the row or creates the row if it doesn ' t exist .
// Map the changes into their remote formats // Create a new row if there is no value for the primary key // Save the new primary key value // Save the instance to cache // Emit the inserted event // Initialize with data // Update an existing row // Skip if there are no pending changes // Set fields in the equalize store equal to themselves // Commit all pending changes // Emit a property-set event