/** * Adds the included models indicated in the request to the entity provided * @param \EEM_Base $model * @param \WP_REST_Request $rest_request * @param array $entity_array * @return array the modified entity */ protected function _include_requested_models(\EEM_Base $model, \WP_REST_Request $rest_request, $entity_array) { $includes_for_this_model = $this->explode_and_get_items_prefixed_with($rest_request->get_param('include'), ''); $includes_for_this_model = $this->_remove_model_names_from_array($includes_for_this_model); //if they passed in * or didn't specify any includes, return everything if (!in_array('*', $includes_for_this_model) && !empty($includes_for_this_model)) { if ($model->has_primary_key_field()) { //always include the primary key. ya just gotta know that at least $includes_for_this_model[] = $model->primary_key_name(); } if ($this->explode_and_get_items_prefixed_with($rest_request->get_param('calculate'), '')) { $includes_for_this_model[] = '_calculated_fields'; } $entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model)); } $relation_settings = $this->get_model_version_info()->relation_settings($model); foreach ($relation_settings as $relation_name => $relation_obj) { $related_fields_to_include = $this->explode_and_get_items_prefixed_with($rest_request->get_param('include'), $relation_name); $related_fields_to_calculate = $this->explode_and_get_items_prefixed_with($rest_request->get_param('calculate'), $relation_name); //did they specify they wanted to include a related model, or //specific fields from a related model? //or did they specify to calculate a field from a related model? if ($related_fields_to_include || $related_fields_to_calculate) { //if so, we should include at least some part of the related model $pretend_related_request = new \WP_REST_Request(); $pretend_related_request->set_query_params(array('caps' => $rest_request->get_param('caps'), 'include' => $related_fields_to_include, 'calculate' => $related_fields_to_calculate)); $pretend_related_request->add_header('no_rest_headers', true); $related_results = $this->get_entities_from_relation($entity_array[$model->primary_key_name()], $relation_obj, $pretend_related_request); $entity_array[Read::get_related_entity_name($relation_name, $relation_obj)] = $related_results instanceof \WP_Error ? null : $related_results; } } return $entity_array; }
/** * Given the model object data, finds the row to update and updates it * @param string|int $id_in_csv * @param array $model_object_data * @param EEM_Base $model * @param array $old_db_to_new_db_mapping * @return array updated $old_db_to_new_db_mapping */ protected function _update_from_data_array($id_in_csv, $model_object_data, $model, $old_db_to_new_db_mapping) { try { //let's keep two copies of the model object data: //one for performing an update, one for everthing else $model_object_data_for_update = $model_object_data; if ($model->has_primary_key_field()) { $conditions = array($model->primary_key_name() => $model_object_data[$model->primary_key_name()]); //remove the primary key because we shouldn't use it for updating unset($model_object_data_for_update[$model->primary_key_name()]); } elseif ($model->get_combined_primary_key_fields() > 1) { $conditions = array(); foreach ($model->get_combined_primary_key_fields() as $key_field) { $conditions[$key_field->get_name()] = $model_object_data[$key_field->get_name()]; } } else { $model->primary_key_name(); //this shoudl just throw an exception, explaining that we dont have a primary key (or a combine dkey) } $success = $model->update($model_object_data_for_update, array($conditions)); if ($success) { $this->_total_updates++; EE_Error::add_success(sprintf(__("Successfully updated %s with csv data %s", "event_espresso"), $model->get_this_model_name(), implode(",", $model_object_data_for_update))); //we should still record the mapping even though it was an update //because if we were going to insert somethign but it was going to conflict //we would have last-minute decided to update. So we'd like to know what we updated //and so we record what record ended up being updated using the mapping if ($model->has_primary_key_field()) { $new_key_for_mapping = $model_object_data[$model->primary_key_name()]; } else { //no primary key just a combined key $new_key_for_mapping = $model->get_index_primary_key_string($model_object_data); } $old_db_to_new_db_mapping[$model->get_this_model_name()][$id_in_csv] = $new_key_for_mapping; } else { $matched_items = $model->get_all(array($conditions)); if (!$matched_items) { //no items were matched (so we shouldn't have updated)... but then we should have inserted? what the heck? $this->_total_update_errors++; EE_Error::add_error(sprintf(__("Could not update %s with the csv data: '%s' for an unknown reason (using WHERE conditions %s)", "event_espresso"), $model->get_this_model_name(), http_build_query($model_object_data), http_build_query($conditions)), __FILE__, __FUNCTION__, __LINE__); } else { $this->_total_updates++; EE_Error::add_success(sprintf(__("%s with csv data '%s' was found in the database and didn't need updating because all the data is identical.", "event_espresso"), $model->get_this_model_name(), implode(",", $model_object_data))); } } } catch (EE_Error $e) { $this->_total_update_errors++; $basic_message = sprintf(__("Could not update %s with the csv data: %s because %s", "event_espresso"), $model->get_this_model_name(), implode(",", $model_object_data), $e->getMessage()); $debug_message = $basic_message . ' Stack trace: ' . $e->getTraceAsString(); EE_Error::add_error("{$basic_message} | {$debug_message}", __FILE__, __FUNCTION__, __LINE__); } return $old_db_to_new_db_mapping; }