/** * Inserts a new row in $table, using the $cols_n_values which apply to that table. * If a $new_id is supplied and if $table is an EE_Other_Table, we assume * we need to add a foreign key column to point to $new_id (which should be the primary key's value * on the main table) * This is protected rather than private because private is not accessible to any child methods and there MAY be cases where we want to call it directly rather than via insert(). * @access protected * @param EE_Table_Base $table * @param array $fields_n_values each key should be in field's keys, and value should be an int, string or float * @param int $new_id for now we assume only int keys * @throws EE_Error * @global WPDB $wpdb only used to get the $wpdb->insert_id after performing an insert * @return int ID of new row inserted, or FALSE on failure */ protected function _insert_into_specific_table(EE_Table_Base $table, $fields_n_values, $new_id = 0) { global $wpdb; $insertion_col_n_values = array(); $format_for_insertion = array(); $fields_on_table = $this->_get_fields_for_table($table->get_table_alias()); foreach ($fields_on_table as $field_name => $field_obj) { //check if its an auto-incrementing column, in which case we should just leave it to do its autoincrement thing if ($field_obj->is_auto_increment()) { continue; } $prepared_value = $this->_prepare_value_or_use_default($field_obj, $fields_n_values); //if the value we want to assign it to is NULL, just don't mention it for the insertion if ($prepared_value !== NULL) { $insertion_col_n_values[$field_obj->get_table_column()] = $prepared_value; $format_for_insertion[] = $field_obj->get_wpdb_data_type(); } } if ($table instanceof EE_Secondary_Table && $new_id) { //its not the main table, so we should have already saved the main table's PK which we just inserted //so add the fk to the main table as a column $insertion_col_n_values[$table->get_fk_on_table()] = $new_id; $format_for_insertion[] = '%d'; //yes right now we're only allowing these foreign keys to be INTs } //insert the new entry $result = $this->_do_wpdb_query('insert', array($table->get_table_name(), $insertion_col_n_values, $format_for_insertion)); if ($result === false) { return false; } //ok, now what do we return for the ID of the newly-inserted thing? if ($this->has_primary_key_field()) { if ($this->get_primary_key_field()->is_auto_increment()) { return $wpdb->insert_id; } else { //it's not an auto-increment primary key, so //it must have been supplied return $fields_n_values[$this->get_primary_key_field()->get_name()]; } } else { //we can't return a primary key because there is none. instead return //a unique string indicating this model return $this->get_index_primary_key_string($fields_n_values); } }
/** * * @global type $wpdb * @param string $table_name with or without wpdb prefix * @param string $pk_column name of primary key column * @param boolean $global whether the table is "global" as in there is only 1 table on an entire multisite install, * or whether each site on a multisite install has a copy of this table */ function __construct($table_name, $pk_column = null, $global = false) { parent::__construct($table_name, $pk_column, $global); }
function __construct($table_name, $pk_column = null) { parent::__construct($table_name, $pk_column); }
/** * * @global type $wpdb * @param string $table_name with or without wpdb prefix * @param string $pk_column name of primary key column on THIS table * @param string $fk_column the name of the COLUMN that is a foreign key to the primary table's primary key * @param string $extra_join_conditions string for additional SQL to add onto the join statement's ON condition * @param boolean $global whether the table is "global" as in there is only 1 table on an entire multisite install, * or whether each site on a multisite install has a copy of this table */ function __construct($table_name, $pk_column, $fk_column = null, $extra_join_conditions = null, $global = false) { $this->_fk_on_table = $fk_column; $this->_extra_join_conditions = $extra_join_conditions; parent::__construct($table_name, $pk_column, $global); }
/** * Inserts a new row in $table, using the $cols_n_values which apply to that table. * If a $new_id is supplied and if $table is an EE_Other_Table, we assume * we need to add a foreign key column to point to $new_id (which should be the primary key's value * on the main table) * This is protected rather than private because private is not accessible to any child methods and there MAY be cases where we want to call it directly rather than via insert(). * @access protected * @param EE_Table_Base $table * @param array $fields_n_values each key should be in _field's keys, and value should be an int, string or float * @param bool|int $new_id * @throws EE_Error * @internal param int $new_id for now we assume only int keys * @return int ID of new row inserted */ protected function _insert_into_specific_table(EE_Table_Base $table, $fields_n_values, $new_id = false) { global $wpdb; $insertion_col_n_values = array(); $format_for_insertion = array(); $fields_on_table = $this->_get_fields_for_table($table->get_table_alias()); foreach ($fields_on_table as $field_name => $field_obj) { //check if its an auto-incrementing column, in which case we should just leave it to do its autoincrement thing if ($field_obj->is_auto_increment()) { continue; } if (!isset($fields_n_values[$field_name]) || $fields_n_values[$field_name] === null) { //they didnt include this field. so just use default $insertion_col_n_values[$field_obj->get_table_column()] = $this->_prepare_value_for_use_in_db($field_obj->get_default_value(), $field_obj, true); } else { //they have specified the value for this field, so use it values_already_prepared_by_model_object $insertion_col_n_values[$field_obj->get_table_column()] = $this->_prepare_value_for_use_in_db($fields_n_values[$field_name], $field_obj); } $format_for_insertion[] = $field_obj->get_wpdb_data_type(); } if ($table instanceof EE_Secondary_Table && $new_id) { //its not the main table, so we should have already saved the main table's PK which we just inserted //so add the fk to the main table as a column $insertion_col_n_values[$table->get_fk_on_table()] = $new_id; $format_for_insertion[] = '%d'; //yes right now we're only allowing these foreign keys to be INTs } //insert the new entry $old_show_errors_value = $wpdb->show_errors; $wpdb->show_errors(false); $result = $wpdb->insert($table->get_table_name(), $insertion_col_n_values, $format_for_insertion); $wpdb->show_errors($old_show_errors_value); $this->show_db_query_if_previously_requested($wpdb->last_query); if (!$result) { throw new EE_Error(sprintf(__("Error inserting values %s for columns %s, using data types %s, into table %s. Error was %s", 'event_espresso'), implode(",", $insertion_col_n_values), implode(",", array_keys($insertion_col_n_values)), implode(",", $format_for_insertion), $table->get_table_name(), $wpdb->last_error)); } //ok, now what do we return for the ID of the newly-inserted thing? if ($this->has_primary_key_field()) { if ($this->get_primary_key_field()->is_auto_increment()) { return $wpdb->insert_id; } else { //it's not an auto-increment primary key, so //it must have been supplied return $fields_n_values[$this->get_primary_key_field()->get_name()]; } } else { //we can't return a primary key because there is none. instead return //a unique string indicating this model return $this->get_index_primary_key_string($fields_n_values); } }