@@ -3,22 +3,22 @@ |
||
3 | 3 | class EE_Primary_Key_String_Field extends EE_Primary_Key_Field_Base |
4 | 4 | { |
5 | 5 | |
6 | - public function __construct($table_column, $nicename) |
|
7 | - { |
|
8 | - parent::__construct($table_column, $nicename, null); |
|
9 | - } |
|
6 | + public function __construct($table_column, $nicename) |
|
7 | + { |
|
8 | + parent::__construct($table_column, $nicename, null); |
|
9 | + } |
|
10 | 10 | |
11 | - /** |
|
12 | - * removes all tags when setting |
|
13 | - * |
|
14 | - * @param string $value_inputted_for_field_on_model_object |
|
15 | - * @return string |
|
16 | - */ |
|
17 | - public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
18 | - { |
|
19 | - if ($this->is_model_obj_of_type_pointed_to($value_inputted_for_field_on_model_object)) { |
|
20 | - $value_inputted_for_field_on_model_object = $value_inputted_for_field_on_model_object->ID(); |
|
21 | - } |
|
22 | - return wp_strip_all_tags($value_inputted_for_field_on_model_object); |
|
23 | - } |
|
11 | + /** |
|
12 | + * removes all tags when setting |
|
13 | + * |
|
14 | + * @param string $value_inputted_for_field_on_model_object |
|
15 | + * @return string |
|
16 | + */ |
|
17 | + public function prepare_for_set($value_inputted_for_field_on_model_object) |
|
18 | + { |
|
19 | + if ($this->is_model_obj_of_type_pointed_to($value_inputted_for_field_on_model_object)) { |
|
20 | + $value_inputted_for_field_on_model_object = $value_inputted_for_field_on_model_object->ID(); |
|
21 | + } |
|
22 | + return wp_strip_all_tags($value_inputted_for_field_on_model_object); |
|
23 | + } |
|
24 | 24 | } |
@@ -49,7 +49,7 @@ |
||
49 | 49 | ); |
50 | 50 | $this->_model_chain_to_wp_user = 'Message_Template_Group'; |
51 | 51 | foreach ($this->_cap_contexts_to_cap_action_map as $context => $action) { |
52 | - $this->_cap_restriction_generators[ $context ] = new EE_Restriction_Generator_Global('Message_Template_Group.MTP_is_global'); |
|
52 | + $this->_cap_restriction_generators[$context] = new EE_Restriction_Generator_Global('Message_Template_Group.MTP_is_global'); |
|
53 | 53 | } |
54 | 54 | $this->_caps_slug = 'messages'; |
55 | 55 | parent::__construct($timezone); |
@@ -14,45 +14,45 @@ |
||
14 | 14 | */ |
15 | 15 | class EEM_Message_Template extends EEM_Base |
16 | 16 | { |
17 | - // private instance of the EEM_Message_Template object |
|
18 | - protected static $_instance = null; |
|
17 | + // private instance of the EEM_Message_Template object |
|
18 | + protected static $_instance = null; |
|
19 | 19 | |
20 | 20 | |
21 | 21 | |
22 | - /** |
|
23 | - * private constructor to prevent direct creation |
|
24 | - * |
|
25 | - * @Constructor |
|
26 | - * @access protected |
|
27 | - * @param string $timezone |
|
28 | - * @throws \EE_Error |
|
29 | - */ |
|
30 | - protected function __construct($timezone = null) |
|
31 | - { |
|
32 | - $this->singular_item = esc_html__('Message Template', 'event_espresso'); |
|
33 | - $this->plural_item = esc_html__('Message Templates', 'event_espresso'); |
|
22 | + /** |
|
23 | + * private constructor to prevent direct creation |
|
24 | + * |
|
25 | + * @Constructor |
|
26 | + * @access protected |
|
27 | + * @param string $timezone |
|
28 | + * @throws \EE_Error |
|
29 | + */ |
|
30 | + protected function __construct($timezone = null) |
|
31 | + { |
|
32 | + $this->singular_item = esc_html__('Message Template', 'event_espresso'); |
|
33 | + $this->plural_item = esc_html__('Message Templates', 'event_espresso'); |
|
34 | 34 | |
35 | - $this->_tables = array( |
|
36 | - 'Message_Template' => new EE_Primary_Table('esp_message_template', 'MTP_ID') |
|
37 | - ); |
|
38 | - $this->_fields = array( |
|
39 | - 'Message_Template' => array( |
|
40 | - 'MTP_ID' => new EE_Primary_Key_Int_Field('MTP_ID', esc_html__('Message Template ID', 'event_espresso')), |
|
41 | - 'GRP_ID' => new EE_Foreign_Key_Int_Field('GRP_ID', esc_html__('Message Template Group ID', 'event_espresso'), false, 0, 'Message_Template_Group'), |
|
42 | - 'MTP_template_field' => new EE_Plain_Text_Field('MTP_template_field', esc_html__('Field Name for this Template', 'event_espresso'), false, 'default'), |
|
43 | - 'MTP_context' => new EE_Plain_Text_Field('MTP_context', esc_html__('Message Type Context for this field', 'event_espresso'), false, 'admin'), |
|
44 | - 'MTP_content' => new EE_Serialized_Text_Field('MTP_content', esc_html__('The field content for the template', 'event_espresso'), false, ''), |
|
45 | - ) |
|
46 | - ); |
|
35 | + $this->_tables = array( |
|
36 | + 'Message_Template' => new EE_Primary_Table('esp_message_template', 'MTP_ID') |
|
37 | + ); |
|
38 | + $this->_fields = array( |
|
39 | + 'Message_Template' => array( |
|
40 | + 'MTP_ID' => new EE_Primary_Key_Int_Field('MTP_ID', esc_html__('Message Template ID', 'event_espresso')), |
|
41 | + 'GRP_ID' => new EE_Foreign_Key_Int_Field('GRP_ID', esc_html__('Message Template Group ID', 'event_espresso'), false, 0, 'Message_Template_Group'), |
|
42 | + 'MTP_template_field' => new EE_Plain_Text_Field('MTP_template_field', esc_html__('Field Name for this Template', 'event_espresso'), false, 'default'), |
|
43 | + 'MTP_context' => new EE_Plain_Text_Field('MTP_context', esc_html__('Message Type Context for this field', 'event_espresso'), false, 'admin'), |
|
44 | + 'MTP_content' => new EE_Serialized_Text_Field('MTP_content', esc_html__('The field content for the template', 'event_espresso'), false, ''), |
|
45 | + ) |
|
46 | + ); |
|
47 | 47 | |
48 | - $this->_model_relations = array( |
|
49 | - 'Message_Template_Group' => new EE_Belongs_To_Relation() |
|
50 | - ); |
|
51 | - $this->_model_chain_to_wp_user = 'Message_Template_Group'; |
|
52 | - foreach ($this->_cap_contexts_to_cap_action_map as $context => $action) { |
|
53 | - $this->_cap_restriction_generators[ $context ] = new EE_Restriction_Generator_Global('Message_Template_Group.MTP_is_global'); |
|
54 | - } |
|
55 | - $this->_caps_slug = 'messages'; |
|
56 | - parent::__construct($timezone); |
|
57 | - } |
|
48 | + $this->_model_relations = array( |
|
49 | + 'Message_Template_Group' => new EE_Belongs_To_Relation() |
|
50 | + ); |
|
51 | + $this->_model_chain_to_wp_user = 'Message_Template_Group'; |
|
52 | + foreach ($this->_cap_contexts_to_cap_action_map as $context => $action) { |
|
53 | + $this->_cap_restriction_generators[ $context ] = new EE_Restriction_Generator_Global('Message_Template_Group.MTP_is_global'); |
|
54 | + } |
|
55 | + $this->_caps_slug = 'messages'; |
|
56 | + parent::__construct($timezone); |
|
57 | + } |
|
58 | 58 | } |
@@ -35,19 +35,19 @@ |
||
35 | 35 | $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
36 | 36 | $model_relation_chain, |
37 | 37 | $this->get_this_model()->get_this_model_name() |
38 | - ) . $this_table_fk_field->get_table_alias(); |
|
39 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
38 | + ).$this_table_fk_field->get_table_alias(); |
|
39 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
40 | 40 | $model_relation_chain, |
41 | 41 | $this->get_other_model()->get_this_model_name() |
42 | - ) . $other_table_pk_field->get_table_alias(); |
|
43 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
42 | + ).$other_table_pk_field->get_table_alias(); |
|
43 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
44 | 44 | return $this->_left_join( |
45 | 45 | $other_table, |
46 | 46 | $other_table_alias, |
47 | 47 | $other_table_pk_field->get_table_column(), |
48 | 48 | $this_table_alias, |
49 | 49 | $this_table_fk_field->get_table_column(), |
50 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'" |
|
50 | + $field_with_model_name->get_qualified_column()."='".$this->get_other_model()->get_this_model_name()."'" |
|
51 | 51 | ) |
52 | 52 | . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
53 | 53 | } |
@@ -14,94 +14,94 @@ |
||
14 | 14 | */ |
15 | 15 | class EE_Belongs_To_Any_Relation extends EE_Belongs_To_Relation |
16 | 16 | { |
17 | - /** |
|
18 | - * get_join_statement |
|
19 | - * |
|
20 | - * @param string $model_relation_chain |
|
21 | - * @return string |
|
22 | - * @throws \EE_Error |
|
23 | - */ |
|
24 | - public function get_join_statement($model_relation_chain) |
|
25 | - { |
|
26 | - // create the sql string like |
|
27 | - $this_table_fk_field = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
28 | - // ALSO, need to get the field with the model name |
|
29 | - $field_with_model_name = $this->get_this_model()->get_field_containing_related_model_name(); |
|
17 | + /** |
|
18 | + * get_join_statement |
|
19 | + * |
|
20 | + * @param string $model_relation_chain |
|
21 | + * @return string |
|
22 | + * @throws \EE_Error |
|
23 | + */ |
|
24 | + public function get_join_statement($model_relation_chain) |
|
25 | + { |
|
26 | + // create the sql string like |
|
27 | + $this_table_fk_field = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
28 | + // ALSO, need to get the field with the model name |
|
29 | + $field_with_model_name = $this->get_this_model()->get_field_containing_related_model_name(); |
|
30 | 30 | |
31 | 31 | |
32 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
33 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
34 | - $model_relation_chain, |
|
35 | - $this->get_this_model()->get_this_model_name() |
|
36 | - ) . $this_table_fk_field->get_table_alias(); |
|
37 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
38 | - $model_relation_chain, |
|
39 | - $this->get_other_model()->get_this_model_name() |
|
40 | - ) . $other_table_pk_field->get_table_alias(); |
|
41 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
42 | - return $this->_left_join( |
|
43 | - $other_table, |
|
44 | - $other_table_alias, |
|
45 | - $other_table_pk_field->get_table_column(), |
|
46 | - $this_table_alias, |
|
47 | - $this_table_fk_field->get_table_column(), |
|
48 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'" |
|
49 | - ) |
|
50 | - . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
51 | - } |
|
32 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
33 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
34 | + $model_relation_chain, |
|
35 | + $this->get_this_model()->get_this_model_name() |
|
36 | + ) . $this_table_fk_field->get_table_alias(); |
|
37 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
38 | + $model_relation_chain, |
|
39 | + $this->get_other_model()->get_this_model_name() |
|
40 | + ) . $other_table_pk_field->get_table_alias(); |
|
41 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
42 | + return $this->_left_join( |
|
43 | + $other_table, |
|
44 | + $other_table_alias, |
|
45 | + $other_table_pk_field->get_table_column(), |
|
46 | + $this_table_alias, |
|
47 | + $this_table_fk_field->get_table_column(), |
|
48 | + $field_with_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'" |
|
49 | + ) |
|
50 | + . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
51 | + } |
|
52 | 52 | |
53 | 53 | |
54 | - /** |
|
55 | - * Sets this model object's foreign key to the other model object's primary key. Feel free to do this manually if |
|
56 | - * you like. |
|
57 | - * |
|
58 | - * @param EE_Base_Class|int $this_obj_or_id |
|
59 | - * @param EE_Base_Class|int $other_obj_or_id |
|
60 | - * @param array $extra_join_model_fields_n_values |
|
61 | - * @return \EE_Base_Class |
|
62 | - * @throws \EE_Error |
|
63 | - */ |
|
64 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
65 | - { |
|
66 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
67 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
68 | - // find the field on THIS model which a foreign key to the other model |
|
69 | - $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
70 | - // set that field on the other model to this model's ID |
|
71 | - $this_model_obj->set($fk_on_this_model->get_name(), $other_model_obj->ID()); |
|
72 | - // and make sure this model's field with the foreign model name is set to the correct value |
|
73 | - $this_model_obj->set( |
|
74 | - $this->get_this_model()->get_field_containing_related_model_name()->get_name(), |
|
75 | - $this->get_other_model()->get_this_model_name() |
|
76 | - ); |
|
77 | - $this_model_obj->save(); |
|
78 | - return $other_model_obj; |
|
79 | - } |
|
54 | + /** |
|
55 | + * Sets this model object's foreign key to the other model object's primary key. Feel free to do this manually if |
|
56 | + * you like. |
|
57 | + * |
|
58 | + * @param EE_Base_Class|int $this_obj_or_id |
|
59 | + * @param EE_Base_Class|int $other_obj_or_id |
|
60 | + * @param array $extra_join_model_fields_n_values |
|
61 | + * @return \EE_Base_Class |
|
62 | + * @throws \EE_Error |
|
63 | + */ |
|
64 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
65 | + { |
|
66 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
67 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
68 | + // find the field on THIS model which a foreign key to the other model |
|
69 | + $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
70 | + // set that field on the other model to this model's ID |
|
71 | + $this_model_obj->set($fk_on_this_model->get_name(), $other_model_obj->ID()); |
|
72 | + // and make sure this model's field with the foreign model name is set to the correct value |
|
73 | + $this_model_obj->set( |
|
74 | + $this->get_this_model()->get_field_containing_related_model_name()->get_name(), |
|
75 | + $this->get_other_model()->get_this_model_name() |
|
76 | + ); |
|
77 | + $this_model_obj->save(); |
|
78 | + return $other_model_obj; |
|
79 | + } |
|
80 | 80 | |
81 | 81 | |
82 | - /** |
|
83 | - * Sets the this model object's foreign key to its default, instead of pointing to the other model object |
|
84 | - * |
|
85 | - * @param EE_Base_Class|int $this_obj_or_id |
|
86 | - * @param EE_Base_Class|int $other_obj_or_id |
|
87 | - * @param array $where_query |
|
88 | - * @return \EE_Base_Class |
|
89 | - * @throws \EE_Error |
|
90 | - */ |
|
91 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
92 | - { |
|
93 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
94 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
95 | - // find the field on the other model which is a foreign key to this model |
|
96 | - $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
97 | - // set that field on the other model to this model's ID |
|
98 | - $this_model_obj->set($fk_on_this_model->get_name(), null, true); |
|
99 | - $this_model_obj->set( |
|
100 | - $this->get_this_model()->get_field_containing_related_model_name()->get_name(), |
|
101 | - null, |
|
102 | - true |
|
103 | - ); |
|
104 | - $this_model_obj->save(); |
|
105 | - return $other_model_obj; |
|
106 | - } |
|
82 | + /** |
|
83 | + * Sets the this model object's foreign key to its default, instead of pointing to the other model object |
|
84 | + * |
|
85 | + * @param EE_Base_Class|int $this_obj_or_id |
|
86 | + * @param EE_Base_Class|int $other_obj_or_id |
|
87 | + * @param array $where_query |
|
88 | + * @return \EE_Base_Class |
|
89 | + * @throws \EE_Error |
|
90 | + */ |
|
91 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
92 | + { |
|
93 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
94 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
95 | + // find the field on the other model which is a foreign key to this model |
|
96 | + $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
97 | + // set that field on the other model to this model's ID |
|
98 | + $this_model_obj->set($fk_on_this_model->get_name(), null, true); |
|
99 | + $this_model_obj->set( |
|
100 | + $this->get_this_model()->get_field_containing_related_model_name()->get_name(), |
|
101 | + null, |
|
102 | + true |
|
103 | + ); |
|
104 | + $this_model_obj->save(); |
|
105 | + return $other_model_obj; |
|
106 | + } |
|
107 | 107 | } |
@@ -46,12 +46,12 @@ discard block |
||
46 | 46 | $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
47 | 47 | $model_relation_chain, |
48 | 48 | $this->get_this_model()->get_this_model_name() |
49 | - ) . $this_table_pk_field->get_table_alias(); |
|
50 | - $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
49 | + ).$this_table_pk_field->get_table_alias(); |
|
50 | + $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
51 | 51 | $model_relation_chain, |
52 | 52 | $this->get_other_model()->get_this_model_name() |
53 | - ) . $other_table_fk_field->get_table_alias(); |
|
54 | - $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
|
53 | + ).$other_table_fk_field->get_table_alias(); |
|
54 | + $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
|
55 | 55 | |
56 | 56 | return $this->_left_join( |
57 | 57 | $fk_table, |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | $other_table_fk_field->get_table_column(), |
60 | 60 | $pk_table_alias, |
61 | 61 | $this_table_pk_field->get_table_column() |
62 | - ) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
|
62 | + ).$this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
|
63 | 63 | } |
64 | 64 | |
65 | 65 |
@@ -10,102 +10,102 @@ |
||
10 | 10 | */ |
11 | 11 | class EE_Has_Many_Relation extends EE_Model_Relation_Base |
12 | 12 | { |
13 | - /** |
|
14 | - * Object representing the relationship between two models. Has_Many_Relations are where the OTHER model has the |
|
15 | - * foreign key this model. IE, there can be many other model objects related to one of this model's objects (but |
|
16 | - * NOT through a JOIN table, which is the case for EE_HABTM_Relations). This knows how to join the models, get |
|
17 | - * related models across the relation, and add-and-remove the relationships. |
|
18 | - * |
|
19 | - * @param boolean $block_deletes For this type of r elation, we block by default. If there are |
|
20 | - * related models across this relation, block (prevent and add an |
|
21 | - * error) the deletion of this model |
|
22 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
23 | - * default |
|
24 | - */ |
|
25 | - public function __construct($block_deletes = true, $blocking_delete_error_message = null) |
|
26 | - { |
|
27 | - parent::__construct($block_deletes, $blocking_delete_error_message); |
|
28 | - } |
|
13 | + /** |
|
14 | + * Object representing the relationship between two models. Has_Many_Relations are where the OTHER model has the |
|
15 | + * foreign key this model. IE, there can be many other model objects related to one of this model's objects (but |
|
16 | + * NOT through a JOIN table, which is the case for EE_HABTM_Relations). This knows how to join the models, get |
|
17 | + * related models across the relation, and add-and-remove the relationships. |
|
18 | + * |
|
19 | + * @param boolean $block_deletes For this type of r elation, we block by default. If there are |
|
20 | + * related models across this relation, block (prevent and add an |
|
21 | + * error) the deletion of this model |
|
22 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
23 | + * default |
|
24 | + */ |
|
25 | + public function __construct($block_deletes = true, $blocking_delete_error_message = null) |
|
26 | + { |
|
27 | + parent::__construct($block_deletes, $blocking_delete_error_message); |
|
28 | + } |
|
29 | 29 | |
30 | 30 | |
31 | - /** |
|
32 | - * Gets the SQL string for performing the join between this model and the other model. |
|
33 | - * |
|
34 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
35 | - * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
36 | - * other_model_primary_table.fk" etc |
|
37 | - * @throws \EE_Error |
|
38 | - */ |
|
39 | - public function get_join_statement($model_relation_chain) |
|
40 | - { |
|
41 | - // create the sql string like |
|
42 | - // LEFT JOIN other_table AS table_alias ON this_table_alias.pk = other_table_alias.fk extra_join_conditions |
|
43 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
44 | - $other_table_fk_field = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
45 | - $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
46 | - $model_relation_chain, |
|
47 | - $this->get_this_model()->get_this_model_name() |
|
48 | - ) . $this_table_pk_field->get_table_alias(); |
|
49 | - $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
50 | - $model_relation_chain, |
|
51 | - $this->get_other_model()->get_this_model_name() |
|
52 | - ) . $other_table_fk_field->get_table_alias(); |
|
53 | - $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
|
31 | + /** |
|
32 | + * Gets the SQL string for performing the join between this model and the other model. |
|
33 | + * |
|
34 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
35 | + * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
36 | + * other_model_primary_table.fk" etc |
|
37 | + * @throws \EE_Error |
|
38 | + */ |
|
39 | + public function get_join_statement($model_relation_chain) |
|
40 | + { |
|
41 | + // create the sql string like |
|
42 | + // LEFT JOIN other_table AS table_alias ON this_table_alias.pk = other_table_alias.fk extra_join_conditions |
|
43 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
44 | + $other_table_fk_field = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
45 | + $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
46 | + $model_relation_chain, |
|
47 | + $this->get_this_model()->get_this_model_name() |
|
48 | + ) . $this_table_pk_field->get_table_alias(); |
|
49 | + $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
50 | + $model_relation_chain, |
|
51 | + $this->get_other_model()->get_this_model_name() |
|
52 | + ) . $other_table_fk_field->get_table_alias(); |
|
53 | + $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
|
54 | 54 | |
55 | - return $this->_left_join( |
|
56 | - $fk_table, |
|
57 | - $fk_table_alias, |
|
58 | - $other_table_fk_field->get_table_column(), |
|
59 | - $pk_table_alias, |
|
60 | - $this_table_pk_field->get_table_column() |
|
61 | - ) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
|
62 | - } |
|
55 | + return $this->_left_join( |
|
56 | + $fk_table, |
|
57 | + $fk_table_alias, |
|
58 | + $other_table_fk_field->get_table_column(), |
|
59 | + $pk_table_alias, |
|
60 | + $this_table_pk_field->get_table_column() |
|
61 | + ) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
|
62 | + } |
|
63 | 63 | |
64 | 64 | |
65 | - /** |
|
66 | - * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
67 | - * you like. |
|
68 | - * |
|
69 | - * @param EE_Base_Class|int $this_obj_or_id |
|
70 | - * @param EE_Base_Class|int $other_obj_or_id |
|
71 | - * @param array $extra_join_model_fields_n_values |
|
72 | - * @return \EE_Base_Class |
|
73 | - * @throws \EE_Error |
|
74 | - */ |
|
75 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
76 | - { |
|
77 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
78 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
65 | + /** |
|
66 | + * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
67 | + * you like. |
|
68 | + * |
|
69 | + * @param EE_Base_Class|int $this_obj_or_id |
|
70 | + * @param EE_Base_Class|int $other_obj_or_id |
|
71 | + * @param array $extra_join_model_fields_n_values |
|
72 | + * @return \EE_Base_Class |
|
73 | + * @throws \EE_Error |
|
74 | + */ |
|
75 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
76 | + { |
|
77 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
78 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
79 | 79 | |
80 | - // find the field on the other model which is a foreign key to this model |
|
81 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
82 | - if ($other_model_obj->get($fk_field_on_other_model->get_name()) != $this_model_obj->ID()) { |
|
83 | - // set that field on the other model to this model's ID |
|
84 | - $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
85 | - $other_model_obj->save(); |
|
86 | - } |
|
87 | - return $other_model_obj; |
|
88 | - } |
|
80 | + // find the field on the other model which is a foreign key to this model |
|
81 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
82 | + if ($other_model_obj->get($fk_field_on_other_model->get_name()) != $this_model_obj->ID()) { |
|
83 | + // set that field on the other model to this model's ID |
|
84 | + $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
85 | + $other_model_obj->save(); |
|
86 | + } |
|
87 | + return $other_model_obj; |
|
88 | + } |
|
89 | 89 | |
90 | 90 | |
91 | - /** |
|
92 | - * Sets the other model object's foreign key to its default, instead of pointing to this model object. |
|
93 | - * If $other_obj_or_id doesn't have any other relations, this function is essentially orphaning it |
|
94 | - * |
|
95 | - * @param EE_Base_Class|int $this_obj_or_id |
|
96 | - * @param EE_Base_Class|int $other_obj_or_id |
|
97 | - * @param array $where_query |
|
98 | - * @return \EE_Base_Class |
|
99 | - * @throws \EE_Error |
|
100 | - */ |
|
101 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
102 | - { |
|
103 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
104 | - // find the field on the other model which is a foreign key to this model |
|
105 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
106 | - // set that field on the other model to this model's ID |
|
107 | - $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
108 | - $other_model_obj->save(); |
|
109 | - return $other_model_obj; |
|
110 | - } |
|
91 | + /** |
|
92 | + * Sets the other model object's foreign key to its default, instead of pointing to this model object. |
|
93 | + * If $other_obj_or_id doesn't have any other relations, this function is essentially orphaning it |
|
94 | + * |
|
95 | + * @param EE_Base_Class|int $this_obj_or_id |
|
96 | + * @param EE_Base_Class|int $other_obj_or_id |
|
97 | + * @param array $where_query |
|
98 | + * @return \EE_Base_Class |
|
99 | + * @throws \EE_Error |
|
100 | + */ |
|
101 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
102 | + { |
|
103 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
104 | + // find the field on the other model which is a foreign key to this model |
|
105 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
106 | + // set that field on the other model to this model's ID |
|
107 | + $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
108 | + $other_model_obj->save(); |
|
109 | + return $other_model_obj; |
|
110 | + } |
|
111 | 111 | } |
@@ -45,19 +45,19 @@ discard block |
||
45 | 45 | $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
46 | 46 | $model_relation_chain, |
47 | 47 | $this->get_this_model()->get_this_model_name() |
48 | - ) . $this_table_fk_field->get_table_alias(); |
|
49 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
48 | + ).$this_table_fk_field->get_table_alias(); |
|
49 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
50 | 50 | $model_relation_chain, |
51 | 51 | $this->get_other_model()->get_this_model_name() |
52 | - ) . $other_table_pk_field->get_table_alias(); |
|
53 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
52 | + ).$other_table_pk_field->get_table_alias(); |
|
53 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
54 | 54 | return $this->_left_join( |
55 | 55 | $other_table, |
56 | 56 | $other_table_alias, |
57 | 57 | $other_table_pk_field->get_table_column(), |
58 | 58 | $this_table_alias, |
59 | 59 | $this_table_fk_field->get_table_column() |
60 | - ) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
60 | + ).$this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
61 | 61 | } |
62 | 62 | |
63 | 63 | |
@@ -139,8 +139,8 @@ discard block |
||
139 | 139 | } |
140 | 140 | $ID_value_on_other_model = $model_obj->get($fk_field_obj->get_name()); |
141 | 141 | // get all where their PK matches that value |
142 | - $query_params[0][ $this->get_other_model()->get_primary_key_field()->get_name() ] = $ID_value_on_other_model; |
|
143 | - $query_params = $this->_disable_default_where_conditions_on_query_param($query_params); |
|
142 | + $query_params[0][$this->get_other_model()->get_primary_key_field()->get_name()] = $ID_value_on_other_model; |
|
143 | + $query_params = $this->_disable_default_where_conditions_on_query_param($query_params); |
|
144 | 144 | // echo '$query_params'; |
145 | 145 | // var_dump($query_params); |
146 | 146 | return $this->get_other_model()->get_all($query_params); |
@@ -11,137 +11,137 @@ |
||
11 | 11 | */ |
12 | 12 | class EE_Belongs_To_Relation extends EE_Model_Relation_Base |
13 | 13 | { |
14 | - /** |
|
15 | - * Object representing the relationship between two models. Belongs_To means that THIS model has the foreign key |
|
16 | - * to the other model. This knows how to join the models, |
|
17 | - * get related models across the relation, and add-and-remove the relationships. |
|
18 | - * |
|
19 | - * @param boolean $block_deletes For Belongs_To relations, this is set to FALSE by |
|
20 | - * default. if there are related models across this |
|
21 | - * relation, block (prevent and add an error) the |
|
22 | - * deletion of this model |
|
23 | - * @param string $related_model_objects_deletion_error_message a customized error message on blocking deletes |
|
24 | - * instead of the default |
|
25 | - */ |
|
26 | - public function __construct($block_deletes = false, $related_model_objects_deletion_error_message = null) |
|
27 | - { |
|
28 | - parent::__construct($block_deletes, $related_model_objects_deletion_error_message); |
|
29 | - } |
|
14 | + /** |
|
15 | + * Object representing the relationship between two models. Belongs_To means that THIS model has the foreign key |
|
16 | + * to the other model. This knows how to join the models, |
|
17 | + * get related models across the relation, and add-and-remove the relationships. |
|
18 | + * |
|
19 | + * @param boolean $block_deletes For Belongs_To relations, this is set to FALSE by |
|
20 | + * default. if there are related models across this |
|
21 | + * relation, block (prevent and add an error) the |
|
22 | + * deletion of this model |
|
23 | + * @param string $related_model_objects_deletion_error_message a customized error message on blocking deletes |
|
24 | + * instead of the default |
|
25 | + */ |
|
26 | + public function __construct($block_deletes = false, $related_model_objects_deletion_error_message = null) |
|
27 | + { |
|
28 | + parent::__construct($block_deletes, $related_model_objects_deletion_error_message); |
|
29 | + } |
|
30 | 30 | |
31 | 31 | |
32 | - /** |
|
33 | - * get_join_statement |
|
34 | - * |
|
35 | - * @param string $model_relation_chain |
|
36 | - * @return string |
|
37 | - * @throws \EE_Error |
|
38 | - */ |
|
39 | - public function get_join_statement($model_relation_chain) |
|
40 | - { |
|
41 | - // create the sql string like |
|
42 | - $this_table_fk_field = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
43 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
44 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
45 | - $model_relation_chain, |
|
46 | - $this->get_this_model()->get_this_model_name() |
|
47 | - ) . $this_table_fk_field->get_table_alias(); |
|
48 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
49 | - $model_relation_chain, |
|
50 | - $this->get_other_model()->get_this_model_name() |
|
51 | - ) . $other_table_pk_field->get_table_alias(); |
|
52 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
53 | - return $this->_left_join( |
|
54 | - $other_table, |
|
55 | - $other_table_alias, |
|
56 | - $other_table_pk_field->get_table_column(), |
|
57 | - $this_table_alias, |
|
58 | - $this_table_fk_field->get_table_column() |
|
59 | - ) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
60 | - } |
|
32 | + /** |
|
33 | + * get_join_statement |
|
34 | + * |
|
35 | + * @param string $model_relation_chain |
|
36 | + * @return string |
|
37 | + * @throws \EE_Error |
|
38 | + */ |
|
39 | + public function get_join_statement($model_relation_chain) |
|
40 | + { |
|
41 | + // create the sql string like |
|
42 | + $this_table_fk_field = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
43 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
44 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
45 | + $model_relation_chain, |
|
46 | + $this->get_this_model()->get_this_model_name() |
|
47 | + ) . $this_table_fk_field->get_table_alias(); |
|
48 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
49 | + $model_relation_chain, |
|
50 | + $this->get_other_model()->get_this_model_name() |
|
51 | + ) . $other_table_pk_field->get_table_alias(); |
|
52 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
53 | + return $this->_left_join( |
|
54 | + $other_table, |
|
55 | + $other_table_alias, |
|
56 | + $other_table_pk_field->get_table_column(), |
|
57 | + $this_table_alias, |
|
58 | + $this_table_fk_field->get_table_column() |
|
59 | + ) . $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
60 | + } |
|
61 | 61 | |
62 | 62 | |
63 | - /** |
|
64 | - * Sets this model object's foreign key to the other model object's primary key. Feel free to do this manually if |
|
65 | - * you like. |
|
66 | - * |
|
67 | - * @param EE_Base_Class|int $this_obj_or_id |
|
68 | - * @param EE_Base_Class|int $other_obj_or_id |
|
69 | - * @param array $extra_join_model_fields_n_values |
|
70 | - * @return \EE_Base_Class |
|
71 | - * @throws \EE_Error |
|
72 | - */ |
|
73 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
74 | - { |
|
75 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
76 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
77 | - // find the field on the other model which is a foreign key to this model |
|
78 | - $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
79 | - if ($this_model_obj->get($fk_on_this_model->get_name()) != $other_model_obj->ID()) { |
|
80 | - // set that field on the other model to this model's ID |
|
81 | - $this_model_obj->set($fk_on_this_model->get_name(), $other_model_obj->ID()); |
|
82 | - $this_model_obj->save(); |
|
83 | - } |
|
84 | - return $other_model_obj; |
|
85 | - } |
|
63 | + /** |
|
64 | + * Sets this model object's foreign key to the other model object's primary key. Feel free to do this manually if |
|
65 | + * you like. |
|
66 | + * |
|
67 | + * @param EE_Base_Class|int $this_obj_or_id |
|
68 | + * @param EE_Base_Class|int $other_obj_or_id |
|
69 | + * @param array $extra_join_model_fields_n_values |
|
70 | + * @return \EE_Base_Class |
|
71 | + * @throws \EE_Error |
|
72 | + */ |
|
73 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
74 | + { |
|
75 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
76 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
77 | + // find the field on the other model which is a foreign key to this model |
|
78 | + $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
79 | + if ($this_model_obj->get($fk_on_this_model->get_name()) != $other_model_obj->ID()) { |
|
80 | + // set that field on the other model to this model's ID |
|
81 | + $this_model_obj->set($fk_on_this_model->get_name(), $other_model_obj->ID()); |
|
82 | + $this_model_obj->save(); |
|
83 | + } |
|
84 | + return $other_model_obj; |
|
85 | + } |
|
86 | 86 | |
87 | 87 | |
88 | - /** |
|
89 | - * Sets the this model object's foreign key to its default, instead of pointing to the other model object |
|
90 | - * |
|
91 | - * @param EE_Base_Class|int $this_obj_or_id |
|
92 | - * @param EE_Base_Class|int $other_obj_or_id |
|
93 | - * @param array $where_query |
|
94 | - * @return \EE_Base_Class |
|
95 | - * @throws \EE_Error |
|
96 | - */ |
|
97 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
98 | - { |
|
99 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
100 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
101 | - // find the field on the other model which is a foreign key to this model |
|
102 | - $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
103 | - // set that field on the other model to this model's ID |
|
104 | - $this_model_obj->set($fk_on_this_model->get_name(), null, true); |
|
105 | - $this_model_obj->save(); |
|
106 | - return $other_model_obj; |
|
107 | - } |
|
88 | + /** |
|
89 | + * Sets the this model object's foreign key to its default, instead of pointing to the other model object |
|
90 | + * |
|
91 | + * @param EE_Base_Class|int $this_obj_or_id |
|
92 | + * @param EE_Base_Class|int $other_obj_or_id |
|
93 | + * @param array $where_query |
|
94 | + * @return \EE_Base_Class |
|
95 | + * @throws \EE_Error |
|
96 | + */ |
|
97 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
98 | + { |
|
99 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
100 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
101 | + // find the field on the other model which is a foreign key to this model |
|
102 | + $fk_on_this_model = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
103 | + // set that field on the other model to this model's ID |
|
104 | + $this_model_obj->set($fk_on_this_model->get_name(), null, true); |
|
105 | + $this_model_obj->save(); |
|
106 | + return $other_model_obj; |
|
107 | + } |
|
108 | 108 | |
109 | 109 | |
110 | - /** |
|
111 | - * Overrides parent so that we don't NEED to save the $model_object before getting the related objects. |
|
112 | - * |
|
113 | - * @param EE_Base_Class $model_obj_or_id |
|
114 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
115 | - * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
116 | - * @return EE_Base_Class[] |
|
117 | - * @throws \EE_Error |
|
118 | - */ |
|
119 | - public function get_all_related( |
|
120 | - $model_obj_or_id, |
|
121 | - $query_params = array(), |
|
122 | - $values_already_prepared_by_model_object = false |
|
123 | - ) { |
|
124 | - if ($values_already_prepared_by_model_object !== false) { |
|
125 | - EE_Error::doing_it_wrong( |
|
126 | - 'EE_Model_Relation_Base::get_all_related', |
|
127 | - esc_html__('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
128 | - '4.8.1' |
|
129 | - ); |
|
130 | - } |
|
131 | - // get column on this model object which is a foreign key to the other model |
|
132 | - $fk_field_obj = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
133 | - // get its value |
|
134 | - if ($model_obj_or_id instanceof EE_Base_Class) { |
|
135 | - $model_obj = $model_obj_or_id; |
|
136 | - } else { |
|
137 | - $model_obj = $this->get_this_model()->ensure_is_obj($model_obj_or_id); |
|
138 | - } |
|
139 | - $ID_value_on_other_model = $model_obj->get($fk_field_obj->get_name()); |
|
140 | - // get all where their PK matches that value |
|
141 | - $query_params[0][ $this->get_other_model()->get_primary_key_field()->get_name() ] = $ID_value_on_other_model; |
|
142 | - $query_params = $this->_disable_default_where_conditions_on_query_param($query_params); |
|
110 | + /** |
|
111 | + * Overrides parent so that we don't NEED to save the $model_object before getting the related objects. |
|
112 | + * |
|
113 | + * @param EE_Base_Class $model_obj_or_id |
|
114 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
115 | + * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
116 | + * @return EE_Base_Class[] |
|
117 | + * @throws \EE_Error |
|
118 | + */ |
|
119 | + public function get_all_related( |
|
120 | + $model_obj_or_id, |
|
121 | + $query_params = array(), |
|
122 | + $values_already_prepared_by_model_object = false |
|
123 | + ) { |
|
124 | + if ($values_already_prepared_by_model_object !== false) { |
|
125 | + EE_Error::doing_it_wrong( |
|
126 | + 'EE_Model_Relation_Base::get_all_related', |
|
127 | + esc_html__('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
128 | + '4.8.1' |
|
129 | + ); |
|
130 | + } |
|
131 | + // get column on this model object which is a foreign key to the other model |
|
132 | + $fk_field_obj = $this->get_this_model()->get_foreign_key_to($this->get_other_model()->get_this_model_name()); |
|
133 | + // get its value |
|
134 | + if ($model_obj_or_id instanceof EE_Base_Class) { |
|
135 | + $model_obj = $model_obj_or_id; |
|
136 | + } else { |
|
137 | + $model_obj = $this->get_this_model()->ensure_is_obj($model_obj_or_id); |
|
138 | + } |
|
139 | + $ID_value_on_other_model = $model_obj->get($fk_field_obj->get_name()); |
|
140 | + // get all where their PK matches that value |
|
141 | + $query_params[0][ $this->get_other_model()->get_primary_key_field()->get_name() ] = $ID_value_on_other_model; |
|
142 | + $query_params = $this->_disable_default_where_conditions_on_query_param($query_params); |
|
143 | 143 | // echo '$query_params'; |
144 | 144 | // var_dump($query_params); |
145 | - return $this->get_other_model()->get_all($query_params); |
|
146 | - } |
|
145 | + return $this->get_other_model()->get_all($query_params); |
|
146 | + } |
|
147 | 147 | } |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | } else { |
63 | 63 | $order = 'second'; |
64 | 64 | } |
65 | - return $this->get_join_model()->field_settings_for('EXJ_' . $order . '_model_' . $id_or_name_field); |
|
65 | + return $this->get_join_model()->field_settings_for('EXJ_'.$order.'_model_'.$id_or_name_field); |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | |
@@ -87,19 +87,19 @@ discard block |
||
87 | 87 | $this->get_this_model()->get_this_model_name(), |
88 | 88 | 'ID' |
89 | 89 | ); |
90 | - $field_with_model_name = $this->get_join_table_fk_field_to( |
|
90 | + $field_with_model_name = $this->get_join_table_fk_field_to( |
|
91 | 91 | $this->get_this_model()->get_this_model_name(), |
92 | 92 | 'name' |
93 | 93 | ); |
94 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
94 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
95 | 95 | $model_relation_chain, |
96 | 96 | $this->get_this_model()->get_this_model_name() |
97 | - ) . $this_table_pk_field->get_table_alias(); |
|
98 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
97 | + ).$this_table_pk_field->get_table_alias(); |
|
98 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
99 | 99 | $model_relation_chain, |
100 | 100 | $this->get_join_model()->get_this_model_name() |
101 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
102 | - $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
101 | + ).$join_table_fk_field_to_this_table->get_table_alias(); |
|
102 | + $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
103 | 103 | // phew! ok, we have all the info we need, now we can create the SQL join string |
104 | 104 | $SQL = $this->_left_join( |
105 | 105 | $join_table, |
@@ -107,8 +107,8 @@ discard block |
||
107 | 107 | $join_table_fk_field_to_this_table->get_table_column(), |
108 | 108 | $this_table_alias, |
109 | 109 | $this_table_pk_field->get_table_column(), |
110 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_this_model()->get_this_model_name() . "'" |
|
111 | - ) . |
|
110 | + $field_with_model_name->get_qualified_column()."='".$this->get_this_model()->get_this_model_name()."'" |
|
111 | + ). |
|
112 | 112 | $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
113 | 113 | |
114 | 114 | return $SQL; |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | 'event_espresso' |
135 | 135 | ))); |
136 | 136 | } |
137 | - $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
137 | + $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
138 | 138 | $this->get_this_model()->get_this_model_name(), |
139 | 139 | 'ID' |
140 | 140 | ); |
@@ -142,7 +142,7 @@ discard block |
||
142 | 142 | $this->get_other_model()->get_this_model_name(), |
143 | 143 | 'ID' |
144 | 144 | ); |
145 | - $field_with_other_model_name = $this->get_join_table_fk_field_to( |
|
145 | + $field_with_other_model_name = $this->get_join_table_fk_field_to( |
|
146 | 146 | $this->get_other_model()->get_this_model_name(), |
147 | 147 | 'name' |
148 | 148 | ); |
@@ -150,14 +150,14 @@ discard block |
||
150 | 150 | $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
151 | 151 | $this->_model_relation_chain_to_join_model, |
152 | 152 | $this->get_join_model()->get_this_model_name() |
153 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
153 | + ).$join_table_fk_field_to_this_table->get_table_alias(); |
|
154 | 154 | |
155 | 155 | $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
156 | 156 | $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
157 | 157 | $model_relation_chain, |
158 | 158 | $this->get_other_model()->get_this_model_name() |
159 | - ) . $other_table_pk_field->get_table_alias(); |
|
160 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
159 | + ).$other_table_pk_field->get_table_alias(); |
|
160 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
161 | 161 | |
162 | 162 | $SQL = $this->_left_join( |
163 | 163 | $other_table, |
@@ -165,8 +165,8 @@ discard block |
||
165 | 165 | $other_table_pk_field->get_table_column(), |
166 | 166 | $join_table_alias, |
167 | 167 | $join_table_fk_field_to_other_table->get_table_column(), |
168 | - $field_with_other_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'" |
|
169 | - ) . |
|
168 | + $field_with_other_model_name->get_qualified_column()."='".$this->get_other_model()->get_this_model_name()."'" |
|
169 | + ). |
|
170 | 170 | $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
171 | 171 | return $SQL; |
172 | 172 | } |
@@ -188,15 +188,15 @@ discard block |
||
188 | 188 | $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
189 | 189 | $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
190 | 190 | // check if such a relationship already exists |
191 | - $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
191 | + $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
192 | 192 | $this->get_this_model()->get_this_model_name(), |
193 | 193 | 'ID' |
194 | 194 | ); |
195 | - $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
195 | + $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
196 | 196 | $this->get_this_model()->get_this_model_name(), |
197 | 197 | 'name' |
198 | 198 | ); |
199 | - $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
199 | + $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
200 | 200 | $this->get_other_model()->get_this_model_name(), |
201 | 201 | 'ID' |
202 | 202 | ); |
@@ -213,17 +213,17 @@ discard block |
||
213 | 213 | ); |
214 | 214 | |
215 | 215 | // if $where_query exists lets add them to the query_params. |
216 | - if (! empty($extra_join_model_fields_n_values)) { |
|
216 | + if ( ! empty($extra_join_model_fields_n_values)) { |
|
217 | 217 | // 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) |
218 | 218 | // make sure we strip THIS models name from the query param |
219 | 219 | $parsed_query = array(); |
220 | 220 | foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
221 | - $query_param = str_replace( |
|
222 | - $this->get_join_model()->get_this_model_name() . ".", |
|
221 | + $query_param = str_replace( |
|
222 | + $this->get_join_model()->get_this_model_name().".", |
|
223 | 223 | "", |
224 | 224 | $query_param |
225 | 225 | ); |
226 | - $parsed_query[ $query_param ] = $val; |
|
226 | + $parsed_query[$query_param] = $val; |
|
227 | 227 | } |
228 | 228 | $cols_n_values = array_merge($cols_n_values, $parsed_query); |
229 | 229 | } |
@@ -235,7 +235,7 @@ discard block |
||
235 | 235 | // if there is already an entry in the join table, indicating a relationship, we're done |
236 | 236 | // again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
237 | 237 | // the other tables, use the joining model directly! |
238 | - if (! $existing_entry_in_join_table) { |
|
238 | + if ( ! $existing_entry_in_join_table) { |
|
239 | 239 | $this->get_join_model()->insert($cols_n_values); |
240 | 240 | } |
241 | 241 | return $other_model_obj; |
@@ -257,15 +257,15 @@ discard block |
||
257 | 257 | $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
258 | 258 | $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
259 | 259 | // check if such a relationship already exists |
260 | - $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
260 | + $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
261 | 261 | $this->get_this_model()->get_this_model_name(), |
262 | 262 | 'ID' |
263 | 263 | ); |
264 | - $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
264 | + $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
265 | 265 | $this->get_this_model()->get_this_model_name(), |
266 | 266 | 'name' |
267 | 267 | ); |
268 | - $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
268 | + $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
269 | 269 | $this->get_other_model()->get_this_model_name(), |
270 | 270 | 'ID' |
271 | 271 | ); |
@@ -282,17 +282,17 @@ discard block |
||
282 | 282 | ); |
283 | 283 | |
284 | 284 | // if $where_query exists lets add them to the query_params. |
285 | - if (! empty($where_query)) { |
|
285 | + if ( ! empty($where_query)) { |
|
286 | 286 | // 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) |
287 | 287 | // make sure we strip THIS models name from the query param |
288 | 288 | $parsed_query = array(); |
289 | 289 | foreach ($where_query as $query_param => $val) { |
290 | - $query_param = str_replace( |
|
291 | - $this->get_join_model()->get_this_model_name() . ".", |
|
290 | + $query_param = str_replace( |
|
291 | + $this->get_join_model()->get_this_model_name().".", |
|
292 | 292 | "", |
293 | 293 | $query_param |
294 | 294 | ); |
295 | - $parsed_query[ $query_param ] = $val; |
|
295 | + $parsed_query[$query_param] = $val; |
|
296 | 296 | } |
297 | 297 | $cols_n_values = array_merge($cols_n_values, $parsed_query); |
298 | 298 | } |
@@ -10,294 +10,294 @@ |
||
10 | 10 | */ |
11 | 11 | class EE_HABTM_Any_Relation extends EE_HABTM_Relation |
12 | 12 | { |
13 | - /** |
|
14 | - * @var string |
|
15 | - */ |
|
16 | - protected $_alphabetically_first_model_name; |
|
13 | + /** |
|
14 | + * @var string |
|
15 | + */ |
|
16 | + protected $_alphabetically_first_model_name; |
|
17 | 17 | |
18 | - /** |
|
19 | - * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
20 | - * (and an ee joining-model.) This knows how to join the models, |
|
21 | - * get related models across the relation, and add-and-remove the relationships. |
|
22 | - * |
|
23 | - * @param boolean $block_deletes for this type of relation, we block by default for now. if there |
|
24 | - * are related models across this relation, block (prevent and add an |
|
25 | - * error) the deletion of this model |
|
26 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
27 | - * default |
|
28 | - */ |
|
29 | - public function __construct($block_deletes = true, $blocking_delete_error_message = '') |
|
30 | - { |
|
31 | - parent::__construct('Extra_Join', $block_deletes, $blocking_delete_error_message); |
|
32 | - } |
|
18 | + /** |
|
19 | + * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table |
|
20 | + * (and an ee joining-model.) This knows how to join the models, |
|
21 | + * get related models across the relation, and add-and-remove the relationships. |
|
22 | + * |
|
23 | + * @param boolean $block_deletes for this type of relation, we block by default for now. if there |
|
24 | + * are related models across this relation, block (prevent and add an |
|
25 | + * error) the deletion of this model |
|
26 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
27 | + * default |
|
28 | + */ |
|
29 | + public function __construct($block_deletes = true, $blocking_delete_error_message = '') |
|
30 | + { |
|
31 | + parent::__construct('Extra_Join', $block_deletes, $blocking_delete_error_message); |
|
32 | + } |
|
33 | 33 | |
34 | 34 | |
35 | - /** |
|
36 | - * @param $this_model_name |
|
37 | - * @param $other_model_name |
|
38 | - * @throws EE_Error |
|
39 | - */ |
|
40 | - public function _construct_finalize_set_models($this_model_name, $other_model_name) |
|
41 | - { |
|
42 | - if ($this_model_name < $other_model_name) { |
|
43 | - $this->_alphabetically_first_model_name = $this_model_name; |
|
44 | - } else { |
|
45 | - $this->_alphabetically_first_model_name = $other_model_name; |
|
46 | - } |
|
47 | - parent::_construct_finalize_set_models($this_model_name, $other_model_name); |
|
48 | - } |
|
35 | + /** |
|
36 | + * @param $this_model_name |
|
37 | + * @param $other_model_name |
|
38 | + * @throws EE_Error |
|
39 | + */ |
|
40 | + public function _construct_finalize_set_models($this_model_name, $other_model_name) |
|
41 | + { |
|
42 | + if ($this_model_name < $other_model_name) { |
|
43 | + $this->_alphabetically_first_model_name = $this_model_name; |
|
44 | + } else { |
|
45 | + $this->_alphabetically_first_model_name = $other_model_name; |
|
46 | + } |
|
47 | + parent::_construct_finalize_set_models($this_model_name, $other_model_name); |
|
48 | + } |
|
49 | 49 | |
50 | 50 | |
51 | - /** |
|
52 | - * @param string $model_name |
|
53 | - * @param string $id_or_name_field should be the string 'ID' or 'name' only |
|
54 | - * @return EE_Model_Field_Base |
|
55 | - * @throws \EE_Error |
|
56 | - */ |
|
57 | - public function get_join_table_fk_field_to($model_name, $id_or_name_field) |
|
58 | - { |
|
59 | - $order = null; |
|
60 | - if ($model_name === $this->_alphabetically_first_model_name) { |
|
61 | - $order = 'first'; |
|
62 | - } else { |
|
63 | - $order = 'second'; |
|
64 | - } |
|
65 | - return $this->get_join_model()->field_settings_for('EXJ_' . $order . '_model_' . $id_or_name_field); |
|
66 | - } |
|
51 | + /** |
|
52 | + * @param string $model_name |
|
53 | + * @param string $id_or_name_field should be the string 'ID' or 'name' only |
|
54 | + * @return EE_Model_Field_Base |
|
55 | + * @throws \EE_Error |
|
56 | + */ |
|
57 | + public function get_join_table_fk_field_to($model_name, $id_or_name_field) |
|
58 | + { |
|
59 | + $order = null; |
|
60 | + if ($model_name === $this->_alphabetically_first_model_name) { |
|
61 | + $order = 'first'; |
|
62 | + } else { |
|
63 | + $order = 'second'; |
|
64 | + } |
|
65 | + return $this->get_join_model()->field_settings_for('EXJ_' . $order . '_model_' . $id_or_name_field); |
|
66 | + } |
|
67 | 67 | |
68 | 68 | |
69 | - /** |
|
70 | - * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
71 | - * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
72 | - * |
|
73 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
74 | - * @return string of SQL |
|
75 | - * @throws \EE_Error |
|
76 | - */ |
|
77 | - public function get_join_to_intermediate_model_statement($model_relation_chain) |
|
78 | - { |
|
79 | - // create sql like |
|
80 | - // LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
81 | - // LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
82 | - // remember the model relation chain to the JOIN model, because we'll |
|
83 | - // need it for get_join_statement() |
|
84 | - $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
85 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
86 | - $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
87 | - $this->get_this_model()->get_this_model_name(), |
|
88 | - 'ID' |
|
89 | - ); |
|
90 | - $field_with_model_name = $this->get_join_table_fk_field_to( |
|
91 | - $this->get_this_model()->get_this_model_name(), |
|
92 | - 'name' |
|
93 | - ); |
|
94 | - $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
95 | - $model_relation_chain, |
|
96 | - $this->get_this_model()->get_this_model_name() |
|
97 | - ) . $this_table_pk_field->get_table_alias(); |
|
98 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
99 | - $model_relation_chain, |
|
100 | - $this->get_join_model()->get_this_model_name() |
|
101 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
102 | - $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
103 | - // phew! ok, we have all the info we need, now we can create the SQL join string |
|
104 | - $SQL = $this->_left_join( |
|
105 | - $join_table, |
|
106 | - $join_table_alias, |
|
107 | - $join_table_fk_field_to_this_table->get_table_column(), |
|
108 | - $this_table_alias, |
|
109 | - $this_table_pk_field->get_table_column(), |
|
110 | - $field_with_model_name->get_qualified_column() . "='" . $this->get_this_model()->get_this_model_name() . "'" |
|
111 | - ) . |
|
112 | - $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
69 | + /** |
|
70 | + * Gets the SQL string for joining the main model's table containing the pk to the join table. Eg "LEFT JOIN |
|
71 | + * real_join_table AS join_table_alias ON this_table_alias.pk = join_table_alias.fk_to_this_table" |
|
72 | + * |
|
73 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
74 | + * @return string of SQL |
|
75 | + * @throws \EE_Error |
|
76 | + */ |
|
77 | + public function get_join_to_intermediate_model_statement($model_relation_chain) |
|
78 | + { |
|
79 | + // create sql like |
|
80 | + // LEFT JOIN join_table AS join_table_alias ON this_table_alias.this_table_pk = join_table_alias.join_table_fk_to_this |
|
81 | + // LEFT JOIN other_table AS other_table_alias ON join_table_alias.join_table_fk_to_other = other_table_alias.other_table_pk |
|
82 | + // remember the model relation chain to the JOIN model, because we'll |
|
83 | + // need it for get_join_statement() |
|
84 | + $this->_model_relation_chain_to_join_model = $model_relation_chain; |
|
85 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
86 | + $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
87 | + $this->get_this_model()->get_this_model_name(), |
|
88 | + 'ID' |
|
89 | + ); |
|
90 | + $field_with_model_name = $this->get_join_table_fk_field_to( |
|
91 | + $this->get_this_model()->get_this_model_name(), |
|
92 | + 'name' |
|
93 | + ); |
|
94 | + $this_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
95 | + $model_relation_chain, |
|
96 | + $this->get_this_model()->get_this_model_name() |
|
97 | + ) . $this_table_pk_field->get_table_alias(); |
|
98 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
99 | + $model_relation_chain, |
|
100 | + $this->get_join_model()->get_this_model_name() |
|
101 | + ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
102 | + $join_table = $this->get_join_model()->get_table_for_alias($join_table_alias); |
|
103 | + // phew! ok, we have all the info we need, now we can create the SQL join string |
|
104 | + $SQL = $this->_left_join( |
|
105 | + $join_table, |
|
106 | + $join_table_alias, |
|
107 | + $join_table_fk_field_to_this_table->get_table_column(), |
|
108 | + $this_table_alias, |
|
109 | + $this_table_pk_field->get_table_column(), |
|
110 | + $field_with_model_name->get_qualified_column() . "='" . $this->get_this_model()->get_this_model_name() . "'" |
|
111 | + ) . |
|
112 | + $this->get_join_model()->_construct_internal_join_to_table_with_alias($join_table_alias); |
|
113 | 113 | |
114 | - return $SQL; |
|
115 | - } |
|
114 | + return $SQL; |
|
115 | + } |
|
116 | 116 | |
117 | 117 | |
118 | - /** |
|
119 | - * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
120 | - * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
121 | - * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
122 | - * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
123 | - * the intermediate join table, and then to the other model's pk's table |
|
124 | - * |
|
125 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
126 | - * @return string of SQL |
|
127 | - * @throws \EE_Error |
|
128 | - */ |
|
129 | - public function get_join_statement($model_relation_chain) |
|
130 | - { |
|
131 | - if ($this->_model_relation_chain_to_join_model === null) { |
|
132 | - throw new EE_Error(sprintf(esc_html__( |
|
133 | - 'When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
134 | - 'event_espresso' |
|
135 | - ))); |
|
136 | - } |
|
137 | - $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
138 | - $this->get_this_model()->get_this_model_name(), |
|
139 | - 'ID' |
|
140 | - ); |
|
141 | - $join_table_fk_field_to_other_table = $this->get_join_table_fk_field_to( |
|
142 | - $this->get_other_model()->get_this_model_name(), |
|
143 | - 'ID' |
|
144 | - ); |
|
145 | - $field_with_other_model_name = $this->get_join_table_fk_field_to( |
|
146 | - $this->get_other_model()->get_this_model_name(), |
|
147 | - 'name' |
|
148 | - ); |
|
118 | + /** |
|
119 | + * Gets the SQL string for joining the join table to the other model's pk's table. Eg "LEFT JOIN real_other_table |
|
120 | + * AS other_table_alias ON join_table_alias.fk_to_other_table = other_table_alias.pk" If you want to join between |
|
121 | + * modelA -> joinModelAB -> modelB (eg, Event -> Event_Question_Group -> Question_Group), you should prepend the |
|
122 | + * result of this function with results from get_join_to_intermediate_model_statement(), so that you join first to |
|
123 | + * the intermediate join table, and then to the other model's pk's table |
|
124 | + * |
|
125 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
126 | + * @return string of SQL |
|
127 | + * @throws \EE_Error |
|
128 | + */ |
|
129 | + public function get_join_statement($model_relation_chain) |
|
130 | + { |
|
131 | + if ($this->_model_relation_chain_to_join_model === null) { |
|
132 | + throw new EE_Error(sprintf(esc_html__( |
|
133 | + 'When using EE_HABTM_Relation to create a join, you must call get_join_to_intermediate_model_statement BEFORE get_join_statement', |
|
134 | + 'event_espresso' |
|
135 | + ))); |
|
136 | + } |
|
137 | + $join_table_fk_field_to_this_table = $this->get_join_table_fk_field_to( |
|
138 | + $this->get_this_model()->get_this_model_name(), |
|
139 | + 'ID' |
|
140 | + ); |
|
141 | + $join_table_fk_field_to_other_table = $this->get_join_table_fk_field_to( |
|
142 | + $this->get_other_model()->get_this_model_name(), |
|
143 | + 'ID' |
|
144 | + ); |
|
145 | + $field_with_other_model_name = $this->get_join_table_fk_field_to( |
|
146 | + $this->get_other_model()->get_this_model_name(), |
|
147 | + 'name' |
|
148 | + ); |
|
149 | 149 | |
150 | - $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
151 | - $this->_model_relation_chain_to_join_model, |
|
152 | - $this->get_join_model()->get_this_model_name() |
|
153 | - ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
150 | + $join_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
151 | + $this->_model_relation_chain_to_join_model, |
|
152 | + $this->get_join_model()->get_this_model_name() |
|
153 | + ) . $join_table_fk_field_to_this_table->get_table_alias(); |
|
154 | 154 | |
155 | - $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
156 | - $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
157 | - $model_relation_chain, |
|
158 | - $this->get_other_model()->get_this_model_name() |
|
159 | - ) . $other_table_pk_field->get_table_alias(); |
|
160 | - $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
155 | + $other_table_pk_field = $this->get_other_model()->get_primary_key_field(); |
|
156 | + $other_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
157 | + $model_relation_chain, |
|
158 | + $this->get_other_model()->get_this_model_name() |
|
159 | + ) . $other_table_pk_field->get_table_alias(); |
|
160 | + $other_table = $this->get_other_model()->get_table_for_alias($other_table_alias); |
|
161 | 161 | |
162 | - $SQL = $this->_left_join( |
|
163 | - $other_table, |
|
164 | - $other_table_alias, |
|
165 | - $other_table_pk_field->get_table_column(), |
|
166 | - $join_table_alias, |
|
167 | - $join_table_fk_field_to_other_table->get_table_column(), |
|
168 | - $field_with_other_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'" |
|
169 | - ) . |
|
170 | - $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
171 | - return $SQL; |
|
172 | - } |
|
162 | + $SQL = $this->_left_join( |
|
163 | + $other_table, |
|
164 | + $other_table_alias, |
|
165 | + $other_table_pk_field->get_table_column(), |
|
166 | + $join_table_alias, |
|
167 | + $join_table_fk_field_to_other_table->get_table_column(), |
|
168 | + $field_with_other_model_name->get_qualified_column() . "='" . $this->get_other_model()->get_this_model_name() . "'" |
|
169 | + ) . |
|
170 | + $this->get_other_model()->_construct_internal_join_to_table_with_alias($other_table_alias); |
|
171 | + return $SQL; |
|
172 | + } |
|
173 | 173 | |
174 | 174 | |
175 | - /** |
|
176 | - * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
177 | - * |
|
178 | - * @param EE_Base_Class|int $this_obj_or_id |
|
179 | - * @param EE_Base_Class|int $other_obj_or_id |
|
180 | - * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
181 | - * checking existing values and for setting new rows if |
|
182 | - * no exact matches. |
|
183 | - * @return EE_Base_Class |
|
184 | - * @throws \EE_Error |
|
185 | - */ |
|
186 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
187 | - { |
|
188 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
189 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
190 | - // check if such a relationship already exists |
|
191 | - $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
192 | - $this->get_this_model()->get_this_model_name(), |
|
193 | - 'ID' |
|
194 | - ); |
|
195 | - $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
196 | - $this->get_this_model()->get_this_model_name(), |
|
197 | - 'name' |
|
198 | - ); |
|
199 | - $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
200 | - $this->get_other_model()->get_this_model_name(), |
|
201 | - 'ID' |
|
202 | - ); |
|
203 | - $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
204 | - $this->get_other_model()->get_this_model_name(), |
|
205 | - 'name' |
|
206 | - ); |
|
175 | + /** |
|
176 | + * Ensures there is an entry in the join table between these two models. Feel free to do this manually if you like. |
|
177 | + * |
|
178 | + * @param EE_Base_Class|int $this_obj_or_id |
|
179 | + * @param EE_Base_Class|int $other_obj_or_id |
|
180 | + * @param array $extra_join_model_fields_n_values col=>val pairs that are used as extra conditions for |
|
181 | + * checking existing values and for setting new rows if |
|
182 | + * no exact matches. |
|
183 | + * @return EE_Base_Class |
|
184 | + * @throws \EE_Error |
|
185 | + */ |
|
186 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
187 | + { |
|
188 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
189 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
190 | + // check if such a relationship already exists |
|
191 | + $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
192 | + $this->get_this_model()->get_this_model_name(), |
|
193 | + 'ID' |
|
194 | + ); |
|
195 | + $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
196 | + $this->get_this_model()->get_this_model_name(), |
|
197 | + 'name' |
|
198 | + ); |
|
199 | + $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
200 | + $this->get_other_model()->get_this_model_name(), |
|
201 | + 'ID' |
|
202 | + ); |
|
203 | + $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
204 | + $this->get_other_model()->get_this_model_name(), |
|
205 | + 'name' |
|
206 | + ); |
|
207 | 207 | |
208 | - $cols_n_values = array( |
|
209 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
210 | - $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
211 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
212 | - $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
213 | - ); |
|
208 | + $cols_n_values = array( |
|
209 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
210 | + $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
211 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
212 | + $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
213 | + ); |
|
214 | 214 | |
215 | - // if $where_query exists lets add them to the query_params. |
|
216 | - if (! empty($extra_join_model_fields_n_values)) { |
|
217 | - // 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) |
|
218 | - // make sure we strip THIS models name from the query param |
|
219 | - $parsed_query = array(); |
|
220 | - foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
221 | - $query_param = str_replace( |
|
222 | - $this->get_join_model()->get_this_model_name() . ".", |
|
223 | - "", |
|
224 | - $query_param |
|
225 | - ); |
|
226 | - $parsed_query[ $query_param ] = $val; |
|
227 | - } |
|
228 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
229 | - } |
|
215 | + // if $where_query exists lets add them to the query_params. |
|
216 | + if (! empty($extra_join_model_fields_n_values)) { |
|
217 | + // 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) |
|
218 | + // make sure we strip THIS models name from the query param |
|
219 | + $parsed_query = array(); |
|
220 | + foreach ($extra_join_model_fields_n_values as $query_param => $val) { |
|
221 | + $query_param = str_replace( |
|
222 | + $this->get_join_model()->get_this_model_name() . ".", |
|
223 | + "", |
|
224 | + $query_param |
|
225 | + ); |
|
226 | + $parsed_query[ $query_param ] = $val; |
|
227 | + } |
|
228 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
229 | + } |
|
230 | 230 | |
231 | - $query_params = array($cols_n_values); |
|
231 | + $query_params = array($cols_n_values); |
|
232 | 232 | |
233 | 233 | |
234 | - $existing_entry_in_join_table = $this->get_join_model()->get_one($query_params); |
|
235 | - // if there is already an entry in the join table, indicating a relationship, we're done |
|
236 | - // again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
237 | - // the other tables, use the joining model directly! |
|
238 | - if (! $existing_entry_in_join_table) { |
|
239 | - $this->get_join_model()->insert($cols_n_values); |
|
240 | - } |
|
241 | - return $other_model_obj; |
|
242 | - } |
|
234 | + $existing_entry_in_join_table = $this->get_join_model()->get_one($query_params); |
|
235 | + // if there is already an entry in the join table, indicating a relationship, we're done |
|
236 | + // again, if you want more sophisticated logic or insertions (handling more columns than just 2 foreign keys to |
|
237 | + // the other tables, use the joining model directly! |
|
238 | + if (! $existing_entry_in_join_table) { |
|
239 | + $this->get_join_model()->insert($cols_n_values); |
|
240 | + } |
|
241 | + return $other_model_obj; |
|
242 | + } |
|
243 | 243 | |
244 | 244 | |
245 | - /** |
|
246 | - * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
247 | - * |
|
248 | - * @param EE_Base_Class|int $this_obj_or_id |
|
249 | - * @param EE_Base_Class|int $other_obj_or_id |
|
250 | - * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
251 | - * values and for removing existing rows if exact matches exist. |
|
252 | - * @return EE_Base_Class |
|
253 | - * @throws \EE_Error |
|
254 | - */ |
|
255 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
256 | - { |
|
257 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
258 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
259 | - // check if such a relationship already exists |
|
260 | - $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
261 | - $this->get_this_model()->get_this_model_name(), |
|
262 | - 'ID' |
|
263 | - ); |
|
264 | - $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
265 | - $this->get_this_model()->get_this_model_name(), |
|
266 | - 'name' |
|
267 | - ); |
|
268 | - $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
269 | - $this->get_other_model()->get_this_model_name(), |
|
270 | - 'ID' |
|
271 | - ); |
|
272 | - $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
273 | - $this->get_other_model()->get_this_model_name(), |
|
274 | - 'name' |
|
275 | - ); |
|
245 | + /** |
|
246 | + * Deletes any rows in the join table that have foreign keys matching the other model objects specified |
|
247 | + * |
|
248 | + * @param EE_Base_Class|int $this_obj_or_id |
|
249 | + * @param EE_Base_Class|int $other_obj_or_id |
|
250 | + * @param array $where_query col=>val pairs that are used as extra conditions for checking existing |
|
251 | + * values and for removing existing rows if exact matches exist. |
|
252 | + * @return EE_Base_Class |
|
253 | + * @throws \EE_Error |
|
254 | + */ |
|
255 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
256 | + { |
|
257 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
258 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
259 | + // check if such a relationship already exists |
|
260 | + $join_model_fk_to_this_model = $this->get_join_table_fk_field_to( |
|
261 | + $this->get_this_model()->get_this_model_name(), |
|
262 | + 'ID' |
|
263 | + ); |
|
264 | + $join_model_name_field_to_this_model = $this->get_join_table_fk_field_to( |
|
265 | + $this->get_this_model()->get_this_model_name(), |
|
266 | + 'name' |
|
267 | + ); |
|
268 | + $join_model_fk_to_other_model = $this->get_join_table_fk_field_to( |
|
269 | + $this->get_other_model()->get_this_model_name(), |
|
270 | + 'ID' |
|
271 | + ); |
|
272 | + $join_model_name_field_to_other_model = $this->get_join_table_fk_field_to( |
|
273 | + $this->get_other_model()->get_this_model_name(), |
|
274 | + 'name' |
|
275 | + ); |
|
276 | 276 | |
277 | - $cols_n_values = array( |
|
278 | - $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
279 | - $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
280 | - $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
281 | - $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
282 | - ); |
|
277 | + $cols_n_values = array( |
|
278 | + $join_model_fk_to_this_model->get_name() => $this_model_obj->ID(), |
|
279 | + $join_model_name_field_to_this_model->get_name() => $this_model_obj->get_model()->get_this_model_name(), |
|
280 | + $join_model_fk_to_other_model->get_name() => $other_model_obj->ID(), |
|
281 | + $join_model_name_field_to_other_model->get_name() => $other_model_obj->get_model()->get_this_model_name(), |
|
282 | + ); |
|
283 | 283 | |
284 | - // if $where_query exists lets add them to the query_params. |
|
285 | - if (! empty($where_query)) { |
|
286 | - // 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) |
|
287 | - // make sure we strip THIS models name from the query param |
|
288 | - $parsed_query = array(); |
|
289 | - foreach ($where_query as $query_param => $val) { |
|
290 | - $query_param = str_replace( |
|
291 | - $this->get_join_model()->get_this_model_name() . ".", |
|
292 | - "", |
|
293 | - $query_param |
|
294 | - ); |
|
295 | - $parsed_query[ $query_param ] = $val; |
|
296 | - } |
|
297 | - $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
298 | - } |
|
284 | + // if $where_query exists lets add them to the query_params. |
|
285 | + if (! empty($where_query)) { |
|
286 | + // 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) |
|
287 | + // make sure we strip THIS models name from the query param |
|
288 | + $parsed_query = array(); |
|
289 | + foreach ($where_query as $query_param => $val) { |
|
290 | + $query_param = str_replace( |
|
291 | + $this->get_join_model()->get_this_model_name() . ".", |
|
292 | + "", |
|
293 | + $query_param |
|
294 | + ); |
|
295 | + $parsed_query[ $query_param ] = $val; |
|
296 | + } |
|
297 | + $cols_n_values = array_merge($cols_n_values, $parsed_query); |
|
298 | + } |
|
299 | 299 | |
300 | - $this->get_join_model()->delete(array($cols_n_values)); |
|
301 | - return $other_model_obj; |
|
302 | - } |
|
300 | + $this->get_join_model()->delete(array($cols_n_values)); |
|
301 | + return $other_model_obj; |
|
302 | + } |
|
303 | 303 | } |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | // k this is where things differ because NOW what we're going to do is get the PARENTS for the get all related (and we'll also start setting up the return_objs array containing related that DON'T have parent ids, for those that DON'T have parents to merge with our returned objects); |
191 | 191 | foreach ($autosave_relations as $a_r) { |
192 | 192 | $pid = $a_r->parent(); |
193 | - if (! empty($pid)) { |
|
193 | + if ( ! empty($pid)) { |
|
194 | 194 | $parent_ids[] = $pid; |
195 | 195 | } else { |
196 | 196 | $return_objs[] = $a_r; |
@@ -206,9 +206,9 @@ discard block |
||
206 | 206 | } |
207 | 207 | |
208 | 208 | // now we setup the query to get all the parents |
209 | - if (! empty($parent_ids)) { |
|
210 | - $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() . "." . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
211 | - $query_param[0][ $query_param_where_this_model_pk ] = array('IN', $parent_ids); |
|
209 | + if ( ! empty($parent_ids)) { |
|
210 | + $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name().".".$this->get_this_model()->get_primary_key_field()->get_name(); |
|
211 | + $query_param[0][$query_param_where_this_model_pk] = array('IN', $parent_ids); |
|
212 | 212 | $parents = $this->get_other_model()->get_all($query_params); |
213 | 213 | } |
214 | 214 | |
@@ -243,7 +243,7 @@ discard block |
||
243 | 243 | var_dump($this_obj); |
244 | 244 | var_dump($other_obj);/**/ |
245 | 245 | |
246 | - if (! empty($parent_evt_id) && $parent_evt_id == $other_obj->get($this->_primary_cpt_field)) { |
|
246 | + if ( ! empty($parent_evt_id) && $parent_evt_id == $other_obj->get($this->_primary_cpt_field)) { |
|
247 | 247 | // let's do query on this objects model to see if the incoming pk value on the obj matches any parents in this objects table. |
248 | 248 | $has_parent_obj = $this->get_other_model()->get_one(array( |
249 | 249 | array( |
@@ -259,7 +259,7 @@ discard block |
||
259 | 259 | $other_obj->set($pk_on_related_model, $has_parent_obj->ID()); |
260 | 260 | $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
261 | 261 | |
262 | - if (! $remove_relation) { |
|
262 | + if ( ! $remove_relation) { |
|
263 | 263 | $other_obj->save(); |
264 | 264 | return array($other_obj); |
265 | 265 | } elseif ($remove_relation && ! $this->_blocking_delete) { |
@@ -287,7 +287,7 @@ discard block |
||
287 | 287 | // the last possible condition on a revision is that the incoming other_model object has a fk that == $this_obj pk which means we just return the $other obj and let it save as normal so we see the return at the bottom of this method. |
288 | 288 | } else { |
289 | 289 | // we only need to do the below IF this is not a remove relation |
290 | - if (! $remove_relation) { |
|
290 | + if ( ! $remove_relation) { |
|
291 | 291 | // okay this is is a normal update/save/remove so, let's make sure the other object is not a revision of the current object. |
292 | 292 | // the other object will likely NOT have the correct fk on it (which is the primary_cpt_field_mame) so we must retrieve from the db to get that first. |
293 | 293 | $existing_other_obj = $this->get_other_model()->get_one_by_ID($other_obj->ID()); |
@@ -10,303 +10,303 @@ |
||
10 | 10 | */ |
11 | 11 | class EE_Has_Many_Revision_Relation extends EE_Has_Many_Relation |
12 | 12 | { |
13 | - /** |
|
14 | - * The Foreign key on the model that acts as the PRIMARY KEY used in special autosave handling where we query for |
|
15 | - * autosaves (or the Foreign key on other models in relations pointing to this models primary key which is this |
|
16 | - * value). The _primary_cpt_field is what is equivalent to the post_id field on a cpt join. |
|
17 | - * |
|
18 | - * @var string |
|
19 | - */ |
|
20 | - private $_primary_cpt_field; |
|
21 | - |
|
22 | - |
|
23 | - /** |
|
24 | - * This is what field serves as the "parent" column that is linked with whatever the main model's calling this |
|
25 | - * relation has as a primary key. In other words EEM_Event has 'Datetime' => new |
|
26 | - * EE_Has_Many_Revision_Relation('EVT_ID', 'DTT_parent'). That means that in the EEM_Datetime model the |
|
27 | - * 'DTT_Parent' field is related to the 'DTT_ID' primary key field (in the same model) because 'DTT_ID' is the |
|
28 | - * primary key in the other model (EEM_Datetime). |
|
29 | - * |
|
30 | - * @var string |
|
31 | - */ |
|
32 | - private $_parent_pk_relation_field; |
|
33 | - |
|
34 | - |
|
35 | - /** |
|
36 | - * Object representing the relationship between two models. Has_Many_Relations are where the OTHER model has the |
|
37 | - * foreign key this model. IE, there can be many other model objects related to one of this model's objects (but |
|
38 | - * NOT through a JOIN table, which is the case for EE_HABTM_Relations). This knows how to join the models, get |
|
39 | - * related models across the relation, and add-and-remove the relationships. |
|
40 | - * |
|
41 | - * @param string $primary_cpt_field See property description for details |
|
42 | - * @param string $parent_pk_relation_field This is the field that is "connected" to the $primary_cpt_field. |
|
43 | - * See property desc for details. |
|
44 | - * @param boolean $block_deletes For this type of relation, we block by default. If there are |
|
45 | - * related models across this relation, block (prevent and add an |
|
46 | - * error) the deletion of this model |
|
47 | - * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
48 | - * default |
|
49 | - */ |
|
50 | - public function __construct( |
|
51 | - $primary_cpt_field, |
|
52 | - $parent_pk_relation_field, |
|
53 | - $block_deletes = true, |
|
54 | - $blocking_delete_error_message = null |
|
55 | - ) { |
|
56 | - $this->_primary_cpt_field = $primary_cpt_field; |
|
57 | - $this->_parent_pk_relation_field = $parent_pk_relation_field; |
|
58 | - parent::__construct($block_deletes, $blocking_delete_error_message); |
|
59 | - } |
|
60 | - |
|
61 | - |
|
62 | - /** |
|
63 | - * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
64 | - * you like. |
|
65 | - * |
|
66 | - * @param EE_Base_Class|int $this_obj_or_id |
|
67 | - * @param EE_Base_Class|int $other_obj_or_id |
|
68 | - * @param array $extra_join_model_fields_n_values |
|
69 | - * @return \EE_Base_Class |
|
70 | - * @throws \EE_Error |
|
71 | - */ |
|
72 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
73 | - { |
|
74 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
75 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
76 | - |
|
77 | - // handle possible revisions |
|
78 | - $other_model_obj = $this->_check_for_revision($this_model_obj, $other_model_obj); |
|
79 | - |
|
80 | - // if is array, then we've already done the add_relation so let's get out |
|
81 | - if (is_array($other_model_obj)) { |
|
82 | - return $other_model_obj[0]; |
|
83 | - } |
|
84 | - // find the field on the other model which is a foreign key to this model |
|
85 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
86 | - // set that field on the other model to this model's ID |
|
87 | - $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
88 | - $other_model_obj->save(); |
|
89 | - return $other_model_obj; |
|
90 | - } |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * Sets the other model object's foreign key to its default, instead of pointing to this model object |
|
95 | - * |
|
96 | - * @param EE_Base_Class|int $this_obj_or_id |
|
97 | - * @param EE_Base_Class|int $other_obj_or_id |
|
98 | - * @param array $where_query |
|
99 | - * @return \EE_Base_Class |
|
100 | - * @throws \EE_Error |
|
101 | - */ |
|
102 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
103 | - { |
|
104 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id); |
|
105 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
106 | - // handle possible revisions |
|
107 | - $other_model_obj = $this->_check_for_revision($this_model_obj, $other_model_obj, true); |
|
108 | - |
|
109 | - |
|
110 | - // if is array, then we've already done the add_relation so let's get out |
|
111 | - if (is_array($other_model_obj)) { |
|
112 | - return $other_model_obj[0]; |
|
113 | - } |
|
114 | - |
|
115 | - // find the field on the other model which is a foreign key to this model |
|
116 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
117 | - |
|
118 | - |
|
119 | - // set that field on the other model to this model's ID |
|
120 | - if ($this->_blocking_delete) { |
|
121 | - $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
122 | - $other_model_obj->save(); |
|
123 | - } else { |
|
124 | - $other_model_obj->delete(); |
|
125 | - $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
126 | - return $other_model_obj; |
|
127 | - } |
|
128 | - return $other_model_obj; |
|
129 | - } |
|
130 | - |
|
131 | - |
|
132 | - /** |
|
133 | - * This is identical to EE_Model_Relation->get_all_related() except we're going handle special autosave conditions |
|
134 | - * in here. |
|
135 | - * |
|
136 | - * @param EE_Base_Class|int $model_object_or_id |
|
137 | - * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
138 | - * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
139 | - * @return EE_Base_Class[] |
|
140 | - * @throws \EE_Error |
|
141 | - */ |
|
142 | - public function get_all_related( |
|
143 | - $model_object_or_id, |
|
144 | - $query_params = array(), |
|
145 | - $values_already_prepared_by_model_object = false |
|
146 | - ) { |
|
147 | - if ($values_already_prepared_by_model_object !== false) { |
|
148 | - EE_Error::doing_it_wrong( |
|
149 | - 'EE_Model_Relation_Base::get_all_related', |
|
150 | - esc_html__('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
151 | - '4.8.1' |
|
152 | - ); |
|
153 | - } |
|
154 | - |
|
155 | - // if this is an autosave then we're going to get things differently |
|
156 | - if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
|
157 | - return $this->_do_autosave_get_all($model_object_or_id, $query_params); |
|
158 | - } |
|
159 | - |
|
160 | - return parent::get_all_related($model_object_or_id, $query_params); |
|
161 | - } |
|
162 | - |
|
163 | - |
|
164 | - /** |
|
165 | - * If we're in the midst of an autosave then we're going to do things a bit differently than the usual |
|
166 | - * get_all_related (commenting within). For description of params see the get_all_related() comments |
|
167 | - * |
|
168 | - * @access protected |
|
169 | - * @param $model_object_or_id |
|
170 | - * @param $query_params |
|
171 | - * @param bool $deprecated |
|
172 | - * @return \EE_Base_Class[] |
|
173 | - * @throws \EE_Error |
|
174 | - */ |
|
175 | - protected function _do_autosave_get_all($model_object_or_id, $query_params, $deprecated = false) |
|
176 | - { |
|
177 | - |
|
178 | - // first we check if the post_id for the incoming query is for an autosave. If it isn't that's what we want! |
|
179 | - $model_object_id = $this->_get_model_object_id($model_object_or_id); |
|
180 | - |
|
181 | - $autosave = wp_get_post_autosave($model_object_id); |
|
182 | - $id_to_use = $autosave ? $autosave->ID : $model_object_id; |
|
183 | - |
|
184 | - $autosave_relations = parent::get_all_related($id_to_use, $query_params); |
|
185 | - $parent_ids = $parents = array(); |
|
186 | - $return_objs = array(); |
|
187 | - |
|
188 | - // k this is where things differ because NOW what we're going to do is get the PARENTS for the get all related (and we'll also start setting up the return_objs array containing related that DON'T have parent ids, for those that DON'T have parents to merge with our returned objects); |
|
189 | - foreach ($autosave_relations as $a_r) { |
|
190 | - $pid = $a_r->parent(); |
|
191 | - if (! empty($pid)) { |
|
192 | - $parent_ids[] = $pid; |
|
193 | - } else { |
|
194 | - $return_objs[] = $a_r; |
|
195 | - } |
|
196 | - } |
|
197 | - |
|
198 | - // we have to make sure we also include the ORIGINAL values |
|
199 | - $originals = parent::get_all_related($model_object_or_id, $query_params); |
|
200 | - |
|
201 | - // merge $originals with $return_objs |
|
202 | - if ($originals) { |
|
203 | - $return_objs = array_merge($originals, $return_objs); |
|
204 | - } |
|
205 | - |
|
206 | - // now we setup the query to get all the parents |
|
207 | - if (! empty($parent_ids)) { |
|
208 | - $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() . "." . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
209 | - $query_param[0][ $query_param_where_this_model_pk ] = array('IN', $parent_ids); |
|
210 | - $parents = $this->get_other_model()->get_all($query_params); |
|
211 | - } |
|
212 | - |
|
213 | - // var_dump($parents); |
|
214 | - |
|
215 | - |
|
216 | - // now merge parents with our current $return_objs and send back |
|
217 | - return array_merge($parents, $return_objs); |
|
218 | - } |
|
219 | - |
|
220 | - |
|
221 | - /** |
|
222 | - * Basically this method gets called to verify if the incoming object needs to be manipulated somewhat because it |
|
223 | - * is a revision save. If so, then we change things before sending back. We also do verifications when this IS |
|
224 | - * NOT an revision because we always need to make sure that the autosave/revision has parent recorded (which is |
|
225 | - * sometime delayed if the object is created/saved first by the autosave) |
|
226 | - * |
|
227 | - * @param EE_Base_Class $this_obj |
|
228 | - * @param EE_Base_Class $other_obj |
|
229 | - * @param boolean $remove_relation Indicates whether we're doing a remove_relation or add_relation. |
|
230 | - * @return EE_Base_Class. ($other_obj); |
|
231 | - * @throws \EE_Error |
|
232 | - */ |
|
233 | - protected function _check_for_revision($this_obj, $other_obj, $remove_relation = false) |
|
234 | - { |
|
235 | - $pk_on_related_model = $this->get_other_model()->get_primary_key_field()->get_name(); |
|
236 | - // now we need to determine if we're in a WP revision save cause if we are we need to do some special handling |
|
237 | - if ($this_obj->post_type() === 'revision') { |
|
238 | - // first if $other_obj fk = this_obj pk then we know that this is a pk object, let's make sure there is a matching set for the autosave if there is then we save over it, if there isn't then we need to create a new one. |
|
239 | - $parent_evt_id = $this_obj->parent(); |
|
240 | - /*var_dump($parent_evt_id); |
|
13 | + /** |
|
14 | + * The Foreign key on the model that acts as the PRIMARY KEY used in special autosave handling where we query for |
|
15 | + * autosaves (or the Foreign key on other models in relations pointing to this models primary key which is this |
|
16 | + * value). The _primary_cpt_field is what is equivalent to the post_id field on a cpt join. |
|
17 | + * |
|
18 | + * @var string |
|
19 | + */ |
|
20 | + private $_primary_cpt_field; |
|
21 | + |
|
22 | + |
|
23 | + /** |
|
24 | + * This is what field serves as the "parent" column that is linked with whatever the main model's calling this |
|
25 | + * relation has as a primary key. In other words EEM_Event has 'Datetime' => new |
|
26 | + * EE_Has_Many_Revision_Relation('EVT_ID', 'DTT_parent'). That means that in the EEM_Datetime model the |
|
27 | + * 'DTT_Parent' field is related to the 'DTT_ID' primary key field (in the same model) because 'DTT_ID' is the |
|
28 | + * primary key in the other model (EEM_Datetime). |
|
29 | + * |
|
30 | + * @var string |
|
31 | + */ |
|
32 | + private $_parent_pk_relation_field; |
|
33 | + |
|
34 | + |
|
35 | + /** |
|
36 | + * Object representing the relationship between two models. Has_Many_Relations are where the OTHER model has the |
|
37 | + * foreign key this model. IE, there can be many other model objects related to one of this model's objects (but |
|
38 | + * NOT through a JOIN table, which is the case for EE_HABTM_Relations). This knows how to join the models, get |
|
39 | + * related models across the relation, and add-and-remove the relationships. |
|
40 | + * |
|
41 | + * @param string $primary_cpt_field See property description for details |
|
42 | + * @param string $parent_pk_relation_field This is the field that is "connected" to the $primary_cpt_field. |
|
43 | + * See property desc for details. |
|
44 | + * @param boolean $block_deletes For this type of relation, we block by default. If there are |
|
45 | + * related models across this relation, block (prevent and add an |
|
46 | + * error) the deletion of this model |
|
47 | + * @param string $blocking_delete_error_message a customized error message on blocking deletes instead of the |
|
48 | + * default |
|
49 | + */ |
|
50 | + public function __construct( |
|
51 | + $primary_cpt_field, |
|
52 | + $parent_pk_relation_field, |
|
53 | + $block_deletes = true, |
|
54 | + $blocking_delete_error_message = null |
|
55 | + ) { |
|
56 | + $this->_primary_cpt_field = $primary_cpt_field; |
|
57 | + $this->_parent_pk_relation_field = $parent_pk_relation_field; |
|
58 | + parent::__construct($block_deletes, $blocking_delete_error_message); |
|
59 | + } |
|
60 | + |
|
61 | + |
|
62 | + /** |
|
63 | + * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
64 | + * you like. |
|
65 | + * |
|
66 | + * @param EE_Base_Class|int $this_obj_or_id |
|
67 | + * @param EE_Base_Class|int $other_obj_or_id |
|
68 | + * @param array $extra_join_model_fields_n_values |
|
69 | + * @return \EE_Base_Class |
|
70 | + * @throws \EE_Error |
|
71 | + */ |
|
72 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
73 | + { |
|
74 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
75 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
76 | + |
|
77 | + // handle possible revisions |
|
78 | + $other_model_obj = $this->_check_for_revision($this_model_obj, $other_model_obj); |
|
79 | + |
|
80 | + // if is array, then we've already done the add_relation so let's get out |
|
81 | + if (is_array($other_model_obj)) { |
|
82 | + return $other_model_obj[0]; |
|
83 | + } |
|
84 | + // find the field on the other model which is a foreign key to this model |
|
85 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
86 | + // set that field on the other model to this model's ID |
|
87 | + $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
88 | + $other_model_obj->save(); |
|
89 | + return $other_model_obj; |
|
90 | + } |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * Sets the other model object's foreign key to its default, instead of pointing to this model object |
|
95 | + * |
|
96 | + * @param EE_Base_Class|int $this_obj_or_id |
|
97 | + * @param EE_Base_Class|int $other_obj_or_id |
|
98 | + * @param array $where_query |
|
99 | + * @return \EE_Base_Class |
|
100 | + * @throws \EE_Error |
|
101 | + */ |
|
102 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
103 | + { |
|
104 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id); |
|
105 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id); |
|
106 | + // handle possible revisions |
|
107 | + $other_model_obj = $this->_check_for_revision($this_model_obj, $other_model_obj, true); |
|
108 | + |
|
109 | + |
|
110 | + // if is array, then we've already done the add_relation so let's get out |
|
111 | + if (is_array($other_model_obj)) { |
|
112 | + return $other_model_obj[0]; |
|
113 | + } |
|
114 | + |
|
115 | + // find the field on the other model which is a foreign key to this model |
|
116 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
117 | + |
|
118 | + |
|
119 | + // set that field on the other model to this model's ID |
|
120 | + if ($this->_blocking_delete) { |
|
121 | + $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
122 | + $other_model_obj->save(); |
|
123 | + } else { |
|
124 | + $other_model_obj->delete(); |
|
125 | + $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
126 | + return $other_model_obj; |
|
127 | + } |
|
128 | + return $other_model_obj; |
|
129 | + } |
|
130 | + |
|
131 | + |
|
132 | + /** |
|
133 | + * This is identical to EE_Model_Relation->get_all_related() except we're going handle special autosave conditions |
|
134 | + * in here. |
|
135 | + * |
|
136 | + * @param EE_Base_Class|int $model_object_or_id |
|
137 | + * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
|
138 | + * @param boolean $values_already_prepared_by_model_object @deprecated since 4.8.1 |
|
139 | + * @return EE_Base_Class[] |
|
140 | + * @throws \EE_Error |
|
141 | + */ |
|
142 | + public function get_all_related( |
|
143 | + $model_object_or_id, |
|
144 | + $query_params = array(), |
|
145 | + $values_already_prepared_by_model_object = false |
|
146 | + ) { |
|
147 | + if ($values_already_prepared_by_model_object !== false) { |
|
148 | + EE_Error::doing_it_wrong( |
|
149 | + 'EE_Model_Relation_Base::get_all_related', |
|
150 | + esc_html__('The argument $values_already_prepared_by_model_object is no longer used.', 'event_espresso'), |
|
151 | + '4.8.1' |
|
152 | + ); |
|
153 | + } |
|
154 | + |
|
155 | + // if this is an autosave then we're going to get things differently |
|
156 | + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
|
157 | + return $this->_do_autosave_get_all($model_object_or_id, $query_params); |
|
158 | + } |
|
159 | + |
|
160 | + return parent::get_all_related($model_object_or_id, $query_params); |
|
161 | + } |
|
162 | + |
|
163 | + |
|
164 | + /** |
|
165 | + * If we're in the midst of an autosave then we're going to do things a bit differently than the usual |
|
166 | + * get_all_related (commenting within). For description of params see the get_all_related() comments |
|
167 | + * |
|
168 | + * @access protected |
|
169 | + * @param $model_object_or_id |
|
170 | + * @param $query_params |
|
171 | + * @param bool $deprecated |
|
172 | + * @return \EE_Base_Class[] |
|
173 | + * @throws \EE_Error |
|
174 | + */ |
|
175 | + protected function _do_autosave_get_all($model_object_or_id, $query_params, $deprecated = false) |
|
176 | + { |
|
177 | + |
|
178 | + // first we check if the post_id for the incoming query is for an autosave. If it isn't that's what we want! |
|
179 | + $model_object_id = $this->_get_model_object_id($model_object_or_id); |
|
180 | + |
|
181 | + $autosave = wp_get_post_autosave($model_object_id); |
|
182 | + $id_to_use = $autosave ? $autosave->ID : $model_object_id; |
|
183 | + |
|
184 | + $autosave_relations = parent::get_all_related($id_to_use, $query_params); |
|
185 | + $parent_ids = $parents = array(); |
|
186 | + $return_objs = array(); |
|
187 | + |
|
188 | + // k this is where things differ because NOW what we're going to do is get the PARENTS for the get all related (and we'll also start setting up the return_objs array containing related that DON'T have parent ids, for those that DON'T have parents to merge with our returned objects); |
|
189 | + foreach ($autosave_relations as $a_r) { |
|
190 | + $pid = $a_r->parent(); |
|
191 | + if (! empty($pid)) { |
|
192 | + $parent_ids[] = $pid; |
|
193 | + } else { |
|
194 | + $return_objs[] = $a_r; |
|
195 | + } |
|
196 | + } |
|
197 | + |
|
198 | + // we have to make sure we also include the ORIGINAL values |
|
199 | + $originals = parent::get_all_related($model_object_or_id, $query_params); |
|
200 | + |
|
201 | + // merge $originals with $return_objs |
|
202 | + if ($originals) { |
|
203 | + $return_objs = array_merge($originals, $return_objs); |
|
204 | + } |
|
205 | + |
|
206 | + // now we setup the query to get all the parents |
|
207 | + if (! empty($parent_ids)) { |
|
208 | + $query_param_where_this_model_pk = $this->get_this_model()->get_this_model_name() . "." . $this->get_this_model()->get_primary_key_field()->get_name(); |
|
209 | + $query_param[0][ $query_param_where_this_model_pk ] = array('IN', $parent_ids); |
|
210 | + $parents = $this->get_other_model()->get_all($query_params); |
|
211 | + } |
|
212 | + |
|
213 | + // var_dump($parents); |
|
214 | + |
|
215 | + |
|
216 | + // now merge parents with our current $return_objs and send back |
|
217 | + return array_merge($parents, $return_objs); |
|
218 | + } |
|
219 | + |
|
220 | + |
|
221 | + /** |
|
222 | + * Basically this method gets called to verify if the incoming object needs to be manipulated somewhat because it |
|
223 | + * is a revision save. If so, then we change things before sending back. We also do verifications when this IS |
|
224 | + * NOT an revision because we always need to make sure that the autosave/revision has parent recorded (which is |
|
225 | + * sometime delayed if the object is created/saved first by the autosave) |
|
226 | + * |
|
227 | + * @param EE_Base_Class $this_obj |
|
228 | + * @param EE_Base_Class $other_obj |
|
229 | + * @param boolean $remove_relation Indicates whether we're doing a remove_relation or add_relation. |
|
230 | + * @return EE_Base_Class. ($other_obj); |
|
231 | + * @throws \EE_Error |
|
232 | + */ |
|
233 | + protected function _check_for_revision($this_obj, $other_obj, $remove_relation = false) |
|
234 | + { |
|
235 | + $pk_on_related_model = $this->get_other_model()->get_primary_key_field()->get_name(); |
|
236 | + // now we need to determine if we're in a WP revision save cause if we are we need to do some special handling |
|
237 | + if ($this_obj->post_type() === 'revision') { |
|
238 | + // first if $other_obj fk = this_obj pk then we know that this is a pk object, let's make sure there is a matching set for the autosave if there is then we save over it, if there isn't then we need to create a new one. |
|
239 | + $parent_evt_id = $this_obj->parent(); |
|
240 | + /*var_dump($parent_evt_id); |
|
241 | 241 | var_dump($this_obj); |
242 | 242 | var_dump($other_obj);/**/ |
243 | 243 | |
244 | - if (! empty($parent_evt_id) && $parent_evt_id == $other_obj->get($this->_primary_cpt_field)) { |
|
245 | - // let's do query on this objects model to see if the incoming pk value on the obj matches any parents in this objects table. |
|
246 | - $has_parent_obj = $this->get_other_model()->get_one(array( |
|
247 | - array( |
|
248 | - $this->_parent_pk_relation_field => $other_obj->ID(), |
|
249 | - $this->_primary_cpt_field => $this_obj->ID(), |
|
250 | - ), |
|
251 | - )); |
|
252 | - |
|
253 | - if ($has_parent_obj) { |
|
254 | - // this makes sure the update on the current obj happens to the revision's row NOT the parent row. |
|
255 | - |
|
256 | - $other_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
257 | - $other_obj->set($pk_on_related_model, $has_parent_obj->ID()); |
|
258 | - $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
259 | - |
|
260 | - if (! $remove_relation) { |
|
261 | - $other_obj->save(); |
|
262 | - return array($other_obj); |
|
263 | - } elseif ($remove_relation && ! $this->_blocking_delete) { |
|
264 | - $other_obj->delete(); |
|
265 | - $other_obj->set($this->_parent_pk_relation_field, null, true); |
|
266 | - return array($other_obj); |
|
267 | - } |
|
268 | - } else { |
|
269 | - $other_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
270 | - $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
271 | - $other_obj->set( |
|
272 | - $pk_on_related_model, |
|
273 | - null, |
|
274 | - true |
|
275 | - ); // ensure we create a new row for the autosave with parent id the same as the incoming ID. |
|
276 | - $other_obj->save(); // make sure we insert. |
|
277 | - return array($other_obj); |
|
278 | - } |
|
279 | - } |
|
280 | - |
|
281 | - // var_dump('what makes it here'); |
|
282 | - // var_dump($other_obj); |
|
283 | - // the next possible condition is that the incoming other_model obj has a NULL pk which means it just gets saved (which in turn creates it) |
|
284 | - |
|
285 | - // the last possible condition on a revision is that the incoming other_model object has a fk that == $this_obj pk which means we just return the $other obj and let it save as normal so we see the return at the bottom of this method. |
|
286 | - } else { |
|
287 | - // we only need to do the below IF this is not a remove relation |
|
288 | - if (! $remove_relation) { |
|
289 | - // okay this is is a normal update/save/remove so, let's make sure the other object is not a revision of the current object. |
|
290 | - // the other object will likely NOT have the correct fk on it (which is the primary_cpt_field_mame) so we must retrieve from the db to get that first. |
|
291 | - $existing_other_obj = $this->get_other_model()->get_one_by_ID($other_obj->ID()); |
|
292 | - $potential_revision_id = is_object($existing_other_obj) ? $existing_other_obj->get($this->_primary_cpt_field) : null; |
|
293 | - |
|
294 | - if ($parent_this_obj_id = wp_is_post_revision($potential_revision_id)) { |
|
295 | - // yes the OTHER object is linked to the revision of the parent, not the parent itself. That means we need to make the other_object an attachment of this_obj and then duplicate other_obj for the revision. |
|
296 | - $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
297 | - $other_obj->save(); |
|
298 | - |
|
299 | - // now create a new other_obj and fill with details from existing object |
|
300 | - $new_obj = $other_obj; |
|
301 | - $new_obj->set($this->_primary_cpt_field, $potential_revision_id); |
|
302 | - $new_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
303 | - $new_obj->set($pk_on_related_model, null); |
|
304 | - $new_obj->save(); |
|
305 | - return array($new_obj); |
|
306 | - } |
|
307 | - } |
|
308 | - } |
|
309 | - |
|
310 | - return $other_obj; |
|
311 | - } |
|
244 | + if (! empty($parent_evt_id) && $parent_evt_id == $other_obj->get($this->_primary_cpt_field)) { |
|
245 | + // let's do query on this objects model to see if the incoming pk value on the obj matches any parents in this objects table. |
|
246 | + $has_parent_obj = $this->get_other_model()->get_one(array( |
|
247 | + array( |
|
248 | + $this->_parent_pk_relation_field => $other_obj->ID(), |
|
249 | + $this->_primary_cpt_field => $this_obj->ID(), |
|
250 | + ), |
|
251 | + )); |
|
252 | + |
|
253 | + if ($has_parent_obj) { |
|
254 | + // this makes sure the update on the current obj happens to the revision's row NOT the parent row. |
|
255 | + |
|
256 | + $other_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
257 | + $other_obj->set($pk_on_related_model, $has_parent_obj->ID()); |
|
258 | + $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
259 | + |
|
260 | + if (! $remove_relation) { |
|
261 | + $other_obj->save(); |
|
262 | + return array($other_obj); |
|
263 | + } elseif ($remove_relation && ! $this->_blocking_delete) { |
|
264 | + $other_obj->delete(); |
|
265 | + $other_obj->set($this->_parent_pk_relation_field, null, true); |
|
266 | + return array($other_obj); |
|
267 | + } |
|
268 | + } else { |
|
269 | + $other_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
270 | + $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
271 | + $other_obj->set( |
|
272 | + $pk_on_related_model, |
|
273 | + null, |
|
274 | + true |
|
275 | + ); // ensure we create a new row for the autosave with parent id the same as the incoming ID. |
|
276 | + $other_obj->save(); // make sure we insert. |
|
277 | + return array($other_obj); |
|
278 | + } |
|
279 | + } |
|
280 | + |
|
281 | + // var_dump('what makes it here'); |
|
282 | + // var_dump($other_obj); |
|
283 | + // the next possible condition is that the incoming other_model obj has a NULL pk which means it just gets saved (which in turn creates it) |
|
284 | + |
|
285 | + // the last possible condition on a revision is that the incoming other_model object has a fk that == $this_obj pk which means we just return the $other obj and let it save as normal so we see the return at the bottom of this method. |
|
286 | + } else { |
|
287 | + // we only need to do the below IF this is not a remove relation |
|
288 | + if (! $remove_relation) { |
|
289 | + // okay this is is a normal update/save/remove so, let's make sure the other object is not a revision of the current object. |
|
290 | + // the other object will likely NOT have the correct fk on it (which is the primary_cpt_field_mame) so we must retrieve from the db to get that first. |
|
291 | + $existing_other_obj = $this->get_other_model()->get_one_by_ID($other_obj->ID()); |
|
292 | + $potential_revision_id = is_object($existing_other_obj) ? $existing_other_obj->get($this->_primary_cpt_field) : null; |
|
293 | + |
|
294 | + if ($parent_this_obj_id = wp_is_post_revision($potential_revision_id)) { |
|
295 | + // yes the OTHER object is linked to the revision of the parent, not the parent itself. That means we need to make the other_object an attachment of this_obj and then duplicate other_obj for the revision. |
|
296 | + $other_obj->set($this->_primary_cpt_field, $this_obj->ID()); |
|
297 | + $other_obj->save(); |
|
298 | + |
|
299 | + // now create a new other_obj and fill with details from existing object |
|
300 | + $new_obj = $other_obj; |
|
301 | + $new_obj->set($this->_primary_cpt_field, $potential_revision_id); |
|
302 | + $new_obj->set($this->_parent_pk_relation_field, $other_obj->ID()); |
|
303 | + $new_obj->set($pk_on_related_model, null); |
|
304 | + $new_obj->save(); |
|
305 | + return array($new_obj); |
|
306 | + } |
|
307 | + } |
|
308 | + } |
|
309 | + |
|
310 | + return $other_obj; |
|
311 | + } |
|
312 | 312 | } |
@@ -32,11 +32,11 @@ discard block |
||
32 | 32 | $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
33 | 33 | $model_relation_chain, |
34 | 34 | $this->get_this_model()->get_this_model_name() |
35 | - ) . $this_table_pk_field->get_table_alias(); |
|
36 | - $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
35 | + ).$this_table_pk_field->get_table_alias(); |
|
36 | + $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
37 | 37 | $model_relation_chain, |
38 | 38 | $this->get_other_model()->get_this_model_name() |
39 | - ) . $other_table_fk_field->get_table_alias(); |
|
39 | + ).$other_table_fk_field->get_table_alias(); |
|
40 | 40 | $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
41 | 41 | $field_with_model_name = $this->get_other_model()->get_field_containing_related_model_name(); |
42 | 42 | |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | $other_table_fk_field->get_table_column(), |
47 | 47 | $pk_table_alias, |
48 | 48 | $this_table_pk_field->get_table_column(), |
49 | - $fk_table_alias . '.' . $field_with_model_name->get_table_column() . "='" . $this->get_this_model()->get_this_model_name() . "'" |
|
49 | + $fk_table_alias.'.'.$field_with_model_name->get_table_column()."='".$this->get_this_model()->get_this_model_name()."'" |
|
50 | 50 | ) |
51 | 51 | . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
52 | 52 | } |
@@ -14,94 +14,94 @@ |
||
14 | 14 | */ |
15 | 15 | class EE_Has_Many_Any_Relation extends EE_Has_Many_Relation |
16 | 16 | { |
17 | - /** |
|
18 | - * Gets the SQL string for performing the join between this model and the other model. |
|
19 | - * |
|
20 | - * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
21 | - * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
22 | - * other_model_primary_table.fk" etc |
|
23 | - * @throws \EE_Error |
|
24 | - */ |
|
25 | - public function get_join_statement($model_relation_chain) |
|
26 | - { |
|
27 | - // create the sql string like |
|
28 | - // LEFT JOIN other_table AS table_alias ON this_table_alias.pk = other_table_alias.fk extra_join_conditions |
|
29 | - $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
30 | - $other_table_fk_field = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
31 | - $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
32 | - $model_relation_chain, |
|
33 | - $this->get_this_model()->get_this_model_name() |
|
34 | - ) . $this_table_pk_field->get_table_alias(); |
|
35 | - $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
36 | - $model_relation_chain, |
|
37 | - $this->get_other_model()->get_this_model_name() |
|
38 | - ) . $other_table_fk_field->get_table_alias(); |
|
39 | - $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
|
40 | - $field_with_model_name = $this->get_other_model()->get_field_containing_related_model_name(); |
|
17 | + /** |
|
18 | + * Gets the SQL string for performing the join between this model and the other model. |
|
19 | + * |
|
20 | + * @param string $model_relation_chain like 'Event.Event_Venue.Venue' |
|
21 | + * @return string of SQL, eg "LEFT JOIN table_name AS table_alias ON this_model_primary_table.pk = |
|
22 | + * other_model_primary_table.fk" etc |
|
23 | + * @throws \EE_Error |
|
24 | + */ |
|
25 | + public function get_join_statement($model_relation_chain) |
|
26 | + { |
|
27 | + // create the sql string like |
|
28 | + // LEFT JOIN other_table AS table_alias ON this_table_alias.pk = other_table_alias.fk extra_join_conditions |
|
29 | + $this_table_pk_field = $this->get_this_model()->get_primary_key_field(); |
|
30 | + $other_table_fk_field = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
31 | + $pk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
32 | + $model_relation_chain, |
|
33 | + $this->get_this_model()->get_this_model_name() |
|
34 | + ) . $this_table_pk_field->get_table_alias(); |
|
35 | + $fk_table_alias = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix( |
|
36 | + $model_relation_chain, |
|
37 | + $this->get_other_model()->get_this_model_name() |
|
38 | + ) . $other_table_fk_field->get_table_alias(); |
|
39 | + $fk_table = $this->get_other_model()->get_table_for_alias($fk_table_alias); |
|
40 | + $field_with_model_name = $this->get_other_model()->get_field_containing_related_model_name(); |
|
41 | 41 | |
42 | - return $this->_left_join( |
|
43 | - $fk_table, |
|
44 | - $fk_table_alias, |
|
45 | - $other_table_fk_field->get_table_column(), |
|
46 | - $pk_table_alias, |
|
47 | - $this_table_pk_field->get_table_column(), |
|
48 | - $fk_table_alias . '.' . $field_with_model_name->get_table_column() . "='" . $this->get_this_model()->get_this_model_name() . "'" |
|
49 | - ) |
|
50 | - . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
|
51 | - } |
|
42 | + return $this->_left_join( |
|
43 | + $fk_table, |
|
44 | + $fk_table_alias, |
|
45 | + $other_table_fk_field->get_table_column(), |
|
46 | + $pk_table_alias, |
|
47 | + $this_table_pk_field->get_table_column(), |
|
48 | + $fk_table_alias . '.' . $field_with_model_name->get_table_column() . "='" . $this->get_this_model()->get_this_model_name() . "'" |
|
49 | + ) |
|
50 | + . $this->get_other_model()->_construct_internal_join_to_table_with_alias($fk_table_alias); |
|
51 | + } |
|
52 | 52 | |
53 | 53 | |
54 | - /** |
|
55 | - * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
56 | - * you like. |
|
57 | - * |
|
58 | - * @param EE_Base_Class|int $this_obj_or_id |
|
59 | - * @param EE_Base_Class|int $other_obj_or_id |
|
60 | - * @param array $extra_join_model_fields_n_values |
|
61 | - * @return \EE_Base_Class |
|
62 | - * @throws \EE_Error |
|
63 | - */ |
|
64 | - public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
65 | - { |
|
66 | - $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
67 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
54 | + /** |
|
55 | + * Sets the other model object's foreign key to this model object's primary key. Feel free to do this manually if |
|
56 | + * you like. |
|
57 | + * |
|
58 | + * @param EE_Base_Class|int $this_obj_or_id |
|
59 | + * @param EE_Base_Class|int $other_obj_or_id |
|
60 | + * @param array $extra_join_model_fields_n_values |
|
61 | + * @return \EE_Base_Class |
|
62 | + * @throws \EE_Error |
|
63 | + */ |
|
64 | + public function add_relation_to($this_obj_or_id, $other_obj_or_id, $extra_join_model_fields_n_values = array()) |
|
65 | + { |
|
66 | + $this_model_obj = $this->get_this_model()->ensure_is_obj($this_obj_or_id, true); |
|
67 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
68 | 68 | |
69 | - // find the field on the other model which is a foreign key to this model |
|
70 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
71 | - // set that field on the other model to this model's ID |
|
72 | - $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
73 | - $other_model_obj->set( |
|
74 | - $this->get_other_model()->get_field_containing_related_model_name()->get_name(), |
|
75 | - $this->get_this_model()->get_this_model_name() |
|
76 | - ); |
|
77 | - $other_model_obj->save(); |
|
78 | - return $other_model_obj; |
|
79 | - } |
|
69 | + // find the field on the other model which is a foreign key to this model |
|
70 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
71 | + // set that field on the other model to this model's ID |
|
72 | + $other_model_obj->set($fk_field_on_other_model->get_name(), $this_model_obj->ID()); |
|
73 | + $other_model_obj->set( |
|
74 | + $this->get_other_model()->get_field_containing_related_model_name()->get_name(), |
|
75 | + $this->get_this_model()->get_this_model_name() |
|
76 | + ); |
|
77 | + $other_model_obj->save(); |
|
78 | + return $other_model_obj; |
|
79 | + } |
|
80 | 80 | |
81 | 81 | |
82 | - /** |
|
83 | - * Sets the other model object's foreign key to its default, instead of pointing to this model object. |
|
84 | - * If $other_obj_or_id doesn't have any other relations, this function is essentially orphaning it |
|
85 | - * |
|
86 | - * @param EE_Base_Class|int $this_obj_or_id |
|
87 | - * @param EE_Base_Class|int $other_obj_or_id |
|
88 | - * @param array $where_query |
|
89 | - * @return \EE_Base_Class |
|
90 | - * @throws \EE_Error |
|
91 | - */ |
|
92 | - public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
93 | - { |
|
94 | - $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
95 | - // find the field on the other model which is a foreign key to this model |
|
96 | - $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
97 | - // set that field on the other model to this model's ID |
|
98 | - $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
99 | - $other_model_obj->set( |
|
100 | - $this->get_other_model()->get_field_containing_related_model_name()->get_name(), |
|
101 | - null, |
|
102 | - true |
|
103 | - ); |
|
104 | - $other_model_obj->save(); |
|
105 | - return $other_model_obj; |
|
106 | - } |
|
82 | + /** |
|
83 | + * Sets the other model object's foreign key to its default, instead of pointing to this model object. |
|
84 | + * If $other_obj_or_id doesn't have any other relations, this function is essentially orphaning it |
|
85 | + * |
|
86 | + * @param EE_Base_Class|int $this_obj_or_id |
|
87 | + * @param EE_Base_Class|int $other_obj_or_id |
|
88 | + * @param array $where_query |
|
89 | + * @return \EE_Base_Class |
|
90 | + * @throws \EE_Error |
|
91 | + */ |
|
92 | + public function remove_relation_to($this_obj_or_id, $other_obj_or_id, $where_query = array()) |
|
93 | + { |
|
94 | + $other_model_obj = $this->get_other_model()->ensure_is_obj($other_obj_or_id, true); |
|
95 | + // find the field on the other model which is a foreign key to this model |
|
96 | + $fk_field_on_other_model = $this->get_other_model()->get_foreign_key_to($this->get_this_model()->get_this_model_name()); |
|
97 | + // set that field on the other model to this model's ID |
|
98 | + $other_model_obj->set($fk_field_on_other_model->get_name(), null, true); |
|
99 | + $other_model_obj->set( |
|
100 | + $this->get_other_model()->get_field_containing_related_model_name()->get_name(), |
|
101 | + null, |
|
102 | + true |
|
103 | + ); |
|
104 | + $other_model_obj->save(); |
|
105 | + return $other_model_obj; |
|
106 | + } |
|
107 | 107 | } |
@@ -6,5 +6,5 @@ |
||
6 | 6 | */ |
7 | 7 | class EE_Unique_Index extends EE_Index |
8 | 8 | { |
9 | - // yep, actually the same as index right now |
|
9 | + // yep, actually the same as index right now |
|
10 | 10 | } |