@@ -9,261 +9,261 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class EE_HABTM_Relation extends EE_Model_Relation_Base |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Model which defines the relation between two other models. Eg, the EE_Event_Question_Group model, |
|
| 14 | - * which joins EE_Event and EE_Question_Group |
|
| 15 | - * |
|
| 16 | - * @var EEM_Base |
|
| 17 | - */ |
|
| 18 | - protected $_joining_model_name; |
|
| 12 | + /** |
|
| 13 | + * Model which defines the relation between two other models. Eg, the EE_Event_Question_Group model, |
|
| 14 | + * which joins EE_Event and EE_Question_Group |
|
| 15 | + * |
|
| 16 | + * @var EEM_Base |
|
| 17 | + */ |
|
| 18 | + protected $_joining_model_name; |
|
| 19 | 19 | |
| 20 | - protected $_model_relation_chain_to_join_model; |
|
| 20 | + protected $_model_relation_chain_to_join_model; |
|
| 21 | 21 | |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
| 25 | - * (and an ee joining-model.) This knows how to join the models, |
|
| 26 | - * get related models across the relation, and add-and-remove the relationships. |
|
| 27 | - * |
|
| 28 | - * @param bool $joining_model_name |
|
| 29 | - * @param boolean $block_deletes for this type of relation, we block by default for now. if there |
|
| 30 | - * are related models across this relation, block (prevent and add an |
|
| 31 | - * error) the deletion of this model |
|
| 32 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
| 33 | - * default |
|
| 34 | - */ |
|
| 35 | - public function __construct($joining_model_name, $block_deletes = true, $blocking_delete_error_message = '') |
|
| 36 | - { |
|
| 37 | - $this->_joining_model_name = $joining_model_name; |
|
| 38 | - parent::__construct($block_deletes, $blocking_delete_error_message); |
|
| 39 | - } |
|
| 23 | + /** |
|
| 24 | + * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
| 25 | + * (and an ee joining-model.) This knows how to join the models, |
|
| 26 | + * get related models across the relation, and add-and-remove the relationships. |
|
| 27 | + * |
|
| 28 | + * @param bool $joining_model_name |
|
| 29 | + * @param boolean $block_deletes for this type of relation, we block by default for now. if there |
|
| 30 | + * are related models across this relation, block (prevent and add an |
|
| 31 | + * error) the deletion of this model |
|
| 32 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
| 33 | + * default |
|
| 34 | + */ |
|
| 35 | + public function __construct($joining_model_name, $block_deletes = true, $blocking_delete_error_message = '') |
|
| 36 | + { |
|
| 37 | + $this->_joining_model_name = $joining_model_name; |
|
| 38 | + parent::__construct($block_deletes, $blocking_delete_error_message); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Gets the joining model's object |
|
| 43 | - * |
|
| 44 | - * @return EEM_Base |
|
| 45 | - */ |
|
| 46 | - public function get_join_model() |
|
| 47 | - { |
|
| 48 | - return $this->_get_model($this->_joining_model_name); |
|
| 49 | - } |
|
| 41 | + /** |
|
| 42 | + * Gets the joining model's object |
|
| 43 | + * |
|
| 44 | + * @return EEM_Base |
|
| 45 | + */ |
|
| 46 | + public function get_join_model() |
|
| 47 | + { |
|
| 48 | + return $this->_get_model($this->_joining_model_name); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
| 54 | - * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
| 55 | - * |
|
| 56 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
| 57 | - * @return string of SQL |
|
| 58 | - * @throws \EE_Error |
|
| 59 | - */ |
|
| 60 | - public function get_join_to_intermediate_model_statement($model_relation_chain) |
|
| 61 | - { |
|
| 62 | - // create sql like |
|
| 63 | - // LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
| 64 | - // LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
| 65 | - // remember the model relation chain to the JOIN model, because we'll |
|
| 66 | - // need it for get_join_statement() |
|
| 67 | - $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
| 68 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field();// get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 69 | - $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 70 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
| 71 | - $model_relation_chain, |
|
| 72 | - $this->get_this_model()->get_this_model_name() |
|
| 73 | - ) . $this_table_pk_field->get_table_alias(); |
|
| 52 | + /** |
|
| 53 | + * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
| 54 | + * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
| 55 | + * |
|
| 56 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
| 57 | + * @return string of SQL |
|
| 58 | + * @throws \EE_Error |
|
| 59 | + */ |
|
| 60 | + public function get_join_to_intermediate_model_statement($model_relation_chain) |
|
| 61 | + { |
|
| 62 | + // create sql like |
|
| 63 | + // LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
| 64 | + // LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
| 65 | + // remember the model relation chain to the JOIN model, because we'll |
|
| 66 | + // need it for get_join_statement() |
|
| 67 | + $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
| 68 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field();// get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 69 | + $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 70 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
| 71 | + $model_relation_chain, |
|
| 72 | + $this->get_this_model()->get_this_model_name() |
|
| 73 | + ) . $this_table_pk_field->get_table_alias(); |
|
| 74 | 74 | |
| 75 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
| 76 | - $model_relation_chain, |
|
| 77 | - $this->get_join_model()->get_this_model_name() |
|
| 78 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 79 | - $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
| 80 | - // phew! ok, we have all the info we need, now we can create the SQL join string |
|
| 81 | - $SQL = $this->_left_join( |
|
| 82 | - $join_table, |
|
| 83 | - $join_table_alias, |
|
| 84 | - $join_table_fk_field_to_this_table->get_table_column(), |
|
| 85 | - $this_table_alias, |
|
| 86 | - $this_table_pk_field->get_table_column() |
|
| 87 | - ) . $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
| 75 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
| 76 | + $model_relation_chain, |
|
| 77 | + $this->get_join_model()->get_this_model_name() |
|
| 78 | + ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 79 | + $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
| 80 | + // phew! ok, we have all the info we need, now we can create the SQL join string |
|
| 81 | + $SQL = $this->_left_join( |
|
| 82 | + $join_table, |
|
| 83 | + $join_table_alias, |
|
| 84 | + $join_table_fk_field_to_this_table->get_table_column(), |
|
| 85 | + $this_table_alias, |
|
| 86 | + $this_table_pk_field->get_table_column() |
|
| 87 | + ) . $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
| 88 | 88 | |
| 89 | - return $SQL; |
|
| 90 | - } |
|
| 89 | + return $SQL; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
| 95 | - * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
| 96 | - * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
| 97 | - * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
| 98 | - * the intermediate join table, and then to the other model's pk's table |
|
| 99 | - * |
|
| 100 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
| 101 | - * @return string of SQL |
|
| 102 | - * @throws \EE_Error |
|
| 103 | - */ |
|
| 104 | - public function get_join_statement($model_relation_chain) |
|
| 105 | - { |
|
| 106 | - if ($this->_model_relation_chain_to_join_model === null) { |
|
| 107 | - throw new EE_Error(sprintf(__( |
|
| 108 | - 'When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
| 109 | - 'event_espresso' |
|
| 110 | - ))); |
|
| 111 | - } |
|
| 112 | - $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 113 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
| 114 | - $this->_model_relation_chain_to_join_model, |
|
| 115 | - $this->get_join_model()->get_this_model_name() |
|
| 116 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 117 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
| 118 | - $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 119 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
| 120 | - $model_relation_chain, |
|
| 121 | - $this->get_other_model()->get_this_model_name() |
|
| 122 | - ) . $other_table_pk_field->get_table_alias(); |
|
| 123 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
| 93 | + /** |
|
| 94 | + * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
| 95 | + * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
| 96 | + * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
| 97 | + * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
| 98 | + * the intermediate join table, and then to the other model's pk's table |
|
| 99 | + * |
|
| 100 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
| 101 | + * @return string of SQL |
|
| 102 | + * @throws \EE_Error |
|
| 103 | + */ |
|
| 104 | + public function get_join_statement($model_relation_chain) |
|
| 105 | + { |
|
| 106 | + if ($this->_model_relation_chain_to_join_model === null) { |
|
| 107 | + throw new EE_Error(sprintf(__( |
|
| 108 | + 'When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
| 109 | + 'event_espresso' |
|
| 110 | + ))); |
|
| 111 | + } |
|
| 112 | + $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 113 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
| 114 | + $this->_model_relation_chain_to_join_model, |
|
| 115 | + $this->get_join_model()->get_this_model_name() |
|
| 116 | + ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 117 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
| 118 | + $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 119 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
| 120 | + $model_relation_chain, |
|
| 121 | + $this->get_other_model()->get_this_model_name() |
|
| 122 | + ) . $other_table_pk_field->get_table_alias(); |
|
| 123 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
| 124 | 124 | |
| 125 | - $SQL = $this->_left_join( |
|
| 126 | - $other_table, |
|
| 127 | - $other_table_alias, |
|
| 128 | - $other_table_pk_field->get_table_column(), |
|
| 129 | - $join_table_alias, |
|
| 130 | - $join_table_fk_field_to_other_table->get_table_column() |
|
| 131 | - ) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
| 132 | - return $SQL; |
|
| 133 | - } |
|
| 125 | + $SQL = $this->_left_join( |
|
| 126 | + $other_table, |
|
| 127 | + $other_table_alias, |
|
| 128 | + $other_table_pk_field->get_table_column(), |
|
| 129 | + $join_table_alias, |
|
| 130 | + $join_table_fk_field_to_other_table->get_table_column() |
|
| 131 | + ) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
| 132 | + return $SQL; |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | 135 | |
| 136 | - /** |
|
| 137 | - * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
| 138 | - * If the join table has additional columns (eg, the Event_Question_Group table has a is_primary column), then |
|
| 139 | - * you'll want to directly use the EEM_Event_Question_Group model to add the entry to the table and set those extra |
|
| 140 | - * columns' values |
|
| 141 | - * |
|
| 142 | - * @param EE_Base_Class|int $this_obj_or_id |
|
| 143 | - * @param EE_Base_Class|int $other_obj_or_id |
|
| 144 | - * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
| 145 | - * checking existing values and for setting new rows if |
|
| 146 | - * no exact matches. |
|
| 147 | - * @return EE_Base_Class |
|
| 148 | - * @throws \EE_Error |
|
| 149 | - */ |
|
| 150 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
| 151 | - { |
|
| 152 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 153 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 154 | - // check if such a relationship already exists |
|
| 155 | - $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 156 | - $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 136 | + /** |
|
| 137 | + * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
| 138 | + * If the join table has additional columns (eg, the Event_Question_Group table has a is_primary column), then |
|
| 139 | + * you'll want to directly use the EEM_Event_Question_Group model to add the entry to the table and set those extra |
|
| 140 | + * columns' values |
|
| 141 | + * |
|
| 142 | + * @param EE_Base_Class|int $this_obj_or_id |
|
| 143 | + * @param EE_Base_Class|int $other_obj_or_id |
|
| 144 | + * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
| 145 | + * checking existing values and for setting new rows if |
|
| 146 | + * no exact matches. |
|
| 147 | + * @return EE_Base_Class |
|
| 148 | + * @throws \EE_Error |
|
| 149 | + */ |
|
| 150 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
| 151 | + { |
|
| 152 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 153 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 154 | + // check if such a relationship already exists |
|
| 155 | + $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 156 | + $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 157 | 157 | |
| 158 | - $foreign_keys = $all_fields = array( |
|
| 159 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
| 160 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
| 161 | - ); |
|
| 158 | + $foreign_keys = $all_fields = array( |
|
| 159 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
| 160 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
| 161 | + ); |
|
| 162 | 162 | |
| 163 | - // if $where_query exists lets add them to the query_params. |
|
| 164 | - if (! empty($extra_join_model_fields_n_values)) { |
|
| 165 | - // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
| 166 | - // make sure we strip THIS models name from the query param |
|
| 167 | - $parsed_query = array(); |
|
| 168 | - foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
| 169 | - $query_param = str_replace( |
|
| 170 | - $this->get_join_model()->get_this_model_name() . ".", |
|
| 171 | - "", |
|
| 172 | - $query_param |
|
| 173 | - ); |
|
| 174 | - $parsed_query[ $query_param ] = $val; |
|
| 175 | - } |
|
| 176 | - $all_fields = array_merge($foreign_keys, $parsed_query); |
|
| 177 | - } |
|
| 163 | + // if $where_query exists lets add them to the query_params. |
|
| 164 | + if (! empty($extra_join_model_fields_n_values)) { |
|
| 165 | + // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
| 166 | + // make sure we strip THIS models name from the query param |
|
| 167 | + $parsed_query = array(); |
|
| 168 | + foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
| 169 | + $query_param = str_replace( |
|
| 170 | + $this->get_join_model()->get_this_model_name() . ".", |
|
| 171 | + "", |
|
| 172 | + $query_param |
|
| 173 | + ); |
|
| 174 | + $parsed_query[ $query_param ] = $val; |
|
| 175 | + } |
|
| 176 | + $all_fields = array_merge($foreign_keys, $parsed_query); |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - $existing_entry_in_join_table = $this->get_join_model()->get_one(array($all_fields)); |
|
| 180 | - // If there is already an entry in the join table, indicating a relationship, update it instead of adding a |
|
| 181 | - // new row. |
|
| 182 | - // Again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
| 183 | - // the other tables) use the joining model directly! |
|
| 184 | - if (! $existing_entry_in_join_table) { |
|
| 185 | - $this->get_join_model()->insert($all_fields); |
|
| 186 | - } |
|
| 187 | - return $other_model_obj; |
|
| 188 | - } |
|
| 179 | + $existing_entry_in_join_table = $this->get_join_model()->get_one(array($all_fields)); |
|
| 180 | + // If there is already an entry in the join table, indicating a relationship, update it instead of adding a |
|
| 181 | + // new row. |
|
| 182 | + // Again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
| 183 | + // the other tables) use the joining model directly! |
|
| 184 | + if (! $existing_entry_in_join_table) { |
|
| 185 | + $this->get_join_model()->insert($all_fields); |
|
| 186 | + } |
|
| 187 | + return $other_model_obj; |
|
| 188 | + } |
|
| 189 | 189 | |
| 190 | 190 | |
| 191 | - /** |
|
| 192 | - * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
| 193 | - * |
|
| 194 | - * @param EE_Base_Class|int $this_obj_or_id |
|
| 195 | - * @param EE_Base_Class|int $other_obj_or_id |
|
| 196 | - * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
| 197 | - * values and for removing existing rows if exact matches exist. |
|
| 198 | - * @return EE_Base_Class |
|
| 199 | - * @throws \EE_Error |
|
| 200 | - */ |
|
| 201 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
| 202 | - { |
|
| 203 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 204 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 205 | - // check if such a relationship already exists |
|
| 206 | - $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 207 | - $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 191 | + /** |
|
| 192 | + * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
| 193 | + * |
|
| 194 | + * @param EE_Base_Class|int $this_obj_or_id |
|
| 195 | + * @param EE_Base_Class|int $other_obj_or_id |
|
| 196 | + * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
| 197 | + * values and for removing existing rows if exact matches exist. |
|
| 198 | + * @return EE_Base_Class |
|
| 199 | + * @throws \EE_Error |
|
| 200 | + */ |
|
| 201 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
| 202 | + { |
|
| 203 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
| 204 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
| 205 | + // check if such a relationship already exists |
|
| 206 | + $join_model_fk_to_this_model = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
| 207 | + $join_model_fk_to_other_model = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 208 | 208 | |
| 209 | - $cols_n_values = array( |
|
| 210 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
| 211 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
| 212 | - ); |
|
| 209 | + $cols_n_values = array( |
|
| 210 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
| 211 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
| 212 | + ); |
|
| 213 | 213 | |
| 214 | - // if $where_query exists lets add them to the query_params. |
|
| 215 | - if (! empty($where_query)) { |
|
| 216 | - // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
| 217 | - // make sure we strip THIS models name from the query param |
|
| 218 | - $parsed_query = array(); |
|
| 219 | - foreach ($where_query as $query_param => $val) { |
|
| 220 | - $query_param = str_replace( |
|
| 221 | - $this->get_join_model()->get_this_model_name() . ".", |
|
| 222 | - "", |
|
| 223 | - $query_param |
|
| 224 | - ); |
|
| 225 | - $parsed_query[ $query_param ] = $val; |
|
| 226 | - } |
|
| 227 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
| 228 | - } |
|
| 214 | + // if $where_query exists lets add them to the query_params. |
|
| 215 | + if (! empty($where_query)) { |
|
| 216 | + // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
|
| 217 | + // make sure we strip THIS models name from the query param |
|
| 218 | + $parsed_query = array(); |
|
| 219 | + foreach ($where_query as $query_param => $val) { |
|
| 220 | + $query_param = str_replace( |
|
| 221 | + $this->get_join_model()->get_this_model_name() . ".", |
|
| 222 | + "", |
|
| 223 | + $query_param |
|
| 224 | + ); |
|
| 225 | + $parsed_query[ $query_param ] = $val; |
|
| 226 | + } |
|
| 227 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - $this->get_join_model()->delete(array($cols_n_values)); |
|
| 231 | - return $other_model_obj; |
|
| 232 | - } |
|
| 230 | + $this->get_join_model()->delete(array($cols_n_values)); |
|
| 231 | + return $other_model_obj; |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - /** |
|
| 235 | - * Gets all the non-key fields (ie, not the primary key and not foreign keys) on the join model. |
|
| 236 | - * @since $VID:$ |
|
| 237 | - * @return EE_Model_Field_Base[] |
|
| 238 | - * @throws EE_Error |
|
| 239 | - */ |
|
| 240 | - public function getNonKeyFields() |
|
| 241 | - { |
|
| 242 | - // all fields besides the primary key and two foreign keys should be parameters |
|
| 243 | - $join_model = $this->get_join_model(); |
|
| 244 | - $standard_fields = array(); |
|
| 245 | - if ($join_model->has_primary_key_field()) { |
|
| 246 | - $standard_fields[] = $join_model->primary_key_name(); |
|
| 247 | - } |
|
| 248 | - if ($this->get_this_model()->has_primary_key_field()) { |
|
| 249 | - $standard_fields[] = $this->get_this_model()->primary_key_name(); |
|
| 250 | - } |
|
| 251 | - if ($this->get_other_model()->has_primary_key_field()) { |
|
| 252 | - $standard_fields[] = $this->get_other_model()->primary_key_name(); |
|
| 253 | - } |
|
| 254 | - return array_diff_key( |
|
| 255 | - $join_model->field_settings(), |
|
| 256 | - array_flip($standard_fields) |
|
| 257 | - ); |
|
| 258 | - } |
|
| 234 | + /** |
|
| 235 | + * Gets all the non-key fields (ie, not the primary key and not foreign keys) on the join model. |
|
| 236 | + * @since $VID:$ |
|
| 237 | + * @return EE_Model_Field_Base[] |
|
| 238 | + * @throws EE_Error |
|
| 239 | + */ |
|
| 240 | + public function getNonKeyFields() |
|
| 241 | + { |
|
| 242 | + // all fields besides the primary key and two foreign keys should be parameters |
|
| 243 | + $join_model = $this->get_join_model(); |
|
| 244 | + $standard_fields = array(); |
|
| 245 | + if ($join_model->has_primary_key_field()) { |
|
| 246 | + $standard_fields[] = $join_model->primary_key_name(); |
|
| 247 | + } |
|
| 248 | + if ($this->get_this_model()->has_primary_key_field()) { |
|
| 249 | + $standard_fields[] = $this->get_this_model()->primary_key_name(); |
|
| 250 | + } |
|
| 251 | + if ($this->get_other_model()->has_primary_key_field()) { |
|
| 252 | + $standard_fields[] = $this->get_other_model()->primary_key_name(); |
|
| 253 | + } |
|
| 254 | + return array_diff_key( |
|
| 255 | + $join_model->field_settings(), |
|
| 256 | + array_flip($standard_fields) |
|
| 257 | + ); |
|
| 258 | + } |
|
| 259 | 259 | |
| 260 | - /** |
|
| 261 | - * Returns true if the join model has non-key fields (ie, fields that aren't the primary key or foreign keys.) |
|
| 262 | - * @since $VID:$ |
|
| 263 | - * @return boolean |
|
| 264 | - */ |
|
| 265 | - public function hasNonKeyFields() |
|
| 266 | - { |
|
| 267 | - return count($this->get_join_model()->field_settings()) > 3; |
|
| 268 | - } |
|
| 260 | + /** |
|
| 261 | + * Returns true if the join model has non-key fields (ie, fields that aren't the primary key or foreign keys.) |
|
| 262 | + * @since $VID:$ |
|
| 263 | + * @return boolean |
|
| 264 | + */ |
|
| 265 | + public function hasNonKeyFields() |
|
| 266 | + { |
|
| 267 | + return count($this->get_join_model()->field_settings()) > 3; |
|
| 268 | + } |
|
| 269 | 269 | } |
@@ -65,18 +65,18 @@ discard block |
||
| 65 | 65 | // remember the model relation chain to the JOIN model, because we'll |
| 66 | 66 | // need it for get_join_statement() |
| 67 | 67 | $this->_model_relation_chain_to_join_model = $model_relation_chain; |
| 68 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field();// get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 68 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); // get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
| 69 | 69 | $join_table_fk_field_to_this_table = $this->get_join_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
| 70 | 70 | $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
| 71 | 71 | $model_relation_chain, |
| 72 | 72 | $this->get_this_model()->get_this_model_name() |
| 73 | - ) . $this_table_pk_field->get_table_alias(); |
|
| 73 | + ).$this_table_pk_field->get_table_alias(); |
|
| 74 | 74 | |
| 75 | 75 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
| 76 | 76 | $model_relation_chain, |
| 77 | 77 | $this->get_join_model()->get_this_model_name() |
| 78 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 79 | - $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
| 78 | + ).$join_table_fk_field_to_this_table->get_table_alias(); |
|
| 79 | + $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
| 80 | 80 | // phew! ok, we have all the info we need, now we can create the SQL join string |
| 81 | 81 | $SQL = $this->_left_join( |
| 82 | 82 | $join_table, |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | $join_table_fk_field_to_this_table->get_table_column(), |
| 85 | 85 | $this_table_alias, |
| 86 | 86 | $this_table_pk_field->get_table_column() |
| 87 | - ) . $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
| 87 | + ).$this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
| 88 | 88 | |
| 89 | 89 | return $SQL; |
| 90 | 90 | } |
@@ -113,14 +113,14 @@ discard block |
||
| 113 | 113 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
| 114 | 114 | $this->_model_relation_chain_to_join_model, |
| 115 | 115 | $this->get_join_model()->get_this_model_name() |
| 116 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
| 116 | + ).$join_table_fk_field_to_this_table->get_table_alias(); |
|
| 117 | 117 | $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
| 118 | 118 | $join_table_fk_field_to_other_table = $this->get_join_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
| 119 | 119 | $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
| 120 | 120 | $model_relation_chain, |
| 121 | 121 | $this->get_other_model()->get_this_model_name() |
| 122 | - ) . $other_table_pk_field->get_table_alias(); |
|
| 123 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
| 122 | + ).$other_table_pk_field->get_table_alias(); |
|
| 123 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
| 124 | 124 | |
| 125 | 125 | $SQL = $this->_left_join( |
| 126 | 126 | $other_table, |
@@ -128,7 +128,7 @@ discard block |
||
| 128 | 128 | $other_table_pk_field->get_table_column(), |
| 129 | 129 | $join_table_alias, |
| 130 | 130 | $join_table_fk_field_to_other_table->get_table_column() |
| 131 | - ) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
| 131 | + ).$this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
| 132 | 132 | return $SQL; |
| 133 | 133 | } |
| 134 | 134 | |
@@ -161,17 +161,17 @@ discard block |
||
| 161 | 161 | ); |
| 162 | 162 | |
| 163 | 163 | // if $where_query exists lets add them to the query_params. |
| 164 | - if (! empty($extra_join_model_fields_n_values)) { |
|
| 164 | + if ( ! empty($extra_join_model_fields_n_values)) { |
|
| 165 | 165 | // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
| 166 | 166 | // make sure we strip THIS models name from the query param |
| 167 | 167 | $parsed_query = array(); |
| 168 | 168 | foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
| 169 | - $query_param = str_replace( |
|
| 170 | - $this->get_join_model()->get_this_model_name() . ".", |
|
| 169 | + $query_param = str_replace( |
|
| 170 | + $this->get_join_model()->get_this_model_name().".", |
|
| 171 | 171 | "", |
| 172 | 172 | $query_param |
| 173 | 173 | ); |
| 174 | - $parsed_query[ $query_param ] = $val; |
|
| 174 | + $parsed_query[$query_param] = $val; |
|
| 175 | 175 | } |
| 176 | 176 | $all_fields = array_merge($foreign_keys, $parsed_query); |
| 177 | 177 | } |
@@ -181,7 +181,7 @@ discard block |
||
| 181 | 181 | // new row. |
| 182 | 182 | // Again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
| 183 | 183 | // the other tables) use the joining model directly! |
| 184 | - if (! $existing_entry_in_join_table) { |
|
| 184 | + if ( ! $existing_entry_in_join_table) { |
|
| 185 | 185 | $this->get_join_model()->insert($all_fields); |
| 186 | 186 | } |
| 187 | 187 | return $other_model_obj; |
@@ -212,17 +212,17 @@ discard block |
||
| 212 | 212 | ); |
| 213 | 213 | |
| 214 | 214 | // if $where_query exists lets add them to the query_params. |
| 215 | - if (! empty($where_query)) { |
|
| 215 | + if ( ! empty($where_query)) { |
|
| 216 | 216 | // make sure we strip any of the join model names from the $where_query cause we don't need that in here (why? because client code may have used the same conditionals for get_all_related which DOES need the join model name) |
| 217 | 217 | // make sure we strip THIS models name from the query param |
| 218 | 218 | $parsed_query = array(); |
| 219 | 219 | foreach ($where_query as $query_param => $val) { |
| 220 | - $query_param = str_replace( |
|
| 221 | - $this->get_join_model()->get_this_model_name() . ".", |
|
| 220 | + $query_param = str_replace( |
|
| 221 | + $this->get_join_model()->get_this_model_name().".", |
|
| 222 | 222 | "", |
| 223 | 223 | $query_param |
| 224 | 224 | ); |
| 225 | - $parsed_query[ $query_param ] = $val; |
|
| 225 | + $parsed_query[$query_param] = $val; |
|
| 226 | 226 | } |
| 227 | 227 | $cols_n_values = array_merge($cols_n_values, $parsed_query); |
| 228 | 228 | } |
@@ -38,103 +38,103 @@ |
||
| 38 | 38 | * @since 4.0 |
| 39 | 39 | */ |
| 40 | 40 | if (function_exists('espresso_version')) { |
| 41 | - if (! function_exists('espresso_duplicate_plugin_error')) { |
|
| 42 | - /** |
|
| 43 | - * espresso_duplicate_plugin_error |
|
| 44 | - * displays if more than one version of EE is activated at the same time |
|
| 45 | - */ |
|
| 46 | - function espresso_duplicate_plugin_error() |
|
| 47 | - { |
|
| 48 | - ?> |
|
| 41 | + if (! function_exists('espresso_duplicate_plugin_error')) { |
|
| 42 | + /** |
|
| 43 | + * espresso_duplicate_plugin_error |
|
| 44 | + * displays if more than one version of EE is activated at the same time |
|
| 45 | + */ |
|
| 46 | + function espresso_duplicate_plugin_error() |
|
| 47 | + { |
|
| 48 | + ?> |
|
| 49 | 49 | <div class="error"> |
| 50 | 50 | <p> |
| 51 | 51 | <?php |
| 52 | - echo esc_html__( |
|
| 53 | - 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
| 54 | - 'event_espresso' |
|
| 55 | - ); ?> |
|
| 52 | + echo esc_html__( |
|
| 53 | + 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
| 54 | + 'event_espresso' |
|
| 55 | + ); ?> |
|
| 56 | 56 | </p> |
| 57 | 57 | </div> |
| 58 | 58 | <?php |
| 59 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
| 59 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
| 63 | 63 | } else { |
| 64 | - define('EE_MIN_PHP_VER_REQUIRED', '5.4.0'); |
|
| 65 | - if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
| 66 | - /** |
|
| 67 | - * espresso_minimum_php_version_error |
|
| 68 | - * |
|
| 69 | - * @return void |
|
| 70 | - */ |
|
| 71 | - function espresso_minimum_php_version_error() |
|
| 72 | - { |
|
| 73 | - ?> |
|
| 64 | + define('EE_MIN_PHP_VER_REQUIRED', '5.4.0'); |
|
| 65 | + if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
| 66 | + /** |
|
| 67 | + * espresso_minimum_php_version_error |
|
| 68 | + * |
|
| 69 | + * @return void |
|
| 70 | + */ |
|
| 71 | + function espresso_minimum_php_version_error() |
|
| 72 | + { |
|
| 73 | + ?> |
|
| 74 | 74 | <div class="error"> |
| 75 | 75 | <p> |
| 76 | 76 | <?php |
| 77 | - printf( |
|
| 78 | - esc_html__( |
|
| 79 | - 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
| 80 | - 'event_espresso' |
|
| 81 | - ), |
|
| 82 | - EE_MIN_PHP_VER_REQUIRED, |
|
| 83 | - PHP_VERSION, |
|
| 84 | - '<br/>', |
|
| 85 | - '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
| 86 | - ); |
|
| 87 | - ?> |
|
| 77 | + printf( |
|
| 78 | + esc_html__( |
|
| 79 | + 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
| 80 | + 'event_espresso' |
|
| 81 | + ), |
|
| 82 | + EE_MIN_PHP_VER_REQUIRED, |
|
| 83 | + PHP_VERSION, |
|
| 84 | + '<br/>', |
|
| 85 | + '<a href="http://php.net/downloads.php">http://php.net/downloads.php</a>' |
|
| 86 | + ); |
|
| 87 | + ?> |
|
| 88 | 88 | </p> |
| 89 | 89 | </div> |
| 90 | 90 | <?php |
| 91 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 92 | - } |
|
| 91 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
| 95 | - } else { |
|
| 96 | - define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
| 97 | - /** |
|
| 98 | - * espresso_version |
|
| 99 | - * Returns the plugin version |
|
| 100 | - * |
|
| 101 | - * @return string |
|
| 102 | - */ |
|
| 103 | - function espresso_version() |
|
| 104 | - { |
|
| 105 | - return apply_filters('FHEE__espresso__espresso_version', '4.9.76.rc.011'); |
|
| 106 | - } |
|
| 94 | + add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
| 95 | + } else { |
|
| 96 | + define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
| 97 | + /** |
|
| 98 | + * espresso_version |
|
| 99 | + * Returns the plugin version |
|
| 100 | + * |
|
| 101 | + * @return string |
|
| 102 | + */ |
|
| 103 | + function espresso_version() |
|
| 104 | + { |
|
| 105 | + return apply_filters('FHEE__espresso__espresso_version', '4.9.76.rc.011'); |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * espresso_plugin_activation |
|
| 110 | - * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
| 111 | - */ |
|
| 112 | - function espresso_plugin_activation() |
|
| 113 | - { |
|
| 114 | - update_option('ee_espresso_activation', true); |
|
| 115 | - } |
|
| 108 | + /** |
|
| 109 | + * espresso_plugin_activation |
|
| 110 | + * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
| 111 | + */ |
|
| 112 | + function espresso_plugin_activation() |
|
| 113 | + { |
|
| 114 | + update_option('ee_espresso_activation', true); |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
| 117 | + register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
| 118 | 118 | |
| 119 | - require_once __DIR__ . '/core/bootstrap_espresso.php'; |
|
| 120 | - bootstrap_espresso(); |
|
| 121 | - } |
|
| 119 | + require_once __DIR__ . '/core/bootstrap_espresso.php'; |
|
| 120 | + bootstrap_espresso(); |
|
| 121 | + } |
|
| 122 | 122 | } |
| 123 | 123 | if (! function_exists('espresso_deactivate_plugin')) { |
| 124 | - /** |
|
| 125 | - * deactivate_plugin |
|
| 126 | - * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
| 127 | - * |
|
| 128 | - * @access public |
|
| 129 | - * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
| 130 | - * @return void |
|
| 131 | - */ |
|
| 132 | - function espresso_deactivate_plugin($plugin_basename = '') |
|
| 133 | - { |
|
| 134 | - if (! function_exists('deactivate_plugins')) { |
|
| 135 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
| 136 | - } |
|
| 137 | - unset($_GET['activate'], $_REQUEST['activate']); |
|
| 138 | - deactivate_plugins($plugin_basename); |
|
| 139 | - } |
|
| 124 | + /** |
|
| 125 | + * deactivate_plugin |
|
| 126 | + * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
| 127 | + * |
|
| 128 | + * @access public |
|
| 129 | + * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
| 130 | + * @return void |
|
| 131 | + */ |
|
| 132 | + function espresso_deactivate_plugin($plugin_basename = '') |
|
| 133 | + { |
|
| 134 | + if (! function_exists('deactivate_plugins')) { |
|
| 135 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
| 136 | + } |
|
| 137 | + unset($_GET['activate'], $_REQUEST['activate']); |
|
| 138 | + deactivate_plugins($plugin_basename); |
|
| 139 | + } |
|
| 140 | 140 | } |