/** * get_join_statement * @param string $model_relation_chain * @return string * @throws \EE_Error */ public function get_join_statement($model_relation_chain) { //create the sql string like $this_table_fk_field = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, $this->get_this_model()->get_this_model_name()) . $this_table_fk_field->get_table_alias(); $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); return $this->_left_join($other_table, $other_table_alias, $other_table_pk_field->get_table_column(), $this_table_alias, $this_table_fk_field->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); }
function get_join_statement($model_relation_chain) { //create the sql string like // LEFT JOIN other_table AS table_alias ON this_table_alias.pk = other_table_alias.fk extra_join_conditions $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); $other_table_fk_field = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, $this->get_this_model()->get_this_model_name()) . $this_table_pk_field->get_table_alias(); $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, $this->get_other_model()->get_this_model_name()) . $other_table_fk_field->get_table_alias(); $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); $field_with_model_name = $this->get_other_model()->get_field_containing_related_model_name(); return $this->_left_join($fk_table, $fk_table_alias, $other_table_fk_field->get_table_column(), $pk_table_alias, $this_table_pk_field->get_table_column(), $field_with_model_name->get_qualified_column() . "='" . $this->get_this_model()->get_this_model_name() . "'") . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); }
/** * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" * If you want to join between modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), * you should prepend the result of this function with results from get_join_to_intermediate_model_statement(), * so that you join first to the intermediate join table, and then to the other model's pk's table * * @param string $model_relation_chain like 'Event.Event_Venue.Venue' * @return string of SQL * @throws \EE_Error */ public function get_join_statement($model_relation_chain) { if ($this->_model_relation_chain_to_join_model === NULL) { throw new EE_Error(sprintf(__('When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', 'event_espresso'))); } $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($this->_model_relation_chain_to_join_model, $this->get_join_model()->get_this_model_name()) . $join_table_fk_field_to_this_table->get_table_alias(); $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, $this->get_other_model()->get_this_model_name()) . $other_table_pk_field->get_table_alias(); $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); $SQL = $this->_left_join($other_table, $other_table_alias, $other_table_pk_field->get_table_column(), $join_table_alias, $join_table_fk_field_to_other_table->get_table_column()) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); return $SQL; }
/** * Gets an array of columns to select for this model, which are necessary for it to create its objects. * So that's going to be the columns for all the fields on the model * @param string $model_relation_chain like 'Question.Question_Group.Event' * @return array numerically indexed, values are columns to select and rename, eg "Event.ID AS 'Event.ID'" */ public function _get_columns_to_select_for_this_model($model_relation_chain = '') { $fields = $this->field_settings(); $selects = array(); $table_alias_with_model_relation_chain_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($model_relation_chain, $this->get_this_model_name()); foreach ($fields as $field_obj) { $selects[] = $table_alias_with_model_relation_chain_prefix . $field_obj->get_table_alias() . "." . $field_obj->get_table_column() . " AS '" . $table_alias_with_model_relation_chain_prefix . $field_obj->get_table_alias() . "." . $field_obj->get_table_column() . "'"; } //make sure we are also getting the PKs of each table $tables = $this->get_tables(); if (count($tables) > 1) { foreach ($tables as $table_obj) { $qualified_pk_column = $table_alias_with_model_relation_chain_prefix . $table_obj->get_fully_qualified_pk_column(); if (!in_array($qualified_pk_column, $selects)) { $selects[] = "{$qualified_pk_column} AS '{$qualified_pk_column}'"; } } } return $selects; }
/** * Gets the table alias model relation chain prefix (ie, what can be prepended onto * EE_Model_Field::get_qualified_column() to get the proper column name for that field * in a specific query) from teh query param (eg 'Registration.Event.EVT_ID'). * * @param string $model_name of the model on which the related query param was found to be belong * @param string $original_query_param * @return string */ public static function extract_table_alias_model_relation_chain_from_query_param($model_name, $original_query_param) { $relation_chain = self::extract_model_relation_chain($model_name, $original_query_param); $table_alias_with_model_relation_chain_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($relation_chain, $model_name); return $table_alias_with_model_relation_chain_prefix; }