ng all dirty fields throughout the models and // setting them to null if they don't exist in each model. foreach ( $models_chunk as $model ) { $model_values = []; foreach ( $dirty_column_names as $dirty_column ) { // Set the value to null if it hasn't been set already. if ( ! isset( $model->orm->dirty_fields[ $dirty_column ] ) ) { $model->orm->dirty_fields[ $dirty_column ] = null; } // Only register the value if it is not null. if ( ! is_null( $model->orm->dirty_fields[ $dirty_column ] ) ) { $model_values[] = $model->orm->dirty_fields[ $dirty_column ]; } } $values = \array_merge( $values, $model_values ); } // We now have the same set of dirty columns in all our models and also gathered all values. $query = $this->build_insert_many( $models_chunk, $dirty_column_names ); $success = $success && (bool) self::execute( $query, $values ); } return $success; } /** * Updates many records in the database. * * @return int|bool The number of rows changed if the query was succesful. False otherwise. */ public function update_many() { // Remove any expression fields as they are already baked into the query. $values = \array_values( \array_diff_key( $this->dirty_fields, $this->expr_fields ) ); // UPDATE. // If there are no dirty values, do nothing. if ( empty( $values ) && empty( $this->expr_fields ) ) { return true; } $query = $this->join_if_not_empty( ' ', [ $this->build_update(), $this->build_where() ] ); $success = self::execute( $query, \array_merge( $values, $this->values ) ); $this->dirty_fields = []; $this->expr_fields = []; return $success; } /** * Adds a WHERE clause for every column that belongs to the primary key. * * @return string The where part of the query. */ public function add_id_column_conditions() { $query = []; $query[] = 'WHERE'; $keys = \is_array( $this->get_id_column_name() ) ? $this->get_id_column_name() : [ $this->get_id_column_name() ]; $first = true; foreach ( $keys as $key ) { if ( $first ) { $first = false; } else { $query[] = 'AND'; } $query[] = $this->quote_identifier( $key ); $query[] = '= %s'; } return \implode( ' ', $query ); } /** * Builds an UPDATE query. * * @return string The update query. */ protected function build_update() { $query = []; $query[] = "UPDATE {$this->quote_identifier($this->table_name)} SET"; $field_list = []; foreach ( $this->dirty_fields as $key => $value ) { if ( ! \array_key_exists( $key, $this->expr_fields ) ) { $value = ( $value === null ) ? 'NULL' : '%s'; } $field_list[] = "{$this->quote_identifier($key)} = {$value}"; } $query[] = \implode( ', ', $field_list ); return \implode( ' ', $query ); } /** * Builds an INSERT query. * * @return string The insert query. */ protected function build_insert() { $query = []; $query[] = 'INSERT INTO'; $query[] = $this->quote_identifier( $this->table_name ); $field_list = \array_map( [ $this, 'quote_identifier' ], \array_keys( $this->dirty_fields ) ); $query[] = '(' . \implode( ', ', $field_list ) . ')'; $query[] = 'VALUES'; $placeholders = $this->create_placeholders( $this->dirty_fields ); $query[] = "({$placeholders})"; return \implode( ' ', $query ); } /** * Builds a bulk INSERT query. * * @param array $models Array of model instances to be inserted. * @param array $dirty_column_names Array of dirty fields to be used in INSERT. * * @return string The insert query. */ protected function build_insert_many( $models, $dirty_column_names ) { $example_model = $models[0]; $total_placeholders = ''; $query = []; $query[] = 'INSERT INTO'; $query[] = $this->quote_identifier( $example_model->orm->table_name ); $field_list = \array_map( [ $this, 'quote_identifier' ], $dirty_column_names ); $query[] = '(' . \implode( ', ', $field_list ) . ')'; $query[] = 'VALUES'; // We assign placeholders per model for dirty fields that have values and NULL for dirty fields that don't. foreach ( $models as $model ) { $placeholder = []; foreach ( $dirty_column_names as $dirty_field ) { $placeholder[] = ( $model->orm->dirty_fields[ $dirty_field ] === null ) ? 'NULL' : '%s'; } $placeholders = \implode( ', ', $placeholder ); $total_placeholders .= "({$placeholders}),"; } $query[] = \rtrim( $total_placeholders, ',' ); return \implode( ' ', $query ); } /** * Deletes this record from the database. * * @return string The delete query. * * @throws \Exception Primary key ID contains null value(s). * @throws \Exception Primary key ID missing from row or is null. */ public function delete() { $query = [ 'DELETE FROM', $this->quote_identifier( $this->table_name ), $this->add_id_column_conditions() ]; return self::execute( \implode( ' ', $query ), \is_array( $this->id( true ) ) ? \array_values( $this->id( true ) ) : [ $this->id( true ) ] ); } /** * Deletes many records from the database. * * @return bool|int Response of wpdb::query. */ public function delete_many() { // Build and return the full DELETE statement by concatenating // the results of calling each separate builder method. $query = $this->join_if_not_empty( ' ', [ 'DELETE FROM', $this->quote_identifier( $this->table_name ), $this->build_where(), ] ); return self::execute( $query, $this->values ); } /* * --- ArrayAccess --- */ /** * Checks whether the data has the key. * * @param mixed $offset Key. * * @return bool Whether the data has the key. */ #[ReturnTypeWillChange] public function offsetExists( $offset ) { return \array_key_exists( $offset, $this->data ); } /** * Retrieves the value of the key. * * @param mixed $offset Key. * * @return array|mixed|null The value. */ #[ReturnTypeWillChange] public function offsetGet( $offset ) { return $this->get( $offset ); } /** * Sets the value of the key. * * @param string|int $offset Key. * @param mixed $value Value. */ #[ReturnTypeWillChange] public function offsetSet( $offset, $value ) { if ( \is_null( $offset ) ) { return; } $this->set( $offset, $value ); } /** * Removes the given key from the data. * * @param mixed $offset Key. */ #[ReturnTypeWillChange] public function offsetUnset( $offset ) { unset( $this->data[ $offset ] ); unset( $this->dirty_fields[ $offset ] ); } /* * --- MAGIC METHODS --- */ /** * Handles magic get via offset. * * @param mixed $key Key. * * @return array|mixed|null The value in the offset. */ public function __get( $key ) { return $this->offsetGet( $key ); } /** * Handles magic set via offset. * * @param string|int $key Key. * @param mixed $value Value. */ public function __set( $key, $value ) { $this->offsetSet( $key, $value ); } /** * Handles magic unset via offset. * * @param mixed $key Key. */ public function __unset( $key ) { $this->offsetUnset( $key ); } /** * Handles magic isset via offset. * * @param mixed $key Key. * * @return bool Whether the offset has the key. */ public function __isset( $key ) { return $this->offsetExists( $key ); } }