Submission::sync_upstream()

Synchronizes the model with the database by storing the currently pending values.

Description

If the model is new (i.e. does not have an ID yet), it will be inserted to the database.

See also

Return

(true|WP_Error) True on success, or an error object on failure.

Source

File: src/db-objects/submissions/submission.php

	public function sync_upstream() {
		$result = parent::sync_upstream();

		if ( is_wp_error( $result ) ) {
			return $result;
		}

		if ( null !== $this->values ) {
			$manager = $this->manager->get_child_manager( 'submission_values' );

			$ids = array();
			foreach ( $this->values as $item ) {
				$submission_value = null;
				if ( ! empty( $item['id'] ) ) {
					$submission_value = $manager->get( $item['id'] );
					if ( $submission_value && $this->id !== $submission_value->submission_id ) {
						continue;
					}
				}

				if ( ! $submission_value ) {
					$submission_value = $manager->create();
				}

				$submission_value->submission_id = $this->id;
				$submission_value->field         = $item['field'];
				$submission_value->value         = $item['value'];
				if ( ! empty( $item['element_id'] ) ) {
					$submission_value->element_id = $item['element_id'];
				}

				$submission_value->sync_upstream();

				if ( empty( $submission_value->id ) ) {
					continue;
				}

				$ids[] = $submission_value->id;
			}

			if ( ! empty( $ids ) ) {
				$old_values = $this->get_submission_values( array(
					'exclude' => $ids,
				) );
				foreach ( $old_values as $old_value ) {
					$old_value->delete();
				}
			}

			$this->values = null;
		}

		return $result;
	}

Changelog

Changelog
Version Description
1.0.0 Introduced.