protected function do_save_as_insert() {

		if ( static::table() instanceof TimestampedTable ) {
			$time = $this->fresh_timestamp();

			$this->set_attribute( static::table()->get_created_at_column(), $time );
			$this->set_attribute( static::table()->get_updated_at_column(), $time );
		}

		$this->fire_model_event( 'creating' );

		$columns = static::table()->get_columns();
		$insert  = array();

		foreach ( $this->get_raw_attributes() as $attribute => $value ) {
			$insert[ $attribute ] = $columns[ $attribute ]->prepare_for_storage( $value );
		}

		$insert_id = static::make_query_object()->insert( $insert );

		if ( $insert_id ) {
			$this->set_raw_attribute( static::table()->get_primary_key(), $insert_id );
		}

		$default_columns_to_fill = array();

		foreach ( static::table()->get_columns() as $name => $column ) {

			if ( ! array_key_exists( $name, $this->get_raw_attributes() ) ) {
				$default_columns_to_fill[] = $name;
			}
		}

		if ( $default_columns_to_fill ) {

			$query          = new FluentQuery( static::table() );
			$default_values = $query->where( static::table()->get_primary_key(), '=', $this->get_pk() )
			                        ->select( $default_columns_to_fill )->first();

			if ( $default_values ) {
				foreach ( $default_values as $column => $value ) {
					$this->set_raw_attribute( $column, $value );
				}
			}
		}

		$this->_exists = true;

		if ( static::$_cache ) {
			Cache::update( $this );
		}

		$this->fire_model_event( 'created' );

		return true;
	}
protected function do_save_as_insert() {

		if ( static::table() instanceof TimestampedTable ) {
			$time = $this->fresh_timestamp();

			$this->set_attribute( static::table()->get_created_at_column(), $time );
			$this->set_attribute( static::table()->get_updated_at_column(), $time );
		}

		$this->fire_model_event( 'creating' );

		$columns = static::table()->get_columns();
		$insert  = array();

		foreach ( $this->get_raw_attributes() as $attribute => $value ) {
			$insert[ $attribute ] = $columns[ $attribute ]->prepare_for_storage( $value );
		}

		$insert_id = static::make_query_object()->insert( $insert );

		if ( $insert_id ) {
			$this->set_raw_attribute( static::table()->get_primary_key(), $insert_id );
		}

		$default_columns_to_fill = array();

		foreach ( static::table()->get_columns() as $name => $column ) {

			if ( ! array_key_exists( $name, $this->get_raw_attributes() ) ) {
				$default_columns_to_fill[] = $name;
			}
		}

		if ( $default_columns_to_fill ) {

			$query          = new FluentQuery( static::table() );
			$default_values = $query->where( static::table()->get_primary_key(), '=', $this->get_pk() )
			                        ->select( $default_columns_to_fill )->first();

			if ( $default_values ) {
				foreach ( $default_values as $column => $value ) {
					$this->set_raw_attribute( $column, $value );
				}
			}
		}

		$this->_exists = true;

		if ( static::$_cache ) {
			Cache::update( $this );
		}

		$this->fire_model_event( 'created' );

		return true;
	}
protected function do_save_as_insert ( ) { if ( static :: table ( ) instanceof TimestampedTable ) { $ time = $ this -> fresh_timestamp ( ) ; $ this -> set_attribute ( static :: table ( ) -> get_created_at_column ( ) , $ time ) ; $ this -> set_attribute ( static :: table ( ) -> get_updated_at_column ( ) , $ time ) ; } $ this -> fire_model_event ( 'creating' ) ; $ columns = static :: table ( ) -> get_columns ( ) ; $ insert = array ( ) ; foreach ( $ this -> get_raw_attributes ( ) as $ attribute => $ value ) { $ insert [ $ attribute ] = $ columns [ $ attribute ] -> prepare_for_storage ( $ value ) ; } $ insert_id = static :: make_query_object ( ) -> insert ( $ insert ) ; if ( $ insert_id ) { $ this -> set_raw_attribute ( static :: table ( ) -> get_primary_key ( ) , $ insert_id ) ; } $ default_columns_to_fill = array ( ) ; foreach ( static :: table ( ) -> get_columns ( ) as $ name => $ column ) { if ( ! array_key_exists ( $ name , $ this -> get_raw_attributes ( ) ) ) { $ default_columns_to_fill [ ] = $ name ; } } if ( $ default_columns_to_fill ) { $ query = new FluentQuery ( static :: table ( ) ) ; $ default_values = $ query -> where ( static :: table ( ) -> get_primary_key ( ) , '=' , $ this -> get_pk ( ) ) -> select ( $ default_columns_to_fill ) -> first ( ) ; if ( $ default_values ) { foreach ( $ default_values as $ column => $ value ) { $ this -> set_raw_attribute ( $ column , $ value ) ; } } } $ this -> _exists = true ; if ( static :: $ _cache ) { Cache :: update ( $ this ) ; } $ this -> fire_model_event ( 'created' ) ; return true ; }
Save model as an insert query.

@since 2.0

@return bool

@throws Exception
Save model as an insert query .