/** * Overrides parent so that after an update, we also check the term_taxonomy_counts are * all ok * @param array $fields_n_values see EEM_Base::update * @param array $query_params @see EEM_Base::get_all * @param boolean $keep_model_objs_in_sync if TRUE, makes sure we ALSO update model objects * in this model's entity map according to $fields_n_values that match $query_params. This * obviously has some overhead, so you can disable it by setting this to FALSE, but * be aware that model objects being used could get out-of-sync with the database * @return int */ public function update($fields_n_values, $query_params, $keep_model_objs_in_sync = TRUE) { $count = parent::update($fields_n_values, $query_params, $keep_model_objs_in_sync); if ($count) { $this->update_term_taxonomy_counts(); } return $count; }
/** * Note that this model is a SOFT-DELETABLE model! That means that, by default, trashed/soft-deleted * items are ignored in queries. So for this update, soft-deleted items will be ignroed. * If you want to update soft-deleted items also, use update_deleted_and_undeleted instead. * @param array $fields_n_values like EEM_base::update's $fields_n_values * @param array $query_params like EEM_base::get_all's $query_params * @param boolean $keep_model_objs_in_sync if TRUE, makes sure we ALSO update model objects * in this model's entity map according to $fields_n_values that match $query_params. This * obviously has some overhead, so you can disable it by setting this to FALSE, but * be aware that model objects being used could get out-of-sync with the database * @return int how many items were updated */ public function update($fields_n_values, $query_params, $keep_model_objs_in_sync = TRUE) { return parent::update($fields_n_values, $query_params, $keep_model_objs_in_sync); }
/** * 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; }