Model::save( array $options = array() )

Save the model to the database.


Parameters

$options

(array) (Optional)

Default value: array()


Return

(bool|mixed)


Source

File: Model/Model.php

	public function save( array $options = [] ) {
		global $wpdb;

		$columns = $this->db_columns( $options );

		$is_insert = empty( $columns[ $this->primaryKey ] );

		if ( $is_insert ) {

			if ( $this->timestamp ) {
				$columns['created_at'] = dozent_mysql_time();
			}

			$wpdb->insert( $this->table, $columns );

			$insert_id = (int) $wpdb->insert_id;

		} else {

			$updated_columns = dozent_array_only( get_object_vars( $this ), array_keys( $columns ) );
			unset( $updated_columns[ $this->primaryKey ] );

			$wpdb->update( $this->table, $updated_columns, [ $this->primaryKey => $columns[ $this->primaryKey ] ] );
			$insert_id = (int) $columns[ $this->primaryKey ];
			$columns = array_merge( $columns, $updated_columns );
		}

		if ( $insert_id ) {
			$columns[ $this->primaryKey ] = $insert_id;

			$this->syncModel( $columns );

			return $this;
		}


		return new \WP_Error( 'dozent_model_saving_error', __( ' Something went wrong ', 'dozent' ) );
	}