Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EEM_Base often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EEM_Base, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | abstract class EEM_Base extends EE_Base implements EventEspresso\core\interfaces\ResettableInterface |
||
29 | { |
||
30 | |||
31 | //admin posty |
||
32 | //basic -> grants access to mine -> if they don't have it, select none |
||
33 | //*_others -> grants access to others that aren't private, and all mine -> if they don't have it, select mine |
||
34 | //*_private -> grants full access -> if dont have it, select all mine and others' non-private |
||
35 | //*_published -> grants access to published -> if they dont have it, select non-published |
||
36 | //*_global/default/system -> grants access to global items -> if they don't have it, select non-global |
||
37 | //publish_{thing} -> can change status TO publish; SPECIAL CASE |
||
38 | //frontend posty |
||
39 | //by default has access to published |
||
40 | //basic -> grants access to mine that aren't published, and all published |
||
41 | //*_others ->grants access to others that aren't private, all mine |
||
42 | //*_private -> grants full access |
||
43 | //frontend non-posty |
||
44 | //like admin posty |
||
45 | //category-y |
||
46 | //assign -> grants access to join-table |
||
47 | //(delete, edit) |
||
48 | //payment-method-y |
||
49 | //for each registered payment method, |
||
50 | //ee_payment_method_{pmttype} -> if they don't have it, select all where they aren't of that type |
||
51 | /** |
||
52 | * Flag to indicate whether the values provided to EEM_Base have already been prepared |
||
53 | * by the model object or not (ie, the model object has used the field's _prepare_for_set function on the values). |
||
54 | * They almost always WILL NOT, but it's not necessarily a requirement. |
||
55 | * For example, if you want to run EEM_Event::instance()->get_all(array(array('EVT_ID'=>$_GET['event_id']))); |
||
56 | * |
||
57 | * @var boolean |
||
58 | */ |
||
59 | private $_values_already_prepared_by_model_object = 0; |
||
60 | |||
61 | /** |
||
62 | * when $_values_already_prepared_by_model_object equals this, we assume |
||
63 | * the data is just like form input that needs to have the model fields' |
||
64 | * prepare_for_set and prepare_for_use_in_db called on it |
||
65 | */ |
||
66 | const not_prepared_by_model_object = 0; |
||
67 | |||
68 | /** |
||
69 | * when $_values_already_prepared_by_model_object equals this, we |
||
70 | * assume this value is coming from a model object and doesn't need to have |
||
71 | * prepare_for_set called on it, just prepare_for_use_in_db is used |
||
72 | */ |
||
73 | const prepared_by_model_object = 1; |
||
74 | |||
75 | /** |
||
76 | * when $_values_already_prepared_by_model_object equals this, we assume |
||
77 | * the values are already to be used in the database (ie no processing is done |
||
78 | * on them by the model's fields) |
||
79 | */ |
||
80 | const prepared_for_use_in_db = 2; |
||
81 | |||
82 | |||
83 | protected $singular_item = 'Item'; |
||
84 | |||
85 | protected $plural_item = 'Items'; |
||
86 | |||
87 | /** |
||
88 | * @type \EE_Table_Base[] $_tables array of EE_Table objects for defining which tables comprise this model. |
||
89 | */ |
||
90 | protected $_tables; |
||
91 | |||
92 | /** |
||
93 | * with two levels: top-level has array keys which are database table aliases (ie, keys in _tables) |
||
94 | * and the value is an array. Each of those sub-arrays have keys of field names (eg 'ATT_ID', which should also be |
||
95 | * variable names on the model objects (eg, EE_Attendee), and the keys should be children of EE_Model_Field |
||
96 | * |
||
97 | * @var \EE_Model_Field_Base[] $_fields |
||
98 | */ |
||
99 | protected $_fields; |
||
100 | |||
101 | /** |
||
102 | * array of different kinds of relations |
||
103 | * |
||
104 | * @var \EE_Model_Relation_Base[] $_model_relations |
||
105 | */ |
||
106 | protected $_model_relations; |
||
107 | |||
108 | /** |
||
109 | * @var \EE_Index[] $_indexes |
||
110 | */ |
||
111 | protected $_indexes = array(); |
||
112 | |||
113 | /** |
||
114 | * Default strategy for getting where conditions on this model. This strategy is used to get default |
||
115 | * where conditions which are added to get_all, update, and delete queries. They can be overridden |
||
116 | * by setting the same columns as used in these queries in the query yourself. |
||
117 | * |
||
118 | * @var EE_Default_Where_Conditions |
||
119 | */ |
||
120 | protected $_default_where_conditions_strategy; |
||
121 | |||
122 | /** |
||
123 | * Strategy for getting conditions on this model when 'default_where_conditions' equals 'minimum'. |
||
124 | * This is particularly useful when you want something between 'none' and 'default' |
||
125 | * |
||
126 | * @var EE_Default_Where_Conditions |
||
127 | */ |
||
128 | protected $_minimum_where_conditions_strategy; |
||
129 | |||
130 | /** |
||
131 | * String describing how to find the "owner" of this model's objects. |
||
132 | * When there is a foreign key on this model to the wp_users table, this isn't needed. |
||
133 | * But when there isn't, this indicates which related model, or transiently-related model, |
||
134 | * has the foreign key to the wp_users table. |
||
135 | * Eg, for EEM_Registration this would be 'Event' because registrations are directly |
||
136 | * related to events, and events have a foreign key to wp_users. |
||
137 | * On EEM_Transaction, this would be 'Transaction.Event' |
||
138 | * |
||
139 | * @var string |
||
140 | */ |
||
141 | protected $_model_chain_to_wp_user = ''; |
||
142 | |||
143 | /** |
||
144 | * This is a flag typically set by updates so that we don't load the where strategy on updates because updates |
||
145 | * don't need it (particularly CPT models) |
||
146 | * |
||
147 | * @var bool |
||
148 | */ |
||
149 | protected $_ignore_where_strategy = false; |
||
150 | |||
151 | /** |
||
152 | * String used in caps relating to this model. Eg, if the caps relating to this |
||
153 | * model are 'ee_edit_events', 'ee_read_events', etc, it would be 'events'. |
||
154 | * |
||
155 | * @var string. If null it hasn't been initialized yet. If false then we |
||
156 | * have indicated capabilities don't apply to this |
||
157 | */ |
||
158 | protected $_caps_slug = null; |
||
159 | |||
160 | /** |
||
161 | * 2d array where top-level keys are one of EEM_Base::valid_cap_contexts(), |
||
162 | * and next-level keys are capability names, and each's value is a |
||
163 | * EE_Default_Where_Condition. If the requester requests to apply caps to the query, |
||
164 | * they specify which context to use (ie, frontend, backend, edit or delete) |
||
165 | * and then each capability in the corresponding sub-array that they're missing |
||
166 | * adds the where conditions onto the query. |
||
167 | * |
||
168 | * @var array |
||
169 | */ |
||
170 | protected $_cap_restrictions = array( |
||
171 | self::caps_read => array(), |
||
172 | self::caps_read_admin => array(), |
||
173 | self::caps_edit => array(), |
||
174 | self::caps_delete => array(), |
||
175 | ); |
||
176 | |||
177 | /** |
||
178 | * Array defining which cap restriction generators to use to create default |
||
179 | * cap restrictions to put in EEM_Base::_cap_restrictions. |
||
180 | * Array-keys are one of EEM_Base::valid_cap_contexts(), and values are a child of |
||
181 | * EE_Restriction_Generator_Base. If you don't want any cap restrictions generated |
||
182 | * automatically set this to false (not just null). |
||
183 | * |
||
184 | * @var EE_Restriction_Generator_Base[] |
||
185 | */ |
||
186 | protected $_cap_restriction_generators = array(); |
||
187 | |||
188 | /** |
||
189 | * constants used to categorize capability restrictions on EEM_Base::_caps_restrictions |
||
190 | */ |
||
191 | const caps_read = 'read'; |
||
192 | |||
193 | const caps_read_admin = 'read_admin'; |
||
194 | |||
195 | const caps_edit = 'edit'; |
||
196 | |||
197 | const caps_delete = 'delete'; |
||
198 | |||
199 | /** |
||
200 | * Keys are all the cap contexts (ie constants EEM_Base::_caps_*) and values are their 'action' |
||
201 | * as how they'd be used in capability names. Eg EEM_Base::caps_read ('read_frontend') |
||
202 | * maps to 'read' because when looking for relevant permissions we're going to use |
||
203 | * 'read' in teh capabilities names like 'ee_read_events' etc. |
||
204 | * |
||
205 | * @var array |
||
206 | */ |
||
207 | protected $_cap_contexts_to_cap_action_map = array( |
||
208 | self::caps_read => 'read', |
||
209 | self::caps_read_admin => 'read', |
||
210 | self::caps_edit => 'edit', |
||
211 | self::caps_delete => 'delete', |
||
212 | ); |
||
213 | |||
214 | /** |
||
215 | * Timezone |
||
216 | * This gets set via the constructor so that we know what timezone incoming strings|timestamps are in when there |
||
217 | * are EE_Datetime_Fields in use. This can also be used before a get to set what timezone you want strings coming |
||
218 | * out of the created objects. NOT all EEM_Base child classes use this property but any that use a |
||
219 | * EE_Datetime_Field data type will have access to it. |
||
220 | * |
||
221 | * @var string |
||
222 | */ |
||
223 | protected $_timezone; |
||
224 | |||
225 | |||
226 | /** |
||
227 | * This holds the id of the blog currently making the query. Has no bearing on single site but is used for |
||
228 | * multisite. |
||
229 | * |
||
230 | * @var int |
||
231 | */ |
||
232 | protected static $_model_query_blog_id; |
||
233 | |||
234 | /** |
||
235 | * A copy of _fields, except the array keys are the model names pointed to by |
||
236 | * the field |
||
237 | * |
||
238 | * @var EE_Model_Field_Base[] |
||
239 | */ |
||
240 | private $_cache_foreign_key_to_fields = array(); |
||
241 | |||
242 | /** |
||
243 | * Cached list of all the fields on the model, indexed by their name |
||
244 | * |
||
245 | * @var EE_Model_Field_Base[] |
||
246 | */ |
||
247 | private $_cached_fields = null; |
||
248 | |||
249 | /** |
||
250 | * Cached list of all the fields on the model, except those that are |
||
251 | * marked as only pertinent to the database |
||
252 | * |
||
253 | * @var EE_Model_Field_Base[] |
||
254 | */ |
||
255 | private $_cached_fields_non_db_only = null; |
||
256 | |||
257 | /** |
||
258 | * A cached reference to the primary key for quick lookup |
||
259 | * |
||
260 | * @var EE_Model_Field_Base |
||
261 | */ |
||
262 | private $_primary_key_field = null; |
||
263 | |||
264 | /** |
||
265 | * Flag indicating whether this model has a primary key or not |
||
266 | * |
||
267 | * @var boolean |
||
268 | */ |
||
269 | protected $_has_primary_key_field = null; |
||
270 | |||
271 | /** |
||
272 | * Whether or not this model is based off a table in WP core only (CPTs should set |
||
273 | * this to FALSE, but if we were to make an EE_WP_Post model, it should set this to true). |
||
274 | * |
||
275 | * @var boolean |
||
276 | */ |
||
277 | protected $_wp_core_model = false; |
||
278 | |||
279 | /** |
||
280 | * List of valid operators that can be used for querying. |
||
281 | * The keys are all operators we'll accept, the values are the real SQL |
||
282 | * operators used |
||
283 | * |
||
284 | * @var array |
||
285 | */ |
||
286 | protected $_valid_operators = array( |
||
287 | '=' => '=', |
||
288 | '<=' => '<=', |
||
289 | '<' => '<', |
||
290 | '>=' => '>=', |
||
291 | '>' => '>', |
||
292 | '!=' => '!=', |
||
293 | 'LIKE' => 'LIKE', |
||
294 | 'like' => 'LIKE', |
||
295 | 'NOT_LIKE' => 'NOT LIKE', |
||
296 | 'not_like' => 'NOT LIKE', |
||
297 | 'NOT LIKE' => 'NOT LIKE', |
||
298 | 'not like' => 'NOT LIKE', |
||
299 | 'IN' => 'IN', |
||
300 | 'in' => 'IN', |
||
301 | 'NOT_IN' => 'NOT IN', |
||
302 | 'not_in' => 'NOT IN', |
||
303 | 'NOT IN' => 'NOT IN', |
||
304 | 'not in' => 'NOT IN', |
||
305 | 'between' => 'BETWEEN', |
||
306 | 'BETWEEN' => 'BETWEEN', |
||
307 | 'IS_NOT_NULL' => 'IS NOT NULL', |
||
308 | 'is_not_null' => 'IS NOT NULL', |
||
309 | 'IS NOT NULL' => 'IS NOT NULL', |
||
310 | 'is not null' => 'IS NOT NULL', |
||
311 | 'IS_NULL' => 'IS NULL', |
||
312 | 'is_null' => 'IS NULL', |
||
313 | 'IS NULL' => 'IS NULL', |
||
314 | 'is null' => 'IS NULL', |
||
315 | 'REGEXP' => 'REGEXP', |
||
316 | 'regexp' => 'REGEXP', |
||
317 | 'NOT_REGEXP' => 'NOT REGEXP', |
||
318 | 'not_regexp' => 'NOT REGEXP', |
||
319 | 'NOT REGEXP' => 'NOT REGEXP', |
||
320 | 'not regexp' => 'NOT REGEXP', |
||
321 | ); |
||
322 | |||
323 | /** |
||
324 | * operators that work like 'IN', accepting a comma-separated list of values inside brackets. Eg '(1,2,3)' |
||
325 | * |
||
326 | * @var array |
||
327 | */ |
||
328 | protected $_in_style_operators = array('IN', 'NOT IN'); |
||
329 | |||
330 | /** |
||
331 | * operators that work like 'BETWEEN'. Typically used for datetime calculations, i.e. "BETWEEN '12-1-2011' AND |
||
332 | * '12-31-2012'" |
||
333 | * |
||
334 | * @var array |
||
335 | */ |
||
336 | protected $_between_style_operators = array('BETWEEN'); |
||
337 | |||
338 | /** |
||
339 | * operators that are used for handling NUll and !NULL queries. Typically used for when checking if a row exists |
||
340 | * on a join table. |
||
341 | * |
||
342 | * @var array |
||
343 | */ |
||
344 | protected $_null_style_operators = array('IS NOT NULL', 'IS NULL'); |
||
345 | |||
346 | /** |
||
347 | * Allowed values for $query_params['order'] for ordering in queries |
||
348 | * |
||
349 | * @var array |
||
350 | */ |
||
351 | protected $_allowed_order_values = array('asc', 'desc', 'ASC', 'DESC'); |
||
352 | |||
353 | /** |
||
354 | * When these are keys in a WHERE or HAVING clause, they are handled much differently |
||
355 | * than regular field names. It is assumed that their values are an array of WHERE conditions |
||
356 | * |
||
357 | * @var array |
||
358 | */ |
||
359 | private $_logic_query_param_keys = array('not', 'and', 'or', 'NOT', 'AND', 'OR'); |
||
360 | |||
361 | /** |
||
362 | * Allowed keys in $query_params arrays passed into queries. Note that 0 is meant to always be a |
||
363 | * 'where', but 'where' clauses are so common that we thought we'd omit it |
||
364 | * |
||
365 | * @var array |
||
366 | */ |
||
367 | private $_allowed_query_params = array( |
||
368 | 0, |
||
369 | 'limit', |
||
370 | 'order_by', |
||
371 | 'group_by', |
||
372 | 'having', |
||
373 | 'force_join', |
||
374 | 'order', |
||
375 | 'on_join_limit', |
||
376 | 'default_where_conditions', |
||
377 | 'caps', |
||
378 | ); |
||
379 | |||
380 | /** |
||
381 | * All the data types that can be used in $wpdb->prepare statements. |
||
382 | * |
||
383 | * @var array |
||
384 | */ |
||
385 | private $_valid_wpdb_data_types = array('%d', '%s', '%f'); |
||
386 | |||
387 | /** |
||
388 | * EE_Registry Object |
||
389 | * |
||
390 | * @var object |
||
391 | * @access protected |
||
392 | */ |
||
393 | protected $EE = null; |
||
394 | |||
395 | |||
396 | /** |
||
397 | * Property which, when set, will have this model echo out the next X queries to the page for debugging. |
||
398 | * |
||
399 | * @var int |
||
400 | */ |
||
401 | protected $_show_next_x_db_queries = 0; |
||
402 | |||
403 | /** |
||
404 | * When using _get_all_wpdb_results, you can specify a custom selection. If you do so, |
||
405 | * it gets saved on this property so those selections can be used in WHERE, GROUP_BY, etc. |
||
406 | * |
||
407 | * @var array |
||
408 | */ |
||
409 | protected $_custom_selections = array(); |
||
410 | |||
411 | /** |
||
412 | * key => value Entity Map using array( EEM_Base::$_model_query_blog_id => array( ID => model object ) ) |
||
413 | * caches every model object we've fetched from the DB on this request |
||
414 | * |
||
415 | * @var array |
||
416 | */ |
||
417 | protected $_entity_map; |
||
418 | |||
419 | /** |
||
420 | * constant used to show EEM_Base has not yet verified the db on this http request |
||
421 | */ |
||
422 | const db_verified_none = 0; |
||
423 | |||
424 | /** |
||
425 | * constant used to show EEM_Base has verified the EE core db on this http request, |
||
426 | * but not the addons' dbs |
||
427 | */ |
||
428 | const db_verified_core = 1; |
||
429 | |||
430 | /** |
||
431 | * constant used to show EEM_Base has verified the addons' dbs (and implicitly |
||
432 | * the EE core db too) |
||
433 | */ |
||
434 | const db_verified_addons = 2; |
||
435 | |||
436 | /** |
||
437 | * indicates whether an EEM_Base child has already re-verified the DB |
||
438 | * is ok (we don't want to do it repetitively). Should be set to one the constants |
||
439 | * looking like EEM_Base::db_verified_* |
||
440 | * |
||
441 | * @var int - 0 = none, 1 = core, 2 = addons |
||
442 | */ |
||
443 | protected static $_db_verification_level = EEM_Base::db_verified_none; |
||
444 | |||
445 | /** |
||
446 | * @const constant for 'default_where_conditions' to apply default where conditions to ALL queried models |
||
447 | * (eg, if retrieving registrations ordered by their datetimes, this will only return non-trashed |
||
448 | * registrations for non-trashed tickets for non-trashed datetimes) |
||
449 | */ |
||
450 | const default_where_conditions_all = 'all'; |
||
451 | |||
452 | /** |
||
453 | * @const constant for 'default_where_conditions' to apply default where conditions to THIS model only, but |
||
454 | * no other models which are joined to (eg, if retrieving registrations ordered by their datetimes, this will |
||
455 | * return non-trashed registrations, regardless of the related datetimes and tickets' statuses). |
||
456 | * It is preferred to use EEM_Base::default_where_conditions_minimum_others because, when joining to |
||
457 | * models which share tables with other models, this can return data for the wrong model. |
||
458 | */ |
||
459 | const default_where_conditions_this_only = 'this_model_only'; |
||
460 | |||
461 | /** |
||
462 | * @const constant for 'default_where_conditions' to apply default where conditions to other models queried, |
||
463 | * but not the current model (eg, if retrieving registrations ordered by their datetimes, this will |
||
464 | * return all registrations related to non-trashed tickets and non-trashed datetimes) |
||
465 | */ |
||
466 | const default_where_conditions_others_only = 'other_models_only'; |
||
467 | |||
468 | /** |
||
469 | * @const constant for 'default_where_conditions' to apply minimum where conditions to all models queried. |
||
470 | * For most models this the same as EEM_Base::default_where_conditions_none, except for models which share |
||
471 | * their table with other models, like the Event and Venue models. For example, when querying for events |
||
472 | * ordered by their venues' name, this will be sure to only return real events with associated real venues |
||
473 | * (regardless of whether those events and venues are trashed) |
||
474 | * In contrast, using EEM_Base::default_where_conditions_none would could return WP posts other than EE |
||
475 | * events. |
||
476 | */ |
||
477 | const default_where_conditions_minimum_all = 'minimum'; |
||
478 | |||
479 | /** |
||
480 | * @const constant for 'default_where_conditions' to apply apply where conditions to other models, and full default |
||
481 | * where conditions for the queried model (eg, when querying events ordered by venues' names, this will |
||
482 | * return non-trashed events for any venues, regardless of whether those associated venues are trashed or |
||
483 | * not) |
||
484 | */ |
||
485 | const default_where_conditions_minimum_others = 'full_this_minimum_others'; |
||
486 | |||
487 | /** |
||
488 | * @const constant for 'default_where_conditions' to NOT apply any where conditions. This should very rarely be |
||
489 | * used, because when querying from a model which shares its table with another model (eg Events and Venues) |
||
490 | * it's possible it will return table entries for other models. You should use |
||
491 | * EEM_Base::default_where_conditions_minimum_all instead. |
||
492 | */ |
||
493 | const default_where_conditions_none = 'none'; |
||
494 | |||
495 | |||
496 | |||
497 | /** |
||
498 | * About all child constructors: |
||
499 | * they should define the _tables, _fields and _model_relations arrays. |
||
500 | * Should ALWAYS be called after child constructor. |
||
501 | * In order to make the child constructors to be as simple as possible, this parent constructor |
||
502 | * finalizes constructing all the object's attributes. |
||
503 | * Generally, rather than requiring a child to code |
||
504 | * $this->_tables = array( |
||
505 | * 'Event_Post_Table' => new EE_Table('Event_Post_Table','wp_posts') |
||
506 | * ...); |
||
507 | * (thus repeating itself in the array key and in the constructor of the new EE_Table,) |
||
508 | * each EE_Table has a function to set the table's alias after the constructor, using |
||
509 | * the array key ('Event_Post_Table'), instead of repeating it. The model fields and model relations |
||
510 | * do something similar. |
||
511 | * |
||
512 | * @param null $timezone |
||
513 | * @throws \EE_Error |
||
514 | */ |
||
515 | protected function __construct($timezone = null) |
||
649 | |||
650 | |||
651 | |||
652 | /** |
||
653 | * Generates the cap restrictions for the given context, or if they were |
||
654 | * already generated just gets what's cached |
||
655 | * |
||
656 | * @param string $context one of EEM_Base::valid_cap_contexts() |
||
657 | * @return EE_Default_Where_Conditions[] |
||
658 | */ |
||
659 | protected function _generate_cap_restrictions($context) |
||
671 | |||
672 | |||
673 | |||
674 | /** |
||
675 | * Used to set the $_model_query_blog_id static property. |
||
676 | * |
||
677 | * @param int $blog_id If provided then will set the blog_id for the models to this id. If not provided then the |
||
678 | * value for get_current_blog_id() will be used. |
||
679 | */ |
||
680 | public static function set_model_query_blog_id($blog_id = 0) |
||
684 | |||
685 | |||
686 | |||
687 | /** |
||
688 | * Returns whatever is set as the internal $model_query_blog_id. |
||
689 | * |
||
690 | * @return int |
||
691 | */ |
||
692 | public static function get_model_query_blog_id() |
||
696 | |||
697 | |||
698 | |||
699 | /** |
||
700 | * This function is a singleton method used to instantiate the Espresso_model object |
||
701 | * |
||
702 | * @access public |
||
703 | * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any |
||
704 | * incoming timezone data that gets saved). Note this just sends the timezone info to the |
||
705 | * date time model field objects. Default is NULL (and will be assumed using the set |
||
706 | * timezone in the 'timezone_string' wp option) |
||
707 | * @return static (as in the concrete child class) |
||
708 | */ |
||
709 | View Code Duplication | public static function instance($timezone = null) |
|
721 | |||
722 | |||
723 | |||
724 | /** |
||
725 | * resets the model and returns it |
||
726 | * |
||
727 | * @param null | string $timezone |
||
728 | * @return EEM_Base|null (if the model was already instantiated, returns it, with |
||
729 | * all its properties reset; if it wasn't instantiated, returns null) |
||
730 | */ |
||
731 | public static function reset($timezone = null) |
||
754 | |||
755 | |||
756 | |||
757 | /** |
||
758 | * retrieve the status details from esp_status table as an array IF this model has the status table as a relation. |
||
759 | * |
||
760 | * @param boolean $translated return localized strings or JUST the array. |
||
761 | * @return array |
||
762 | * @throws \EE_Error |
||
763 | */ |
||
764 | public function status_array($translated = false) |
||
780 | |||
781 | |||
782 | |||
783 | /** |
||
784 | * Gets all the EE_Base_Class objects which match the $query_params, by querying the DB. |
||
785 | * |
||
786 | * @param array $query_params { |
||
787 | * @var array $0 (where) array { |
||
788 | * eg: array('QST_display_text'=>'Are you bob?','QST_admin_text'=>'Determine |
||
789 | * if user is bob') becomes SQL >> "...WHERE QST_display_text = 'Are you |
||
790 | * bob?' AND QST_admin_text = 'Determine if user is bob'...") To add WHERE |
||
791 | * conditions based on related models (and even |
||
792 | * models-related-to-related-models) prepend the model's name onto the field |
||
793 | * name. Eg, |
||
794 | * EEM_Event::instance()->get_all(array(array('Venue.VNU_ID'=>12))); becomes |
||
795 | * SQL >> "SELECT * FROM wp_posts AS Event_CPT LEFT JOIN wp_esp_event_meta |
||
796 | * AS Event_Meta ON Event_CPT.ID = Event_Meta.EVT_ID LEFT JOIN |
||
797 | * wp_esp_event_venue AS Event_Venue ON Event_Venue.EVT_ID=Event_CPT.ID LEFT |
||
798 | * JOIN wp_posts AS Venue_CPT ON Venue_CPT.ID=Event_Venue.VNU_ID LEFT JOIN |
||
799 | * wp_esp_venue_meta AS Venue_Meta ON Venue_CPT.ID = Venue_Meta.VNU_ID WHERE |
||
800 | * Venue_CPT.ID = 12 Notice that automatically took care of joining Events |
||
801 | * to Venues (even when each of those models actually consisted of two |
||
802 | * tables). Also, you may chain the model relations together. Eg instead of |
||
803 | * just having |
||
804 | * "Venue.VNU_ID", you could have |
||
805 | * "Registration.Attendee.ATT_ID" as a field on a query for events (because |
||
806 | * events are related to Registrations, which are related to Attendees). You |
||
807 | * can take it even further with |
||
808 | * "Registration.Transaction.Payment.PAY_amount" etc. To change the operator |
||
809 | * (from the default of '='), change the value to an numerically-indexed |
||
810 | * array, where the first item in the list is the operator. eg: array( |
||
811 | * 'QST_display_text' => array('LIKE','%bob%'), 'QST_ID' => array('<',34), |
||
812 | * 'QST_wp_user' => array('in',array(1,2,7,23))) becomes SQL >> "...WHERE |
||
813 | * QST_display_text LIKE '%bob%' AND QST_ID < 34 AND QST_wp_user IN |
||
814 | * (1,2,7,23)...". Valid operators so far: =, !=, <, <=, >, >=, LIKE, NOT |
||
815 | * LIKE, IN (followed by numeric-indexed array), NOT IN (dido), BETWEEN |
||
816 | * (followed by an array with exactly 2 date strings), IS NULL, and IS NOT |
||
817 | * NULL Values can be a string, int, or float. They can also be arrays IFF |
||
818 | * the operator is IN. Also, values can actually be field names. To indicate |
||
819 | * the value is a field, simply provide a third array item (true) to the |
||
820 | * operator-value array like so: eg: array( 'DTT_reg_limit' => array('>', |
||
821 | * 'DTT_sold', TRUE) ) becomes SQL >> "...WHERE DTT_reg_limit > DTT_sold" |
||
822 | * Note: you can also use related model field names like you would any other |
||
823 | * field name. eg: |
||
824 | * array('Datetime.DTT_reg_limit'=>array('=','Datetime.DTT_sold',TRUE) could |
||
825 | * be used if you were querying EEM_Tickets (because Datetime is directly related to tickets) Also, by default all the where conditions are AND'd together. To override this, add an array key 'OR' (or 'AND') and the array to be OR'd together eg: array('OR'=>array('TXN_ID' => 23 , 'TXN_timestamp__>' => |
||
826 | * 345678912)) becomes SQL >> "...WHERE TXN_ID = 23 OR TXN_timestamp = |
||
827 | * 345678912...". Also, to negate an entire set of conditions, use 'NOT' as |
||
828 | * an array key. eg: array('NOT'=>array('TXN_total' => |
||
829 | * 50, 'TXN_paid'=>23) becomes SQL >> "...where ! (TXN_total =50 AND |
||
830 | * TXN_paid =23) Note: the 'glue' used to join each condition will continue |
||
831 | * to be what you last specified. IE, "AND"s by default, but if you had |
||
832 | * previously specified to use ORs to join, ORs will continue to be used. |
||
833 | * So, if you specify to use an "OR" to join conditions, it will continue to |
||
834 | * "stick" until you specify an AND. eg |
||
835 | * array('OR'=>array('NOT'=>array('TXN_total' => 50, |
||
836 | * 'TXN_paid'=>23)),AND=>array('TXN_ID'=>1,'STS_ID'=>'TIN') becomes SQL >> |
||
837 | * "...where ! (TXN_total =50 OR TXN_paid =23) AND TXN_ID=1 AND |
||
838 | * STS_ID='TIN'" They can be nested indefinitely. eg: |
||
839 | * array('OR'=>array('TXN_total' => 23, 'NOT'=> array( 'TXN_timestamp'=> 345678912, 'AND'=>array('TXN_paid' => 53, 'STS_ID' => 'TIN')))) becomes SQL >> "...WHERE TXN_total = 23 OR ! (TXN_timestamp = 345678912 OR (TXN_paid = 53 AND STS_ID = 'TIN'))..." GOTCHA: because this is an array, array keys must be unique, making it impossible to place two or more where conditions applying to the same field. eg: array('PAY_timestamp'=>array('>',$start_date),'PAY_timestamp'=>array('<',$end_date),'PAY_timestamp'=>array('!=',$special_date)), as PHP enforces that the array keys must be unique, thus removing the first two array entries with key 'PAY_timestamp'. becomes SQL >> "PAY_timestamp != 4234232", ignoring the first two PAY_timestamp conditions). To overcome this, you can add a '*' character to the end of the field's name, followed by anything. These will be removed when generating the SQL string, but allow for the array keys to be unique. eg: you could rewrite the previous query as: array('PAY_timestamp'=>array('>',$start_date),'PAY_timestamp*1st'=>array('<',$end_date),'PAY_timestamp*2nd'=>array('!=',$special_date)) which correctly becomes SQL >> |
||
840 | * "PAY_timestamp > 123412341 AND PAY_timestamp < 2354235235234 AND |
||
841 | * PAY_timestamp != 1241234123" This can be applied to condition operators |
||
842 | * too, eg: |
||
843 | * array('OR'=>array('REG_ID'=>3,'Transaction.TXN_ID'=>23),'OR*whatever'=>array('Attendee.ATT_fname'=>'bob','Attendee.ATT_lname'=>'wilson'))); |
||
844 | * @var mixed $limit int|array adds a limit to the query just like the SQL limit clause, so |
||
845 | * limits of "23", "25,50", and array(23,42) are all valid would become SQL |
||
846 | * "...LIMIT 23", "...LIMIT 25,50", and "...LIMIT 23,42" respectively. |
||
847 | * Remember when you provide two numbers for the limit, the 1st number is |
||
848 | * the OFFSET, the 2nd is the LIMIT |
||
849 | * @var array $on_join_limit allows the setting of a special select join with a internal limit so you |
||
850 | * can do paging on one-to-many multi-table-joins. Send an array in the |
||
851 | * following format array('on_join_limit' |
||
852 | * => array( 'table_alias', array(1,2) ) ). |
||
853 | * @var mixed $order_by name of a column to order by, or an array where keys are field names and |
||
854 | * values are either 'ASC' or 'DESC'. |
||
855 | * 'limit'=>array('STS_ID'=>'ASC','REG_date'=>'DESC'), which would becomes |
||
856 | * SQL "...ORDER BY TXN_timestamp..." and "...ORDER BY STS_ID ASC, REG_date |
||
857 | * DESC..." respectively. Like the |
||
858 | * 'where' conditions, these fields can be on related models. Eg |
||
859 | * 'order_by'=>array('Registration.Transaction.TXN_amount'=>'ASC') is |
||
860 | * perfectly valid from any model related to 'Registration' (like Event, |
||
861 | * Attendee, Price, Datetime, etc.) |
||
862 | * @var string $order If 'order_by' is used and its value is a string (NOT an array), then |
||
863 | * 'order' specifies whether to order the field specified in 'order_by' in |
||
864 | * ascending or descending order. Acceptable values are 'ASC' or 'DESC'. If, |
||
865 | * 'order_by' isn't used, but 'order' is, then it is assumed you want to |
||
866 | * order by the primary key. Eg, |
||
867 | * EEM_Event::instance()->get_all(array('order_by'=>'Datetime.DTT_EVT_start','order'=>'ASC'); |
||
868 | * //(will join with the Datetime model's table(s) and order by its field |
||
869 | * DTT_EVT_start) or |
||
870 | * EEM_Registration::instance()->get_all(array('order'=>'ASC'));//will make |
||
871 | * SQL "SELECT * FROM wp_esp_registration ORDER BY REG_ID ASC" |
||
872 | * @var mixed $group_by name of field to order by, or an array of fields. Eg either |
||
873 | * 'group_by'=>'VNU_ID', or |
||
874 | * 'group_by'=>array('EVT_name','Registration.Transaction.TXN_total') Note: |
||
875 | * if no |
||
876 | * $group_by is specified, and a limit is set, automatically groups by the |
||
877 | * model's primary key (or combined primary keys). This avoids some |
||
878 | * weirdness that results when using limits, tons of joins, and no group by, |
||
879 | * see https://events.codebasehq.com/projects/event-espresso/tickets/9389 |
||
880 | * @var array $having exactly like WHERE parameters array, except these conditions apply to the |
||
881 | * grouped results (whereas WHERE conditions apply to the pre-grouped |
||
882 | * results) |
||
883 | * @var array $force_join forces a join with the models named. Should be a numerically-indexed |
||
884 | * array where values are models to be joined in the query.Eg |
||
885 | * array('Attendee','Payment','Datetime'). You may join with transient |
||
886 | * models using period, eg "Registration.Transaction.Payment". You will |
||
887 | * probably only want to do this in hopes of increasing efficiency, as |
||
888 | * related models which belongs to the current model |
||
889 | * (ie, the current model has a foreign key to them, like how Registration |
||
890 | * belongs to Attendee) can be cached in order to avoid future queries |
||
891 | * @var string $default_where_conditions can be set to 'none', 'this_model_only', 'other_models_only', or 'all'. |
||
892 | * set this to 'none' to disable all default where conditions. Eg, usually |
||
893 | * soft-deleted objects are filtered-out if you want to include them, set |
||
894 | * this query param to 'none'. If you want to ONLY disable THIS model's |
||
895 | * default where conditions set it to 'other_models_only'. If you only want |
||
896 | * this model's default where conditions added to the query, use |
||
897 | * 'this_model_only'. If you want to use all default where conditions |
||
898 | * (default), set to 'all'. |
||
899 | * @var string $caps controls what capability requirements to apply to the query; ie, should |
||
900 | * we just NOT apply any capabilities/permissions/restrictions and return |
||
901 | * everything? Or should we only show the current user items they should be |
||
902 | * able to view on the frontend, backend, edit, or delete? can be set to |
||
903 | * 'none' (default), 'read_frontend', 'read_backend', 'edit' or 'delete' |
||
904 | * } |
||
905 | * @return EE_Base_Class[] *note that there is NO option to pass the output type. If you want results different |
||
906 | * from EE_Base_Class[], use _get_all_wpdb_results()and make it public |
||
907 | * again. Array keys are object IDs (if there is a primary key on the model. |
||
908 | * if not, numerically indexed) Some full examples: get 10 transactions |
||
909 | * which have Scottish attendees: EEM_Transaction::instance()->get_all( |
||
910 | * array( array( |
||
911 | * 'OR'=>array( |
||
912 | * 'Registration.Attendee.ATT_fname'=>array('like','Mc%'), |
||
913 | * 'Registration.Attendee.ATT_fname*other'=>array('like','Mac%') |
||
914 | * ) |
||
915 | * ), |
||
916 | * 'limit'=>10, |
||
917 | * 'group_by'=>'TXN_ID' |
||
918 | * )); |
||
919 | * get all the answers to the question titled "shirt size" for event with id |
||
920 | * 12, ordered by their answer EEM_Answer::instance()->get_all(array( array( |
||
921 | * 'Question.QST_display_text'=>'shirt size', |
||
922 | * 'Registration.Event.EVT_ID'=>12 |
||
923 | * ), |
||
924 | * 'order_by'=>array('ANS_value'=>'ASC') |
||
925 | * )); |
||
926 | * @throws \EE_Error |
||
927 | */ |
||
928 | public function get_all($query_params = array()) |
||
937 | |||
938 | |||
939 | |||
940 | /** |
||
941 | * Modifies the query parameters so we only get back model objects |
||
942 | * that "belong" to the current user |
||
943 | * |
||
944 | * @param array $query_params @see EEM_Base::get_all() |
||
945 | * @return array like EEM_Base::get_all |
||
946 | */ |
||
947 | public function alter_query_params_to_only_include_mine($query_params = array()) |
||
955 | |||
956 | |||
957 | |||
958 | /** |
||
959 | * Returns the name of the field's name that points to the WP_User table |
||
960 | * on this model (or follows the _model_chain_to_wp_user and uses that model's |
||
961 | * foreign key to the WP_User table) |
||
962 | * |
||
963 | * @return string|boolean string on success, boolean false when there is no |
||
964 | * foreign key to the WP_User table |
||
965 | */ |
||
966 | public function wp_user_field_name() |
||
984 | |||
985 | |||
986 | |||
987 | /** |
||
988 | * Returns the _model_chain_to_wp_user string, which indicates which related model |
||
989 | * (or transiently-related model) has a foreign key to the wp_users table; |
||
990 | * useful for finding if model objects of this type are 'owned' by the current user. |
||
991 | * This is an empty string when the foreign key is on this model and when it isn't, |
||
992 | * but is only non-empty when this model's ownership is indicated by a RELATED model |
||
993 | * (or transiently-related model) |
||
994 | * |
||
995 | * @return string |
||
996 | */ |
||
997 | public function model_chain_to_wp_user() |
||
1001 | |||
1002 | |||
1003 | |||
1004 | /** |
||
1005 | * Whether this model is 'owned' by a specific wordpress user (even indirectly, |
||
1006 | * like how registrations don't have a foreign key to wp_users, but the |
||
1007 | * events they are for are), or is unrelated to wp users. |
||
1008 | * generally available |
||
1009 | * |
||
1010 | * @return boolean |
||
1011 | */ |
||
1012 | public function is_owned() |
||
1025 | |||
1026 | |||
1027 | |||
1028 | /** |
||
1029 | * Used internally to get WPDB results, because other functions, besides get_all, may want to do some queries, but |
||
1030 | * may want to preserve the WPDB results (eg, update, which first queries to make sure we have all the tables on |
||
1031 | * the model) |
||
1032 | * |
||
1033 | * @param array $query_params like EEM_Base::get_all's $query_params |
||
1034 | * @param string $output ARRAY_A, OBJECT_K, etc. Just like |
||
1035 | * @param mixed $columns_to_select , What columns to select. By default, we select all columns specified by the |
||
1036 | * fields on the model, and the models we joined to in the query. However, you can |
||
1037 | * override this and set the select to "*", or a specific column name, like |
||
1038 | * "ATT_ID", etc. If you would like to use these custom selections in WHERE, |
||
1039 | * GROUP_BY, or HAVING clauses, you must instead provide an array. Array keys are |
||
1040 | * the aliases used to refer to this selection, and values are to be |
||
1041 | * numerically-indexed arrays, where 0 is the selection and 1 is the data type. |
||
1042 | * Eg, array('count'=>array('COUNT(REG_ID)','%d')) |
||
1043 | * @return array | stdClass[] like results of $wpdb->get_results($sql,OBJECT), (ie, output type is OBJECT) |
||
1044 | * @throws \EE_Error |
||
1045 | */ |
||
1046 | protected function _get_all_wpdb_results($query_params = array(), $output = ARRAY_A, $columns_to_select = null) |
||
1059 | |||
1060 | |||
1061 | |||
1062 | /** |
||
1063 | * Gets an array of rows from the database just like $wpdb->get_results would, |
||
1064 | * but you can use the $query_params like on EEM_Base::get_all() to more easily |
||
1065 | * take care of joins, field preparation etc. |
||
1066 | * |
||
1067 | * @param array $query_params like EEM_Base::get_all's $query_params |
||
1068 | * @param string $output ARRAY_A, OBJECT_K, etc. Just like |
||
1069 | * @param mixed $columns_to_select , What columns to select. By default, we select all columns specified by the |
||
1070 | * fields on the model, and the models we joined to in the query. However, you can |
||
1071 | * override this and set the select to "*", or a specific column name, like |
||
1072 | * "ATT_ID", etc. If you would like to use these custom selections in WHERE, |
||
1073 | * GROUP_BY, or HAVING clauses, you must instead provide an array. Array keys are |
||
1074 | * the aliases used to refer to this selection, and values are to be |
||
1075 | * numerically-indexed arrays, where 0 is the selection and 1 is the data type. |
||
1076 | * Eg, array('count'=>array('COUNT(REG_ID)','%d')) |
||
1077 | * @return array|stdClass[] like results of $wpdb->get_results($sql,OBJECT), (ie, output type is OBJECT) |
||
1078 | * @throws \EE_Error |
||
1079 | */ |
||
1080 | public function get_all_wpdb_results($query_params = array(), $output = ARRAY_A, $columns_to_select = null) |
||
1084 | |||
1085 | |||
1086 | |||
1087 | /** |
||
1088 | * For creating a custom select statement |
||
1089 | * |
||
1090 | * @param mixed $columns_to_select either a string to be inserted directly as the select statement, |
||
1091 | * or an array where keys are aliases, and values are arrays where 0=>the selection |
||
1092 | * SQL, and 1=>is the datatype |
||
1093 | * @throws EE_Error |
||
1094 | * @return string |
||
1095 | */ |
||
1096 | private function _construct_select_from_input($columns_to_select) |
||
1135 | |||
1136 | |||
1137 | |||
1138 | /** |
||
1139 | * Convenient wrapper for getting the primary key field's name. Eg, on Registration, this would be 'REG_ID' |
||
1140 | * |
||
1141 | * @return string |
||
1142 | * @throws \EE_Error |
||
1143 | */ |
||
1144 | public function primary_key_name() |
||
1148 | |||
1149 | |||
1150 | |||
1151 | /** |
||
1152 | * Gets a single item for this model from the DB, given only its ID (or null if none is found). |
||
1153 | * If there is no primary key on this model, $id is treated as primary key string |
||
1154 | * |
||
1155 | * @param mixed $id int or string, depending on the type of the model's primary key |
||
1156 | * @return EE_Base_Class |
||
1157 | */ |
||
1158 | public function get_one_by_ID($id) |
||
1170 | |||
1171 | |||
1172 | |||
1173 | /** |
||
1174 | * Alters query parameters to only get items with this ID are returned. |
||
1175 | * Takes into account that the ID might be a string produced by EEM_Base::get_index_primary_key_string(), |
||
1176 | * or could just be a simple primary key ID |
||
1177 | * |
||
1178 | * @param int $id |
||
1179 | * @param array $query_params |
||
1180 | * @return array of normal query params, @see EEM_Base::get_all |
||
1181 | * @throws \EE_Error |
||
1182 | */ |
||
1183 | public function alter_query_params_to_restrict_by_ID($id, $query_params = array()) |
||
1197 | |||
1198 | |||
1199 | |||
1200 | /** |
||
1201 | * Gets a single item for this model from the DB, given the $query_params. Only returns a single class, not an |
||
1202 | * array. If no item is found, null is returned. |
||
1203 | * |
||
1204 | * @param array $query_params like EEM_Base's $query_params variable. |
||
1205 | * @return EE_Base_Class|EE_Soft_Delete_Base_Class|NULL |
||
1206 | * @throws \EE_Error |
||
1207 | */ |
||
1208 | View Code Duplication | public function get_one($query_params = array()) |
|
1224 | |||
1225 | |||
1226 | |||
1227 | /** |
||
1228 | * Returns the next x number of items in sequence from the given value as |
||
1229 | * found in the database matching the given query conditions. |
||
1230 | * |
||
1231 | * @param mixed $current_field_value Value used for the reference point. |
||
1232 | * @param null $field_to_order_by What field is used for the |
||
1233 | * reference point. |
||
1234 | * @param int $limit How many to return. |
||
1235 | * @param array $query_params Extra conditions on the query. |
||
1236 | * @param null $columns_to_select If left null, then an array of |
||
1237 | * EE_Base_Class objects is returned, |
||
1238 | * otherwise you can indicate just the |
||
1239 | * columns you want returned. |
||
1240 | * @return EE_Base_Class[]|array |
||
1241 | * @throws \EE_Error |
||
1242 | */ |
||
1243 | public function next_x( |
||
1253 | |||
1254 | |||
1255 | |||
1256 | /** |
||
1257 | * Returns the previous x number of items in sequence from the given value |
||
1258 | * as found in the database matching the given query conditions. |
||
1259 | * |
||
1260 | * @param mixed $current_field_value Value used for the reference point. |
||
1261 | * @param null $field_to_order_by What field is used for the |
||
1262 | * reference point. |
||
1263 | * @param int $limit How many to return. |
||
1264 | * @param array $query_params Extra conditions on the query. |
||
1265 | * @param null $columns_to_select If left null, then an array of |
||
1266 | * EE_Base_Class objects is returned, |
||
1267 | * otherwise you can indicate just the |
||
1268 | * columns you want returned. |
||
1269 | * @return EE_Base_Class[]|array |
||
1270 | * @throws \EE_Error |
||
1271 | */ |
||
1272 | public function previous_x( |
||
1282 | |||
1283 | |||
1284 | |||
1285 | /** |
||
1286 | * Returns the next item in sequence from the given value as found in the |
||
1287 | * database matching the given query conditions. |
||
1288 | * |
||
1289 | * @param mixed $current_field_value Value used for the reference point. |
||
1290 | * @param null $field_to_order_by What field is used for the |
||
1291 | * reference point. |
||
1292 | * @param array $query_params Extra conditions on the query. |
||
1293 | * @param null $columns_to_select If left null, then an EE_Base_Class |
||
1294 | * object is returned, otherwise you |
||
1295 | * can indicate just the columns you |
||
1296 | * want and a single array indexed by |
||
1297 | * the columns will be returned. |
||
1298 | * @return EE_Base_Class|null|array() |
||
1299 | * @throws \EE_Error |
||
1300 | */ |
||
1301 | View Code Duplication | public function next( |
|
1311 | |||
1312 | |||
1313 | |||
1314 | /** |
||
1315 | * Returns the previous item in sequence from the given value as found in |
||
1316 | * the database matching the given query conditions. |
||
1317 | * |
||
1318 | * @param mixed $current_field_value Value used for the reference point. |
||
1319 | * @param null $field_to_order_by What field is used for the |
||
1320 | * reference point. |
||
1321 | * @param array $query_params Extra conditions on the query. |
||
1322 | * @param null $columns_to_select If left null, then an EE_Base_Class |
||
1323 | * object is returned, otherwise you |
||
1324 | * can indicate just the columns you |
||
1325 | * want and a single array indexed by |
||
1326 | * the columns will be returned. |
||
1327 | * @return EE_Base_Class|null|array() |
||
1328 | * @throws EE_Error |
||
1329 | */ |
||
1330 | View Code Duplication | public function previous( |
|
1340 | |||
1341 | |||
1342 | |||
1343 | /** |
||
1344 | * Returns the a consecutive number of items in sequence from the given |
||
1345 | * value as found in the database matching the given query conditions. |
||
1346 | * |
||
1347 | * @param mixed $current_field_value Value used for the reference point. |
||
1348 | * @param string $operand What operand is used for the sequence. |
||
1349 | * @param string $field_to_order_by What field is used for the reference point. |
||
1350 | * @param int $limit How many to return. |
||
1351 | * @param array $query_params Extra conditions on the query. |
||
1352 | * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, |
||
1353 | * otherwise you can indicate just the columns you want returned. |
||
1354 | * @return EE_Base_Class[]|array |
||
1355 | * @throws EE_Error |
||
1356 | */ |
||
1357 | protected function _get_consecutive( |
||
1400 | |||
1401 | |||
1402 | |||
1403 | /** |
||
1404 | * This sets the _timezone property after model object has been instantiated. |
||
1405 | * |
||
1406 | * @param null | string $timezone valid PHP DateTimeZone timezone string |
||
1407 | */ |
||
1408 | public function set_timezone($timezone) |
||
1424 | |||
1425 | |||
1426 | |||
1427 | /** |
||
1428 | * This just returns whatever is set for the current timezone. |
||
1429 | * |
||
1430 | * @access public |
||
1431 | * @return string |
||
1432 | */ |
||
1433 | public function get_timezone() |
||
1450 | |||
1451 | |||
1452 | |||
1453 | /** |
||
1454 | * This returns the date formats set for the given field name and also ensures that |
||
1455 | * $this->_timezone property is set correctly. |
||
1456 | * |
||
1457 | * @since 4.6.x |
||
1458 | * @param string $field_name The name of the field the formats are being retrieved for. |
||
1459 | * @param bool $pretty Whether to return the pretty formats (true) or not (false). |
||
1460 | * @throws EE_Error If the given field_name is not of the EE_Datetime_Field type. |
||
1461 | * @return array formats in an array with the date format first, and the time format last. |
||
1462 | */ |
||
1463 | public function get_formats_for($field_name, $pretty = false) |
||
1476 | |||
1477 | |||
1478 | |||
1479 | /** |
||
1480 | * This returns the current time in a format setup for a query on this model. |
||
1481 | * Usage of this method makes it easier to setup queries against EE_Datetime_Field columns because |
||
1482 | * it will return: |
||
1483 | * - a formatted string in the timezone and format currently set on the EE_Datetime_Field for the given field for |
||
1484 | * NOW |
||
1485 | * - or a unix timestamp (equivalent to time()) |
||
1486 | * Note: When requesting a formatted string, if the date or time format doesn't include seconds, for example, |
||
1487 | * the time returned, because it uses that format, will also NOT include seconds. For this reason, if you want |
||
1488 | * the time returned to be the current time down to the exact second, set $timestamp to true. |
||
1489 | * @since 4.6.x |
||
1490 | * @param string $field_name The field the current time is needed for. |
||
1491 | * @param bool $timestamp True means to return a unix timestamp. Otherwise a |
||
1492 | * formatted string matching the set format for the field in the set timezone will |
||
1493 | * be returned. |
||
1494 | * @param string $what Whether to return the string in just the time format, the date format, or both. |
||
1495 | * @throws EE_Error If the given field_name is not of the EE_Datetime_Field type. |
||
1496 | * @return int|string If the given field_name is not of the EE_Datetime_Field type, then an EE_Error |
||
1497 | * exception is triggered. |
||
1498 | */ |
||
1499 | public function current_time_for_query($field_name, $timestamp = false, $what = 'both') |
||
1519 | |||
1520 | |||
1521 | |||
1522 | /** |
||
1523 | * This receives a time string for a given field and ensures that it is setup to match what the internal settings |
||
1524 | * for the model are. Returns a DateTime object. |
||
1525 | * Note: a gotcha for when you send in unix timestamp. Remember a unix timestamp is already timezone agnostic, |
||
1526 | * (functionally the equivalent of UTC+0). So when you send it in, whatever timezone string you include is |
||
1527 | * ignored. |
||
1528 | * |
||
1529 | * @param string $field_name The field being setup. |
||
1530 | * @param string $timestring The date time string being used. |
||
1531 | * @param string $incoming_format The format for the time string. |
||
1532 | * @param string $timezone By default, it is assumed the incoming time string is in timezone for |
||
1533 | * the blog. If this is not the case, then it can be specified here. If incoming |
||
1534 | * format is |
||
1535 | * 'U', this is ignored. |
||
1536 | * @return DateTime |
||
1537 | * @throws \EE_Error |
||
1538 | */ |
||
1539 | public function convert_datetime_for_query($field_name, $timestring, $incoming_format, $timezone = '') |
||
1548 | |||
1549 | |||
1550 | |||
1551 | /** |
||
1552 | * Gets all the tables comprising this model. Array keys are the table aliases, and values are EE_Table objects |
||
1553 | * |
||
1554 | * @return EE_Table_Base[] |
||
1555 | */ |
||
1556 | public function get_tables() |
||
1560 | |||
1561 | |||
1562 | |||
1563 | /** |
||
1564 | * Updates all the database entries (in each table for this model) according to $fields_n_values and optionally |
||
1565 | * also updates all the model objects, where the criteria expressed in $query_params are met.. |
||
1566 | * Also note: if this model has multiple tables, this update verifies all the secondary tables have an entry for |
||
1567 | * each row (in the primary table) we're trying to update; if not, it inserts an entry in the secondary table. Eg: |
||
1568 | * if our model has 2 tables: wp_posts (primary), and wp_esp_event (secondary). Let's say we are trying to update a |
||
1569 | * model object with EVT_ID = 1 |
||
1570 | * (which means where wp_posts has ID = 1, because wp_posts.ID is the primary key's column), which exists, but |
||
1571 | * there is no entry in wp_esp_event for this entry in wp_posts. So, this update script will insert a row into |
||
1572 | * wp_esp_event, using any available parameters from $fields_n_values (eg, if "EVT_limit" => 40 is in |
||
1573 | * $fields_n_values, the new entry in wp_esp_event will set EVT_limit = 40, and use default for other columns which |
||
1574 | * are not specified) |
||
1575 | * |
||
1576 | * @param array $fields_n_values keys are model fields (exactly like keys in EEM_Base::_fields, NOT db |
||
1577 | * columns!), values are strings, ints, floats, and maybe arrays if they |
||
1578 | * are to be serialized. Basically, the values are what you'd expect to be |
||
1579 | * values on the model, NOT necessarily what's in the DB. For example, if |
||
1580 | * we wanted to update only the TXN_details on any Transactions where its |
||
1581 | * ID=34, we'd use this method as follows: |
||
1582 | * EEM_Transaction::instance()->update( |
||
1583 | * array('TXN_details'=>array('detail1'=>'monkey','detail2'=>'banana'), |
||
1584 | * array(array('TXN_ID'=>34))); |
||
1585 | * @param array $query_params very much like EEM_Base::get_all's $query_params |
||
1586 | * in client code into what's expected to be stored on each field. Eg, |
||
1587 | * consider updating Question's QST_admin_label field is of type |
||
1588 | * Simple_HTML. If you use this function to update that field to $new_value |
||
1589 | * = (note replace 8's with appropriate opening and closing tags in the |
||
1590 | * following example)"8script8alert('I hack all');8/script88b8boom |
||
1591 | * baby8/b8", then if you set $values_already_prepared_by_model_object to |
||
1592 | * TRUE, it is assumed that you've already called |
||
1593 | * EE_Simple_HTML_Field->prepare_for_set($new_value), which removes the |
||
1594 | * malicious javascript. However, if |
||
1595 | * $values_already_prepared_by_model_object is left as FALSE, then |
||
1596 | * EE_Simple_HTML_Field->prepare_for_set($new_value) will be called on it, |
||
1597 | * and every other field, before insertion. We provide this parameter |
||
1598 | * because model objects perform their prepare_for_set function on all |
||
1599 | * their values, and so don't need to be called again (and in many cases, |
||
1600 | * shouldn't be called again. Eg: if we escape HTML characters in the |
||
1601 | * prepare_for_set method...) |
||
1602 | * @param boolean $keep_model_objs_in_sync if TRUE, makes sure we ALSO update model objects |
||
1603 | * in this model's entity map according to $fields_n_values that match |
||
1604 | * $query_params. This obviously has some overhead, so you can disable it |
||
1605 | * by setting this to FALSE, but be aware that model objects being used |
||
1606 | * could get out-of-sync with the database |
||
1607 | * @return int how many rows got updated or FALSE if something went wrong with the query (wp returns FALSE or num |
||
1608 | * rows affected which *could* include 0 which DOES NOT mean the query was |
||
1609 | * bad) |
||
1610 | * @throws \EE_Error |
||
1611 | */ |
||
1612 | public function update($fields_n_values, $query_params, $keep_model_objs_in_sync = true) |
||
1744 | |||
1745 | |||
1746 | |||
1747 | /** |
||
1748 | * Analogous to $wpdb->get_col, returns a 1-dimensional array where teh values |
||
1749 | * are teh values of the field specified (or by default the primary key field) |
||
1750 | * that matched the query params. Note that you should pass the name of the |
||
1751 | * model FIELD, not the database table's column name. |
||
1752 | * |
||
1753 | * @param array $query_params @see EEM_Base::get_all() |
||
1754 | * @param string $field_to_select |
||
1755 | * @return array just like $wpdb->get_col() |
||
1756 | * @throws \EE_Error |
||
1757 | */ |
||
1758 | public function get_col($query_params = array(), $field_to_select = null) |
||
1773 | |||
1774 | |||
1775 | |||
1776 | /** |
||
1777 | * Returns a single column value for a single row from the database |
||
1778 | * |
||
1779 | * @param array $query_params @see EEM_Base::get_all() |
||
1780 | * @param string $field_to_select @see EEM_Base::get_col() |
||
1781 | * @return string |
||
1782 | * @throws \EE_Error |
||
1783 | */ |
||
1784 | public function get_var($query_params = array(), $field_to_select = null) |
||
1794 | |||
1795 | |||
1796 | |||
1797 | /** |
||
1798 | * Makes the SQL for after "UPDATE table_X inner join table_Y..." and before "...WHERE". Eg "Question.name='party |
||
1799 | * time?', Question.desc='what do you think?',..." Values are filtered through wpdb->prepare to avoid against SQL |
||
1800 | * injection, but currently no further filtering is done |
||
1801 | * |
||
1802 | * @global $wpdb |
||
1803 | * @param array $fields_n_values array keys are field names on this model, and values are what those fields should |
||
1804 | * be updated to in the DB |
||
1805 | * @return string of SQL |
||
1806 | * @throws \EE_Error |
||
1807 | */ |
||
1808 | public function _construct_update_sql($fields_n_values) |
||
1824 | |||
1825 | |||
1826 | |||
1827 | /** |
||
1828 | * Deletes a single row from the DB given the model object's primary key value. (eg, EE_Attendee->ID()'s value). |
||
1829 | * Performs a HARD delete, meaning the database row should always be removed, |
||
1830 | * not just have a flag field on it switched |
||
1831 | * Wrapper for EEM_Base::delete_permanently() |
||
1832 | * |
||
1833 | * @param mixed $id |
||
1834 | * @return boolean whether the row got deleted or not |
||
1835 | * @throws \EE_Error |
||
1836 | */ |
||
1837 | public function delete_permanently_by_ID($id) |
||
1846 | |||
1847 | |||
1848 | |||
1849 | /** |
||
1850 | * Deletes a single row from the DB given the model object's primary key value. (eg, EE_Attendee->ID()'s value). |
||
1851 | * Wrapper for EEM_Base::delete() |
||
1852 | * |
||
1853 | * @param mixed $id |
||
1854 | * @return boolean whether the row got deleted or not |
||
1855 | * @throws \EE_Error |
||
1856 | */ |
||
1857 | public function delete_by_ID($id) |
||
1866 | |||
1867 | |||
1868 | |||
1869 | /** |
||
1870 | * Identical to delete_permanently, but does a "soft" delete if possible, |
||
1871 | * meaning if the model has a field that indicates its been "trashed" or |
||
1872 | * "soft deleted", we will just set that instead of actually deleting the rows. |
||
1873 | * |
||
1874 | * @see EEM_Base::delete_permanently |
||
1875 | * @param array $query_params |
||
1876 | * @param boolean $allow_blocking |
||
1877 | * @return int how many rows got deleted |
||
1878 | * @throws \EE_Error |
||
1879 | */ |
||
1880 | public function delete($query_params, $allow_blocking = true) |
||
1884 | |||
1885 | |||
1886 | |||
1887 | /** |
||
1888 | * Deletes the model objects that meet the query params. Note: this method is overridden |
||
1889 | * in EEM_Soft_Delete_Base so that soft-deleted model objects are instead only flagged |
||
1890 | * as archived, not actually deleted |
||
1891 | * |
||
1892 | * @param array $query_params very much like EEM_Base::get_all's $query_params |
||
1893 | * @param boolean $allow_blocking if TRUE, matched objects will only be deleted if there is no related model info |
||
1894 | * that blocks it (ie, there' sno other data that depends on this data); if false, |
||
1895 | * deletes regardless of other objects which may depend on it. Its generally |
||
1896 | * advisable to always leave this as TRUE, otherwise you could easily corrupt your |
||
1897 | * DB |
||
1898 | * @return int how many rows got deleted |
||
1899 | * @throws \EE_Error |
||
1900 | */ |
||
1901 | public function delete_permanently($query_params, $allow_blocking = true) |
||
1978 | |||
1979 | |||
1980 | |||
1981 | /** |
||
1982 | * Checks all the relations that throw error messages when there are blocking related objects |
||
1983 | * for related model objects. If there are any related model objects on those relations, |
||
1984 | * adds an EE_Error, and return true |
||
1985 | * |
||
1986 | * @param EE_Base_Class|int $this_model_obj_or_id |
||
1987 | * @param EE_Base_Class $ignore_this_model_obj a model object like 'EE_Event', or 'EE_Term_Taxonomy', which |
||
1988 | * should be ignored when determining whether there are related |
||
1989 | * model objects which block this model object's deletion. Useful |
||
1990 | * if you know A is related to B and are considering deleting A, |
||
1991 | * but want to see if A has any other objects blocking its deletion |
||
1992 | * before removing the relation between A and B |
||
1993 | * @return boolean |
||
1994 | * @throws \EE_Error |
||
1995 | */ |
||
1996 | public function delete_is_blocked_by_related_models($this_model_obj_or_id, $ignore_this_model_obj = null) |
||
2031 | |||
2032 | |||
2033 | /** |
||
2034 | * Builds the columns and values for items to delete from the incoming $row_results_for_deleting array. |
||
2035 | * @param array $row_results_for_deleting |
||
2036 | * @param bool $allow_blocking |
||
2037 | * @return array The shape of this array depends on whether the model `has_primary_key_field` or not. If the |
||
2038 | * model DOES have a primary_key_field, then the array will be a simple single dimension array where |
||
2039 | * the key is the fully qualified primary key column and the value is an array of ids that will be |
||
2040 | * deleted. Example: |
||
2041 | * array('Event.EVT_ID' => array( 1,2,3)) |
||
2042 | * If the model DOES NOT have a primary_key_field, then the array will be a two dimensional array |
||
2043 | * where each element is a group of columns and values that get deleted. Example: |
||
2044 | * array( |
||
2045 | * 0 => array( |
||
2046 | * 'Term_Relationship.object_id' => 1 |
||
2047 | * 'Term_Relationship.term_taxonomy_id' => 5 |
||
2048 | * ), |
||
2049 | * 1 => array( |
||
2050 | * 'Term_Relationship.object_id' => 1 |
||
2051 | * 'Term_Relationship.term_taxonomy_id' => 6 |
||
2052 | * ) |
||
2053 | * ) |
||
2054 | * @throws EE_Error |
||
2055 | */ |
||
2056 | protected function _get_ids_for_delete(array $row_results_for_deleting, $allow_blocking = true) |
||
2100 | |||
2101 | |||
2102 | /** |
||
2103 | * This receives an array of columns and values set to be deleted (as prepared by _get_ids_for_delete) and prepares |
||
2104 | * the corresponding query_part for the query performing the delete. |
||
2105 | * |
||
2106 | * @param array $ids_to_delete_indexed_by_column @see _get_ids_for_delete for how this array might be shaped. |
||
2107 | * @return string |
||
2108 | * @throws EE_Error |
||
2109 | */ |
||
2110 | protected function _build_query_part_for_deleting_from_columns_and_values(array $ids_to_delete_indexed_by_column) { |
||
2135 | |||
2136 | |||
2137 | |||
2138 | |||
2139 | /** |
||
2140 | * Count all the rows that match criteria expressed in $query_params (an array just like arg to EEM_Base::get_all). |
||
2141 | * If $field_to_count isn't provided, the model's primary key is used. Otherwise, we count by field_to_count's |
||
2142 | * column |
||
2143 | * |
||
2144 | * @param array $query_params like EEM_Base::get_all's |
||
2145 | * @param string $field_to_count field on model to count by (not column name) |
||
2146 | * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
||
2147 | * that by the setting $distinct to TRUE; |
||
2148 | * @return int |
||
2149 | * @throws \EE_Error |
||
2150 | */ |
||
2151 | public function count($query_params = array(), $field_to_count = null, $distinct = false) |
||
2179 | |||
2180 | |||
2181 | |||
2182 | /** |
||
2183 | * Sums up the value of the $field_to_sum (defaults to the primary key, which isn't terribly useful) |
||
2184 | * |
||
2185 | * @param array $query_params like EEM_Base::get_all |
||
2186 | * @param string $field_to_sum name of field (array key in $_fields array) |
||
2187 | * @return float |
||
2188 | * @throws \EE_Error |
||
2189 | */ |
||
2190 | public function sum($query_params, $field_to_sum = null) |
||
2208 | |||
2209 | |||
2210 | |||
2211 | /** |
||
2212 | * Just calls the specified method on $wpdb with the given arguments |
||
2213 | * Consolidates a little extra error handling code |
||
2214 | * |
||
2215 | * @param string $wpdb_method |
||
2216 | * @param array $arguments_to_provide |
||
2217 | * @throws EE_Error |
||
2218 | * @global wpdb $wpdb |
||
2219 | * @return mixed |
||
2220 | */ |
||
2221 | protected function _do_wpdb_query($wpdb_method, $arguments_to_provide) |
||
2266 | |||
2267 | |||
2268 | |||
2269 | /** |
||
2270 | * Attempts to run the indicated WPDB method with the provided arguments, |
||
2271 | * and if there's an error tries to verify the DB is correct. Uses |
||
2272 | * the static property EEM_Base::$_db_verification_level to determine whether |
||
2273 | * we should try to fix the EE core db, the addons, or just give up |
||
2274 | * |
||
2275 | * @param string $wpdb_method |
||
2276 | * @param array $arguments_to_provide |
||
2277 | * @return mixed |
||
2278 | */ |
||
2279 | private function _process_wpdb_query($wpdb_method, $arguments_to_provide) |
||
2312 | |||
2313 | |||
2314 | |||
2315 | /** |
||
2316 | * Verifies the EE core database is up-to-date and records that we've done it on |
||
2317 | * EEM_Base::$_db_verification_level |
||
2318 | * |
||
2319 | * @param string $wpdb_method |
||
2320 | * @param array $arguments_to_provide |
||
2321 | * @return string |
||
2322 | */ |
||
2323 | View Code Duplication | private function _verify_core_db($wpdb_method, $arguments_to_provide) |
|
2339 | |||
2340 | |||
2341 | |||
2342 | /** |
||
2343 | * Verifies the EE addons' database is up-to-date and records that we've done it on |
||
2344 | * EEM_Base::$_db_verification_level |
||
2345 | * |
||
2346 | * @param $wpdb_method |
||
2347 | * @param $arguments_to_provide |
||
2348 | * @return string |
||
2349 | */ |
||
2350 | View Code Duplication | private function _verify_addons_db($wpdb_method, $arguments_to_provide) |
|
2366 | |||
2367 | |||
2368 | |||
2369 | /** |
||
2370 | * In order to avoid repeating this code for the get_all, sum, and count functions, put the code parts |
||
2371 | * that are identical in here. Returns a string of SQL of everything in a SELECT query except the beginning |
||
2372 | * SELECT clause, eg " FROM wp_posts AS Event INNER JOIN ... WHERE ... ORDER BY ... LIMIT ... GROUP BY ... HAVING |
||
2373 | * ..." |
||
2374 | * |
||
2375 | * @param EE_Model_Query_Info_Carrier $model_query_info |
||
2376 | * @return string |
||
2377 | */ |
||
2378 | private function _construct_2nd_half_of_select_query(EE_Model_Query_Info_Carrier $model_query_info) |
||
2387 | |||
2388 | |||
2389 | |||
2390 | /** |
||
2391 | * Set to easily debug the next X queries ran from this model. |
||
2392 | * |
||
2393 | * @param int $count |
||
2394 | */ |
||
2395 | public function show_next_x_db_queries($count = 1) |
||
2399 | |||
2400 | |||
2401 | |||
2402 | /** |
||
2403 | * @param $sql_query |
||
2404 | */ |
||
2405 | public function show_db_query_if_previously_requested($sql_query) |
||
2412 | |||
2413 | |||
2414 | |||
2415 | /** |
||
2416 | * Adds a relationship of the correct type between $modelObject and $otherModelObject. |
||
2417 | * There are the 3 cases: |
||
2418 | * 'belongsTo' relationship: sets $id_or_obj's foreign_key to be $other_model_id_or_obj's primary_key. If |
||
2419 | * $otherModelObject has no ID, it is first saved. |
||
2420 | * 'hasMany' relationship: sets $other_model_id_or_obj's foreign_key to be $id_or_obj's primary_key. If $id_or_obj |
||
2421 | * has no ID, it is first saved. |
||
2422 | * 'hasAndBelongsToMany' relationships: checks that there isn't already an entry in the join table, and adds one. |
||
2423 | * If one of the model Objects has not yet been saved to the database, it is saved before adding the entry in the |
||
2424 | * join table |
||
2425 | * |
||
2426 | * @param EE_Base_Class /int $thisModelObject |
||
2427 | * @param EE_Base_Class /int $id_or_obj EE_base_Class or ID of other Model Object |
||
2428 | * @param string $relationName , key in EEM_Base::_relations |
||
2429 | * an attendee to a group, you also want to specify which role they |
||
2430 | * will have in that group. So you would use this parameter to |
||
2431 | * specify array('role-column-name'=>'role-id') |
||
2432 | * @param array $extra_join_model_fields_n_values This allows you to enter further query params for the relation |
||
2433 | * to for relation to methods that allow you to further specify |
||
2434 | * extra columns to join by (such as HABTM). Keep in mind that the |
||
2435 | * only acceptable query_params is strict "col" => "value" pairs |
||
2436 | * because these will be inserted in any new rows created as well. |
||
2437 | * @return EE_Base_Class which was added as a relation. Object referred to by $other_model_id_or_obj |
||
2438 | * @throws \EE_Error |
||
2439 | */ |
||
2440 | public function add_relationship_to( |
||
2449 | |||
2450 | |||
2451 | |||
2452 | /** |
||
2453 | * Removes a relationship of the correct type between $modelObject and $otherModelObject. |
||
2454 | * There are the 3 cases: |
||
2455 | * 'belongsTo' relationship: sets $modelObject's foreign_key to null, if that field is nullable.Otherwise throws an |
||
2456 | * error |
||
2457 | * 'hasMany' relationship: sets $otherModelObject's foreign_key to null,if that field is nullable.Otherwise throws |
||
2458 | * an error |
||
2459 | * 'hasAndBelongsToMany' relationships:removes any existing entry in the join table between the two models. |
||
2460 | * |
||
2461 | * @param EE_Base_Class /int $id_or_obj |
||
2462 | * @param EE_Base_Class /int $other_model_id_or_obj EE_Base_Class or ID of other Model Object |
||
2463 | * @param string $relationName key in EEM_Base::_relations |
||
2464 | * @return boolean of success |
||
2465 | * @throws \EE_Error |
||
2466 | * @param array $where_query This allows you to enter further query params for the relation to for relation to |
||
2467 | * methods that allow you to further specify extra columns to join by (such as HABTM). |
||
2468 | * Keep in mind that the only acceptable query_params is strict "col" => "value" pairs |
||
2469 | * because these will be inserted in any new rows created as well. |
||
2470 | */ |
||
2471 | public function remove_relationship_to($id_or_obj, $other_model_id_or_obj, $relationName, $where_query = array()) |
||
2476 | |||
2477 | |||
2478 | |||
2479 | /** |
||
2480 | * @param mixed $id_or_obj |
||
2481 | * @param string $relationName |
||
2482 | * @param array $where_query_params |
||
2483 | * @param EE_Base_Class[] objects to which relations were removed |
||
2484 | * @return \EE_Base_Class[] |
||
2485 | * @throws \EE_Error |
||
2486 | */ |
||
2487 | public function remove_relations($id_or_obj, $relationName, $where_query_params = array()) |
||
2492 | |||
2493 | |||
2494 | |||
2495 | /** |
||
2496 | * Gets all the related items of the specified $model_name, using $query_params. |
||
2497 | * Note: by default, we remove the "default query params" |
||
2498 | * because we want to get even deleted items etc. |
||
2499 | * |
||
2500 | * @param mixed $id_or_obj EE_Base_Class child or its ID |
||
2501 | * @param string $model_name like 'Event', 'Registration', etc. always singular |
||
2502 | * @param array $query_params like EEM_Base::get_all |
||
2503 | * @return EE_Base_Class[] |
||
2504 | * @throws \EE_Error |
||
2505 | */ |
||
2506 | public function get_all_related($id_or_obj, $model_name, $query_params = null) |
||
2512 | |||
2513 | |||
2514 | |||
2515 | /** |
||
2516 | * Deletes all the model objects across the relation indicated by $model_name |
||
2517 | * which are related to $id_or_obj which meet the criteria set in $query_params. |
||
2518 | * However, if the model objects can't be deleted because of blocking related model objects, then |
||
2519 | * they aren't deleted. (Unless the thing that would have been deleted can be soft-deleted, that still happens). |
||
2520 | * |
||
2521 | * @param EE_Base_Class|int|string $id_or_obj |
||
2522 | * @param string $model_name |
||
2523 | * @param array $query_params |
||
2524 | * @return int how many deleted |
||
2525 | * @throws \EE_Error |
||
2526 | */ |
||
2527 | public function delete_related($id_or_obj, $model_name, $query_params = array()) |
||
2533 | |||
2534 | |||
2535 | |||
2536 | /** |
||
2537 | * Hard deletes all the model objects across the relation indicated by $model_name |
||
2538 | * which are related to $id_or_obj which meet the criteria set in $query_params. If |
||
2539 | * the model objects can't be hard deleted because of blocking related model objects, |
||
2540 | * just does a soft-delete on them instead. |
||
2541 | * |
||
2542 | * @param EE_Base_Class|int|string $id_or_obj |
||
2543 | * @param string $model_name |
||
2544 | * @param array $query_params |
||
2545 | * @return int how many deleted |
||
2546 | * @throws \EE_Error |
||
2547 | */ |
||
2548 | public function delete_related_permanently($id_or_obj, $model_name, $query_params = array()) |
||
2554 | |||
2555 | |||
2556 | |||
2557 | /** |
||
2558 | * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
||
2559 | * unless otherwise specified in the $query_params |
||
2560 | * |
||
2561 | * @param int /EE_Base_Class $id_or_obj |
||
2562 | * @param string $model_name like 'Event', or 'Registration' |
||
2563 | * @param array $query_params like EEM_Base::get_all's |
||
2564 | * @param string $field_to_count name of field to count by. By default, uses primary key |
||
2565 | * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
||
2566 | * that by the setting $distinct to TRUE; |
||
2567 | * @return int |
||
2568 | * @throws \EE_Error |
||
2569 | */ |
||
2570 | public function count_related( |
||
2588 | |||
2589 | |||
2590 | |||
2591 | /** |
||
2592 | * Instead of getting the related model objects, simply sums up the values of the specified field. |
||
2593 | * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
||
2594 | * |
||
2595 | * @param int /EE_Base_Class $id_or_obj |
||
2596 | * @param string $model_name like 'Event', or 'Registration' |
||
2597 | * @param array $query_params like EEM_Base::get_all's |
||
2598 | * @param string $field_to_sum name of field to count by. By default, uses primary key |
||
2599 | * @return float |
||
2600 | * @throws \EE_Error |
||
2601 | */ |
||
2602 | public function sum_related($id_or_obj, $model_name, $query_params, $field_to_sum = null) |
||
2621 | |||
2622 | |||
2623 | |||
2624 | /** |
||
2625 | * Uses $this->_relatedModels info to find the first related model object of relation $relationName to the given |
||
2626 | * $modelObject |
||
2627 | * |
||
2628 | * @param int | EE_Base_Class $id_or_obj EE_Base_Class child or its ID |
||
2629 | * @param string $other_model_name , key in $this->_relatedModels, eg 'Registration', or 'Events' |
||
2630 | * @param array $query_params like EEM_Base::get_all's |
||
2631 | * @return EE_Base_Class |
||
2632 | * @throws \EE_Error |
||
2633 | */ |
||
2634 | public function get_first_related(EE_Base_Class $id_or_obj, $other_model_name, $query_params) |
||
2644 | |||
2645 | |||
2646 | |||
2647 | /** |
||
2648 | * Gets the model's name as it's expected in queries. For example, if this is EEM_Event model, that would be Event |
||
2649 | * |
||
2650 | * @return string |
||
2651 | */ |
||
2652 | public function get_this_model_name() |
||
2656 | |||
2657 | |||
2658 | |||
2659 | /** |
||
2660 | * Gets the model field on this model which is of type EE_Any_Foreign_Model_Name_Field |
||
2661 | * |
||
2662 | * @return EE_Any_Foreign_Model_Name_Field |
||
2663 | * @throws EE_Error |
||
2664 | */ |
||
2665 | public function get_field_containing_related_model_name() |
||
2678 | |||
2679 | |||
2680 | |||
2681 | /** |
||
2682 | * Inserts a new entry into the database, for each table. |
||
2683 | * Note: does not add the item to the entity map because that is done by EE_Base_Class::save() right after this. |
||
2684 | * If client code uses EEM_Base::insert() directly, then although the item isn't in the entity map, |
||
2685 | * we also know there is no model object with the newly inserted item's ID at the moment (because |
||
2686 | * if there were, then they would already be in the DB and this would fail); and in the future if someone |
||
2687 | * creates a model object with this ID (or grabs it from the DB) then it will be added to the |
||
2688 | * entity map at that time anyways. SO, no need for EEM_Base::insert ot add to the entity map |
||
2689 | * |
||
2690 | * @param array $field_n_values keys are field names, values are their values (in the client code's domain if |
||
2691 | * $values_already_prepared_by_model_object is false, in the model object's domain if |
||
2692 | * $values_already_prepared_by_model_object is true. See comment about this at the top |
||
2693 | * of EEM_Base) |
||
2694 | * @return int new primary key on main table that got inserted |
||
2695 | * @throws EE_Error |
||
2696 | */ |
||
2697 | public function insert($field_n_values) |
||
2727 | |||
2728 | |||
2729 | |||
2730 | /** |
||
2731 | * Checks that the result would satisfy the unique indexes on this model |
||
2732 | * |
||
2733 | * @param array $field_n_values |
||
2734 | * @param string $action |
||
2735 | * @return boolean |
||
2736 | * @throws \EE_Error |
||
2737 | */ |
||
2738 | protected function _satisfies_unique_indexes($field_n_values, $action = 'insert') |
||
2764 | |||
2765 | |||
2766 | |||
2767 | /** |
||
2768 | * Checks the database for an item that conflicts (ie, if this item were |
||
2769 | * saved to the DB would break some uniqueness requirement, like a primary key |
||
2770 | * or an index primary key set) with the item specified. $id_obj_or_fields_array |
||
2771 | * can be either an EE_Base_Class or an array of fields n values |
||
2772 | * |
||
2773 | * @param EE_Base_Class|array $obj_or_fields_array |
||
2774 | * @param boolean $include_primary_key whether to use the model object's primary key |
||
2775 | * when looking for conflicts |
||
2776 | * (ie, if false, we ignore the model object's primary key |
||
2777 | * when finding "conflicts". If true, it's also considered). |
||
2778 | * Only works for INT primary key, |
||
2779 | * STRING primary keys cannot be ignored |
||
2780 | * @throws EE_Error |
||
2781 | * @return EE_Base_Class|array |
||
2782 | */ |
||
2783 | public function get_one_conflicting($obj_or_fields_array, $include_primary_key = true) |
||
2822 | |||
2823 | |||
2824 | |||
2825 | /** |
||
2826 | * Like count, but is optimized and returns a boolean instead of an int |
||
2827 | * |
||
2828 | * @param array $query_params |
||
2829 | * @return boolean |
||
2830 | * @throws \EE_Error |
||
2831 | */ |
||
2832 | public function exists($query_params) |
||
2837 | |||
2838 | |||
2839 | |||
2840 | /** |
||
2841 | * Wrapper for exists, except ignores default query parameters so we're only considering ID |
||
2842 | * |
||
2843 | * @param int|string $id |
||
2844 | * @return boolean |
||
2845 | * @throws \EE_Error |
||
2846 | */ |
||
2847 | public function exists_by_ID($id) |
||
2858 | |||
2859 | |||
2860 | |||
2861 | /** |
||
2862 | * Inserts a new row in $table, using the $cols_n_values which apply to that table. |
||
2863 | * If a $new_id is supplied and if $table is an EE_Other_Table, we assume |
||
2864 | * we need to add a foreign key column to point to $new_id (which should be the primary key's value |
||
2865 | * on the main table) |
||
2866 | * This is protected rather than private because private is not accessible to any child methods and there MAY be |
||
2867 | * cases where we want to call it directly rather than via insert(). |
||
2868 | * |
||
2869 | * @access protected |
||
2870 | * @param EE_Table_Base $table |
||
2871 | * @param array $fields_n_values each key should be in field's keys, and value should be an int, string or |
||
2872 | * float |
||
2873 | * @param int $new_id for now we assume only int keys |
||
2874 | * @throws EE_Error |
||
2875 | * @global WPDB $wpdb only used to get the $wpdb->insert_id after performing an insert |
||
2876 | * @return int ID of new row inserted, or FALSE on failure |
||
2877 | */ |
||
2878 | protected function _insert_into_specific_table(EE_Table_Base $table, $fields_n_values, $new_id = 0) |
||
2923 | |||
2924 | |||
2925 | |||
2926 | /** |
||
2927 | * Prepare the $field_obj 's value in $fields_n_values for use in the database. |
||
2928 | * If the field doesn't allow NULL, try to use its default. (If it doesn't allow NULL, |
||
2929 | * and there is no default, we pass it along. WPDB will take care of it) |
||
2930 | * |
||
2931 | * @param EE_Model_Field_Base $field_obj |
||
2932 | * @param array $fields_n_values |
||
2933 | * @return mixed string|int|float depending on what the table column will be expecting |
||
2934 | * @throws \EE_Error |
||
2935 | */ |
||
2936 | protected function _prepare_value_or_use_default($field_obj, $fields_n_values) |
||
2953 | |||
2954 | |||
2955 | |||
2956 | /** |
||
2957 | * Consolidates code for preparing a value supplied to the model for use int eh db. Calls the field's |
||
2958 | * prepare_for_use_in_db method on the value, and depending on $value_already_prepare_by_model_obj, may also call |
||
2959 | * the field's prepare_for_set() method. |
||
2960 | * |
||
2961 | * @param mixed $value value in the client code domain if $value_already_prepared_by_model_object is |
||
2962 | * false, otherwise a value in the model object's domain (see lengthy comment at |
||
2963 | * top of file) |
||
2964 | * @param EE_Model_Field_Base $field field which will be doing the preparing of the value. If null, we assume |
||
2965 | * $value is a custom selection |
||
2966 | * @return mixed a value ready for use in the database for insertions, updating, or in a where clause |
||
2967 | */ |
||
2968 | private function _prepare_value_for_use_in_db($value, $field) |
||
2986 | |||
2987 | |||
2988 | |||
2989 | /** |
||
2990 | * Returns the main table on this model |
||
2991 | * |
||
2992 | * @return EE_Primary_Table |
||
2993 | * @throws EE_Error |
||
2994 | */ |
||
2995 | protected function _get_main_table() |
||
3005 | |||
3006 | |||
3007 | |||
3008 | /** |
||
3009 | * table |
||
3010 | * returns EE_Primary_Table table name |
||
3011 | * |
||
3012 | * @return string |
||
3013 | * @throws \EE_Error |
||
3014 | */ |
||
3015 | public function table() |
||
3019 | |||
3020 | |||
3021 | |||
3022 | /** |
||
3023 | * table |
||
3024 | * returns first EE_Secondary_Table table name |
||
3025 | * |
||
3026 | * @return string |
||
3027 | */ |
||
3028 | public function second_table() |
||
3034 | |||
3035 | |||
3036 | |||
3037 | /** |
||
3038 | * get_table_obj_by_alias |
||
3039 | * returns table name given it's alias |
||
3040 | * |
||
3041 | * @param string $table_alias |
||
3042 | * @return EE_Primary_Table | EE_Secondary_Table |
||
3043 | */ |
||
3044 | public function get_table_obj_by_alias($table_alias = '') |
||
3048 | |||
3049 | |||
3050 | |||
3051 | /** |
||
3052 | * Gets all the tables of type EE_Other_Table from EEM_CPT_Basel_Model::_tables |
||
3053 | * |
||
3054 | * @return EE_Secondary_Table[] |
||
3055 | */ |
||
3056 | protected function _get_other_tables() |
||
3066 | |||
3067 | |||
3068 | |||
3069 | /** |
||
3070 | * Finds all the fields that correspond to the given table |
||
3071 | * |
||
3072 | * @param string $table_alias , array key in EEM_Base::_tables |
||
3073 | * @return EE_Model_Field_Base[] |
||
3074 | */ |
||
3075 | public function _get_fields_for_table($table_alias) |
||
3079 | |||
3080 | |||
3081 | |||
3082 | /** |
||
3083 | * Recurses through all the where parameters, and finds all the related models we'll need |
||
3084 | * to complete this query. Eg, given where parameters like array('EVT_ID'=>3) from within Event model, we won't |
||
3085 | * need any related models. But if the array were array('Registrations.REG_ID'=>3), we'd need the related |
||
3086 | * Registration model. If it were array('Registrations.Transactions.Payments.PAY_ID'=>3), then we'd need the |
||
3087 | * related Registration, Transaction, and Payment models. |
||
3088 | * |
||
3089 | * @param array $query_params like EEM_Base::get_all's $query_parameters['where'] |
||
3090 | * @return EE_Model_Query_Info_Carrier |
||
3091 | * @throws \EE_Error |
||
3092 | */ |
||
3093 | public function _extract_related_models_from_query($query_params) |
||
3145 | |||
3146 | |||
3147 | |||
3148 | /** |
||
3149 | * For extracting related models from WHERE (0), HAVING (having), ORDER BY (order_by) or forced joins (force_join) |
||
3150 | * |
||
3151 | * @param array $sub_query_params like EEM_Base::get_all's $query_params[0] or |
||
3152 | * $query_params['having'] |
||
3153 | * @param EE_Model_Query_Info_Carrier $model_query_info_carrier |
||
3154 | * @param string $query_param_type one of $this->_allowed_query_params |
||
3155 | * @throws EE_Error |
||
3156 | * @return \EE_Model_Query_Info_Carrier |
||
3157 | */ |
||
3158 | private function _extract_related_models_from_sub_params_array_keys( |
||
3202 | |||
3203 | |||
3204 | |||
3205 | /** |
||
3206 | * For extracting related models from forced_joins, where the array values contain the info about what |
||
3207 | * models to join with. Eg an array like array('Attendee','Price.Price_Type'); |
||
3208 | * |
||
3209 | * @param array $sub_query_params like EEM_Base::get_all's $query_params[0] or |
||
3210 | * $query_params['having'] |
||
3211 | * @param EE_Model_Query_Info_Carrier $model_query_info_carrier |
||
3212 | * @param string $query_param_type one of $this->_allowed_query_params |
||
3213 | * @throws EE_Error |
||
3214 | * @return \EE_Model_Query_Info_Carrier |
||
3215 | */ |
||
3216 | private function _extract_related_models_from_sub_params_array_values( |
||
3234 | |||
3235 | |||
3236 | |||
3237 | /** |
||
3238 | * Extract all the query parts from $query_params (an array like whats passed to EEM_Base::get_all) |
||
3239 | * and put into a EEM_Related_Model_Info_Carrier for easy extraction into a query. We create this object |
||
3240 | * instead of directly constructing the SQL because often we need to extract info from the $query_params |
||
3241 | * but use them in a different order. Eg, we need to know what models we are querying |
||
3242 | * before we know what joins to perform. However, we need to know what data types correspond to which fields on |
||
3243 | * other models before we can finalize the where clause SQL. |
||
3244 | * |
||
3245 | * @param array $query_params |
||
3246 | * @throws EE_Error |
||
3247 | * @return EE_Model_Query_Info_Carrier |
||
3248 | */ |
||
3249 | public function _create_model_query_info_carrier($query_params) |
||
3441 | |||
3442 | |||
3443 | |||
3444 | /** |
||
3445 | * Gets the where conditions that should be imposed on the query based on the |
||
3446 | * context (eg reading frontend, backend, edit or delete). |
||
3447 | * |
||
3448 | * @param string $context one of EEM_Base::valid_cap_contexts() |
||
3449 | * @return array like EEM_Base::get_all() 's $query_params[0] |
||
3450 | * @throws \EE_Error |
||
3451 | */ |
||
3452 | public function caps_where_conditions($context = self::caps_read) |
||
3467 | |||
3468 | |||
3469 | |||
3470 | /** |
||
3471 | * Verifies that $should_be_order_string is in $this->_allowed_order_values, |
||
3472 | * otherwise throws an exception |
||
3473 | * |
||
3474 | * @param string $should_be_order_string |
||
3475 | * @return string either ASC, asc, DESC or desc |
||
3476 | * @throws EE_Error |
||
3477 | */ |
||
3478 | View Code Duplication | private function _extract_order($should_be_order_string) |
|
3487 | |||
3488 | |||
3489 | |||
3490 | /** |
||
3491 | * Looks at all the models which are included in this query, and asks each |
||
3492 | * for their universal_where_params, and returns them in the same format as $query_params[0] (where), |
||
3493 | * so they can be merged |
||
3494 | * |
||
3495 | * @param EE_Model_Query_Info_Carrier $query_info_carrier |
||
3496 | * @param string $use_default_where_conditions can be 'none','other_models_only', or 'all'. |
||
3497 | * 'none' means NO default where conditions will |
||
3498 | * be used AT ALL during this query. |
||
3499 | * 'other_models_only' means default where |
||
3500 | * conditions from other models will be used, but |
||
3501 | * not for this primary model. 'all', the default, |
||
3502 | * means default where conditions will apply as |
||
3503 | * normal |
||
3504 | * @param array $where_query_params like EEM_Base::get_all's $query_params[0] |
||
3505 | * @throws EE_Error |
||
3506 | * @return array like $query_params[0], see EEM_Base::get_all for documentation |
||
3507 | */ |
||
3508 | private function _get_default_where_conditions_for_models_in_query( |
||
3548 | |||
3549 | |||
3550 | |||
3551 | /** |
||
3552 | * Determines whether or not we should use default where conditions for the model in question |
||
3553 | * (this model, or other related models). |
||
3554 | * Basically, we should use default where conditions on this model if they have requested to use them on all models, |
||
3555 | * this model only, or to use minimum where conditions on all other models and normal where conditions on this one. |
||
3556 | * We should use default where conditions on related models when they requested to use default where conditions |
||
3557 | * on all models, or specifically just on other related models |
||
3558 | * @param $default_where_conditions_value |
||
3559 | * @param bool $for_this_model false means this is for OTHER related models |
||
3560 | * @return bool |
||
3561 | */ |
||
3562 | private function _should_use_default_where_conditions( $default_where_conditions_value, $for_this_model = true ) |
||
3588 | |||
3589 | /** |
||
3590 | * Determines whether or not we should use default minimum conditions for the model in question |
||
3591 | * (this model, or other related models). |
||
3592 | * Basically, we should use minimum where conditions on this model only if they requested all models to use minimum |
||
3593 | * where conditions. |
||
3594 | * We should use minimum where conditions on related models if they requested to use minimum where conditions |
||
3595 | * on this model or others |
||
3596 | * @param $default_where_conditions_value |
||
3597 | * @param bool $for_this_model false means this is for OTHER related models |
||
3598 | * @return bool |
||
3599 | */ |
||
3600 | private function _should_use_minimum_where_conditions($default_where_conditions_value, $for_this_model = true) |
||
3618 | |||
3619 | |||
3620 | /** |
||
3621 | * Checks if any of the defaults have been overridden. If there are any that AREN'T overridden, |
||
3622 | * then we also add a special where condition which allows for that model's primary key |
||
3623 | * to be null (which is important for JOINs. Eg, if you want to see all Events ordered by Venue's name, |
||
3624 | * then Event's with NO Venue won't appear unless you allow VNU_ID to be NULL) |
||
3625 | * |
||
3626 | * @param array $default_where_conditions |
||
3627 | * @param array $provided_where_conditions |
||
3628 | * @param EEM_Base $model |
||
3629 | * @param string $model_relation_path like 'Transaction.Payment.' |
||
3630 | * @return array like EEM_Base::get_all's $query_params[0] |
||
3631 | * @throws \EE_Error |
||
3632 | */ |
||
3633 | private function _override_defaults_or_make_null_friendly( |
||
3660 | |||
3661 | |||
3662 | |||
3663 | /** |
||
3664 | * Uses the _default_where_conditions_strategy set during __construct() to get |
||
3665 | * default where conditions on all get_all, update, and delete queries done by this model. |
||
3666 | * Use the same syntax as client code. Eg on the Event model, use array('Event.EVT_post_type'=>'esp_event'), |
||
3667 | * NOT array('Event_CPT.post_type'=>'esp_event'). |
||
3668 | * |
||
3669 | * @param string $model_relation_path eg, path from Event to Payment is "Registration.Transaction.Payment." |
||
3670 | * @return array like EEM_Base::get_all's $query_params[0] (where conditions) |
||
3671 | */ |
||
3672 | private function _get_default_where_conditions($model_relation_path = null) |
||
3679 | |||
3680 | |||
3681 | |||
3682 | /** |
||
3683 | * Uses the _minimum_where_conditions_strategy set during __construct() to get |
||
3684 | * minimum where conditions on all get_all, update, and delete queries done by this model. |
||
3685 | * Use the same syntax as client code. Eg on the Event model, use array('Event.EVT_post_type'=>'esp_event'), |
||
3686 | * NOT array('Event_CPT.post_type'=>'esp_event'). |
||
3687 | * Similar to _get_default_where_conditions |
||
3688 | * |
||
3689 | * @param string $model_relation_path eg, path from Event to Payment is "Registration.Transaction.Payment." |
||
3690 | * @return array like EEM_Base::get_all's $query_params[0] (where conditions) |
||
3691 | */ |
||
3692 | protected function _get_minimum_where_conditions($model_relation_path = null) |
||
3699 | |||
3700 | |||
3701 | |||
3702 | /** |
||
3703 | * Creates the string of SQL for the select part of a select query, everything behind SELECT and before FROM. |
||
3704 | * Eg, "Event.post_id, Event.post_name,Event_Detail.EVT_ID..." |
||
3705 | * |
||
3706 | * @param EE_Model_Query_Info_Carrier $model_query_info |
||
3707 | * @return string |
||
3708 | * @throws \EE_Error |
||
3709 | */ |
||
3710 | private function _construct_default_select_sql(EE_Model_Query_Info_Carrier $model_query_info) |
||
3725 | |||
3726 | |||
3727 | |||
3728 | /** |
||
3729 | * Gets an array of columns to select for this model, which are necessary for it to create its objects. |
||
3730 | * So that's going to be the columns for all the fields on the model |
||
3731 | * |
||
3732 | * @param string $model_relation_chain like 'Question.Question_Group.Event' |
||
3733 | * @return array numerically indexed, values are columns to select and rename, eg "Event.ID AS 'Event.ID'" |
||
3734 | */ |
||
3735 | public function _get_columns_to_select_for_this_model($model_relation_chain = '') |
||
3766 | |||
3767 | |||
3768 | |||
3769 | /** |
||
3770 | * Given a $query_param like 'Registration.Transaction.TXN_ID', pops off 'Registration.', |
||
3771 | * gets the join statement for it; gets the data types for it; and passes the remaining 'Transaction.TXN_ID' |
||
3772 | * onto its related Transaction object to do the same. Returns an EE_Join_And_Data_Types object which contains the |
||
3773 | * SQL for joining, and the data types |
||
3774 | * |
||
3775 | * @param null|string $original_query_param |
||
3776 | * @param string $query_param like Registration.Transaction.TXN_ID |
||
3777 | * @param EE_Model_Query_Info_Carrier $passed_in_query_info |
||
3778 | * @param string $query_param_type like Registration.Transaction.TXN_ID |
||
3779 | * or 'PAY_ID'. Otherwise, we don't expect there to be a |
||
3780 | * column name. We only want model names, eg 'Event.Venue' |
||
3781 | * or 'Registration's |
||
3782 | * @param string $original_query_param what it originally was (eg |
||
3783 | * Registration.Transaction.TXN_ID). If null, we assume it |
||
3784 | * matches $query_param |
||
3785 | * @throws EE_Error |
||
3786 | * @return void only modifies the EEM_Related_Model_Info_Carrier passed into it |
||
3787 | */ |
||
3788 | private function _extract_related_model_info_from_query_param( |
||
3872 | |||
3873 | |||
3874 | |||
3875 | /** |
||
3876 | * Privately used by _extract_related_model_info_from_query_param to add a join to $model_name |
||
3877 | * and store it on $passed_in_query_info |
||
3878 | * |
||
3879 | * @param string $model_name |
||
3880 | * @param EE_Model_Query_Info_Carrier $passed_in_query_info |
||
3881 | * @param string $original_query_param used to extract the relation chain between the queried |
||
3882 | * model and $model_name. Eg, if we are querying Event, |
||
3883 | * and are adding a join to 'Payment' with the original |
||
3884 | * query param key |
||
3885 | * 'Registration.Transaction.Payment.PAY_amount', we want |
||
3886 | * to extract 'Registration.Transaction.Payment', in case |
||
3887 | * Payment wants to add default query params so that it |
||
3888 | * will know what models to prepend onto its default query |
||
3889 | * params or in case it wants to rename tables (in case |
||
3890 | * there are multiple joins to the same table) |
||
3891 | * @return void |
||
3892 | * @throws \EE_Error |
||
3893 | */ |
||
3894 | private function _add_join_to_model( |
||
3919 | |||
3920 | |||
3921 | |||
3922 | /** |
||
3923 | * Constructs SQL for where clause, like "WHERE Event.ID = 23 AND Transaction.amount > 100" etc. |
||
3924 | * |
||
3925 | * @param array $where_params like EEM_Base::get_all |
||
3926 | * @return string of SQL |
||
3927 | * @throws \EE_Error |
||
3928 | */ |
||
3929 | private function _construct_where_clause($where_params) |
||
3938 | |||
3939 | |||
3940 | |||
3941 | /** |
||
3942 | * Just like the _construct_where_clause, except prepends 'HAVING' instead of 'WHERE', |
||
3943 | * and should be passed HAVING parameters, not WHERE parameters |
||
3944 | * |
||
3945 | * @param array $having_params |
||
3946 | * @return string |
||
3947 | * @throws \EE_Error |
||
3948 | */ |
||
3949 | private function _construct_having_clause($having_params) |
||
3958 | |||
3959 | |||
3960 | |||
3961 | /** |
||
3962 | * Gets the EE_Model_Field on the model indicated by $model_name and the $field_name. |
||
3963 | * Eg, if called with _get_field_on_model('ATT_ID','Attendee'), it will return the EE_Primary_Key_Field on |
||
3964 | * EEM_Attendee. |
||
3965 | * |
||
3966 | * @param string $field_name |
||
3967 | * @param string $model_name |
||
3968 | * @return EE_Model_Field_Base |
||
3969 | * @throws EE_Error |
||
3970 | */ |
||
3971 | protected function _get_field_on_model($field_name, $model_name) |
||
3985 | |||
3986 | |||
3987 | |||
3988 | /** |
||
3989 | * Used for creating nested WHERE conditions. Eg "WHERE ! (Event.ID = 3 OR ( Event_Meta.meta_key = 'bob' AND |
||
3990 | * Event_Meta.meta_value = 'foo'))" |
||
3991 | * |
||
3992 | * @param array $where_params see EEM_Base::get_all for documentation |
||
3993 | * @param string $glue joins each subclause together. Should really only be " AND " or " OR "... |
||
3994 | * @throws EE_Error |
||
3995 | * @return string of SQL |
||
3996 | */ |
||
3997 | private function _construct_condition_clause_recursive($where_params, $glue = ' AND') |
||
4043 | |||
4044 | |||
4045 | |||
4046 | /** |
||
4047 | * Takes the input parameter and extract the table name (alias) and column name |
||
4048 | * |
||
4049 | * @param array $query_param like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
||
4050 | * @throws EE_Error |
||
4051 | * @return string table alias and column name for SQL, eg "Transaction.TXN_ID" |
||
4052 | */ |
||
4053 | private function _deduce_column_name_from_query_param($query_param) |
||
4069 | |||
4070 | |||
4071 | |||
4072 | /** |
||
4073 | * Removes the * and anything after it from the condition query param key. It is useful to add the * to condition |
||
4074 | * query param keys (eg, 'OR*', 'EVT_ID') in order for the array keys to still be unique, so that they don't get |
||
4075 | * overwritten Takes a string like 'Event.EVT_ID*', 'TXN_total**', 'OR*1st', and 'DTT_reg_start*foobar' to |
||
4076 | * 'Event.EVT_ID', 'TXN_total', 'OR', and 'DTT_reg_start', respectively. |
||
4077 | * |
||
4078 | * @param string $condition_query_param_key |
||
4079 | * @return string |
||
4080 | */ |
||
4081 | View Code Duplication | private function _remove_stars_and_anything_after_from_condition_query_param_key($condition_query_param_key) |
|
4091 | |||
4092 | |||
4093 | |||
4094 | /** |
||
4095 | * creates the SQL for the operator and the value in a WHERE clause, eg "< 23" or "LIKE '%monkey%'" |
||
4096 | * |
||
4097 | * @param mixed array | string $op_and_value |
||
4098 | * @param EE_Model_Field_Base|string $field_obj . If string, should be one of EEM_Base::_valid_wpdb_data_types |
||
4099 | * @throws EE_Error |
||
4100 | * @return string |
||
4101 | */ |
||
4102 | private function _construct_op_and_value($op_and_value, $field_obj) |
||
4204 | |||
4205 | |||
4206 | |||
4207 | /** |
||
4208 | * Creates the operands to be used in a BETWEEN query, eg "'2014-12-31 20:23:33' AND '2015-01-23 12:32:54'" |
||
4209 | * |
||
4210 | * @param array $values |
||
4211 | * @param EE_Model_Field_Base|string $field_obj if string, it should be the datatype to be used when querying, eg |
||
4212 | * '%s' |
||
4213 | * @return string |
||
4214 | * @throws \EE_Error |
||
4215 | */ |
||
4216 | public function _construct_between_value($values, $field_obj) |
||
4224 | |||
4225 | |||
4226 | |||
4227 | /** |
||
4228 | * Takes an array or a comma-separated list of $values and cleans them |
||
4229 | * according to $data_type using $wpdb->prepare, and then makes the list a |
||
4230 | * string surrounded by ( and ). Eg, _construct_in_value(array(1,2,3),'%d') would |
||
4231 | * return '(1,2,3)'; _construct_in_value("1,2,hack",'%d') would return '(1,2,1)' (assuming |
||
4232 | * I'm right that a string, when interpreted as a digit, becomes a 1. It might become a 0) |
||
4233 | * |
||
4234 | * @param mixed $values array or comma-separated string |
||
4235 | * @param EE_Model_Field_Base|string $field_obj if string, it should be a wpdb data type like '%s', or '%d' |
||
4236 | * @return string of SQL to follow an 'IN' or 'NOT IN' operator |
||
4237 | * @throws \EE_Error |
||
4238 | */ |
||
4239 | public function _construct_in_value($values, $field_obj) |
||
4265 | |||
4266 | |||
4267 | |||
4268 | /** |
||
4269 | * @param mixed $value |
||
4270 | * @param EE_Model_Field_Base|string $field_obj if string it should be a wpdb data type like '%d' |
||
4271 | * @throws EE_Error |
||
4272 | * @return false|null|string |
||
4273 | */ |
||
4274 | private function _wpdb_prepare_using_field($value, $field_obj) |
||
4289 | |||
4290 | |||
4291 | |||
4292 | /** |
||
4293 | * Takes the input parameter and finds the model field that it indicates. |
||
4294 | * |
||
4295 | * @param string $query_param_name like Registration.Transaction.TXN_ID, Event.Datetime.start_time, or REG_ID |
||
4296 | * @throws EE_Error |
||
4297 | * @return EE_Model_Field_Base |
||
4298 | */ |
||
4299 | protected function _deduce_field_from_query_param($query_param_name) |
||
4324 | |||
4325 | |||
4326 | |||
4327 | /** |
||
4328 | * Given a field's name (ie, a key in $this->field_settings()), uses the EE_Model_Field object to get the table's |
||
4329 | * alias and column which corresponds to it |
||
4330 | * |
||
4331 | * @param string $field_name |
||
4332 | * @throws EE_Error |
||
4333 | * @return string |
||
4334 | */ |
||
4335 | public function _get_qualified_column_for_field($field_name) |
||
4346 | |||
4347 | |||
4348 | |||
4349 | /** |
||
4350 | * similar to \EEM_Base::_get_qualified_column_for_field() but returns an array with data for ALL fields. |
||
4351 | * Example usage: |
||
4352 | * EEM_Ticket::instance()->get_all_wpdb_results( |
||
4353 | * array(), |
||
4354 | * ARRAY_A, |
||
4355 | * EEM_Ticket::instance()->get_qualified_columns_for_all_fields() |
||
4356 | * ); |
||
4357 | * is equivalent to |
||
4358 | * EEM_Ticket::instance()->get_all_wpdb_results( array(), ARRAY_A, '*' ); |
||
4359 | * and |
||
4360 | * EEM_Event::instance()->get_all_wpdb_results( |
||
4361 | * array( |
||
4362 | * array( |
||
4363 | * 'Datetime.Ticket.TKT_ID' => array( '<', 100 ), |
||
4364 | * ), |
||
4365 | * ARRAY_A, |
||
4366 | * implode( |
||
4367 | * ', ', |
||
4368 | * array_merge( |
||
4369 | * EEM_Event::instance()->get_qualified_columns_for_all_fields( '', false ), |
||
4370 | * EEM_Ticket::instance()->get_qualified_columns_for_all_fields( 'Datetime', false ) |
||
4371 | * ) |
||
4372 | * ) |
||
4373 | * ) |
||
4374 | * ); |
||
4375 | * selects rows from the database, selecting all the event and ticket columns, where the ticket ID is below 100 |
||
4376 | * |
||
4377 | * @param string $model_relation_chain the chain of models used to join between the model you want to query |
||
4378 | * and the one whose fields you are selecting for example: when querying |
||
4379 | * tickets model and selecting fields from the tickets model you would |
||
4380 | * leave this parameter empty, because no models are needed to join |
||
4381 | * between the queried model and the selected one. Likewise when |
||
4382 | * querying the datetime model and selecting fields from the tickets |
||
4383 | * model, it would also be left empty, because there is a direct |
||
4384 | * relation from datetimes to tickets, so no model is needed to join |
||
4385 | * them together. However, when querying from the event model and |
||
4386 | * selecting fields from the ticket model, you should provide the string |
||
4387 | * 'Datetime', indicating that the event model must first join to the |
||
4388 | * datetime model in order to find its relation to ticket model. |
||
4389 | * Also, when querying from the venue model and selecting fields from |
||
4390 | * the ticket model, you should provide the string 'Event.Datetime', |
||
4391 | * indicating you need to join the venue model to the event model, |
||
4392 | * to the datetime model, in order to find its relation to the ticket model. |
||
4393 | * This string is used to deduce the prefix that gets added onto the |
||
4394 | * models' tables qualified columns |
||
4395 | * @param bool $return_string if true, will return a string with qualified column names separated |
||
4396 | * by ', ' if false, will simply return a numerically indexed array of |
||
4397 | * qualified column names |
||
4398 | * @return array|string |
||
4399 | */ |
||
4400 | public function get_qualified_columns_for_all_fields($model_relation_chain = '', $return_string = true) |
||
4409 | |||
4410 | |||
4411 | |||
4412 | /** |
||
4413 | * constructs the select use on special limit joins |
||
4414 | * NOTE: for now this has only been tested and will work when the table alias is for the PRIMARY table. Although |
||
4415 | * its setup so the select query will be setup on and just doing the special select join off of the primary table |
||
4416 | * (as that is typically where the limits would be set). |
||
4417 | * |
||
4418 | * @param string $table_alias The table the select is being built for |
||
4419 | * @param mixed|string $limit The limit for this select |
||
4420 | * @return string The final select join element for the query. |
||
4421 | */ |
||
4422 | public function _construct_limit_join_select($table_alias, $limit) |
||
4438 | |||
4439 | |||
4440 | |||
4441 | /** |
||
4442 | * Constructs the internal join if there are multiple tables, or simply the table's name and alias |
||
4443 | * Eg "wp_post AS Event" or "wp_post AS Event INNER JOIN wp_postmeta Event_Meta ON Event.ID = Event_Meta.post_id" |
||
4444 | * |
||
4445 | * @return string SQL |
||
4446 | * @throws \EE_Error |
||
4447 | */ |
||
4448 | public function _construct_internal_join() |
||
4454 | |||
4455 | |||
4456 | |||
4457 | /** |
||
4458 | * Constructs the SQL for joining all the tables on this model. |
||
4459 | * Normally $alias should be the primary table's alias, but in cases where |
||
4460 | * we have already joined to a secondary table (eg, the secondary table has a foreign key and is joined before the |
||
4461 | * primary table) then we should provide that secondary table's alias. Eg, with $alias being the primary table's |
||
4462 | * alias, this will construct SQL like: |
||
4463 | * " INNER JOIN wp_esp_secondary_table AS Secondary_Table ON Primary_Table.pk = Secondary_Table.fk". |
||
4464 | * With $alias being a secondary table's alias, this will construct SQL like: |
||
4465 | * " INNER JOIN wp_esp_primary_table AS Primary_Table ON Primary_Table.pk = Secondary_Table.fk". |
||
4466 | * |
||
4467 | * @param string $alias_prefixed table alias to join to (this table should already be in the FROM SQL clause) |
||
4468 | * @return string |
||
4469 | */ |
||
4470 | public function _construct_internal_join_to_table_with_alias($alias_prefixed) |
||
4489 | |||
4490 | |||
4491 | |||
4492 | /** |
||
4493 | * Gets an array for storing all the data types on the next-to-be-executed-query. |
||
4494 | * This should be a growing array of keys being table-columns (eg 'EVT_ID' and 'Event.EVT_ID'), and values being |
||
4495 | * their data type (eg, '%s', '%d', etc) |
||
4496 | * |
||
4497 | * @return array |
||
4498 | */ |
||
4499 | public function _get_data_types() |
||
4509 | |||
4510 | |||
4511 | |||
4512 | /** |
||
4513 | * Gets the model object given the relation's name / model's name (eg, 'Event', 'Registration',etc. Always singular) |
||
4514 | * |
||
4515 | * @param string $model_name |
||
4516 | * @throws EE_Error |
||
4517 | * @return EEM_Base |
||
4518 | */ |
||
4519 | public function get_related_model_obj($model_name) |
||
4528 | |||
4529 | |||
4530 | |||
4531 | /** |
||
4532 | * Returns the array of EE_ModelRelations for this model. |
||
4533 | * |
||
4534 | * @return EE_Model_Relation_Base[] |
||
4535 | */ |
||
4536 | public function relation_settings() |
||
4540 | |||
4541 | |||
4542 | |||
4543 | /** |
||
4544 | * Gets all related models that this model BELONGS TO. Handy to know sometimes |
||
4545 | * because without THOSE models, this model probably doesn't have much purpose. |
||
4546 | * (Eg, without an event, datetimes have little purpose.) |
||
4547 | * |
||
4548 | * @return EE_Belongs_To_Relation[] |
||
4549 | */ |
||
4550 | public function belongs_to_relations() |
||
4560 | |||
4561 | |||
4562 | |||
4563 | /** |
||
4564 | * Returns the specified EE_Model_Relation, or throws an exception |
||
4565 | * |
||
4566 | * @param string $relation_name name of relation, key in $this->_relatedModels |
||
4567 | * @throws EE_Error |
||
4568 | * @return EE_Model_Relation_Base |
||
4569 | */ |
||
4570 | public function related_settings_for($relation_name) |
||
4586 | |||
4587 | |||
4588 | |||
4589 | /** |
||
4590 | * A convenience method for getting a specific field's settings, instead of getting all field settings for all |
||
4591 | * fields |
||
4592 | * |
||
4593 | * @param string $fieldName |
||
4594 | * @throws EE_Error |
||
4595 | * @return EE_Model_Field_Base |
||
4596 | */ |
||
4597 | View Code Duplication | public function field_settings_for($fieldName) |
|
4606 | |||
4607 | |||
4608 | |||
4609 | /** |
||
4610 | * Checks if this field exists on this model |
||
4611 | * |
||
4612 | * @param string $fieldName a key in the model's _field_settings array |
||
4613 | * @return boolean |
||
4614 | */ |
||
4615 | public function has_field($fieldName) |
||
4624 | |||
4625 | |||
4626 | |||
4627 | /** |
||
4628 | * Returns whether or not this model has a relation to the specified model |
||
4629 | * |
||
4630 | * @param string $relation_name possibly one of the keys in the relation_settings array |
||
4631 | * @return boolean |
||
4632 | */ |
||
4633 | public function has_relation($relation_name) |
||
4642 | |||
4643 | |||
4644 | |||
4645 | /** |
||
4646 | * gets the field object of type 'primary_key' from the fieldsSettings attribute. |
||
4647 | * Eg, on EE_Answer that would be ANS_ID field object |
||
4648 | * |
||
4649 | * @param $field_obj |
||
4650 | * @return boolean |
||
4651 | */ |
||
4652 | public function is_primary_key_field($field_obj) |
||
4656 | |||
4657 | |||
4658 | |||
4659 | /** |
||
4660 | * gets the field object of type 'primary_key' from the fieldsSettings attribute. |
||
4661 | * Eg, on EE_Answer that would be ANS_ID field object |
||
4662 | * |
||
4663 | * @return EE_Model_Field_Base |
||
4664 | * @throws EE_Error |
||
4665 | */ |
||
4666 | public function get_primary_key_field() |
||
4682 | |||
4683 | |||
4684 | |||
4685 | /** |
||
4686 | * Returns whether or not not there is a primary key on this model. |
||
4687 | * Internally does some caching. |
||
4688 | * |
||
4689 | * @return boolean |
||
4690 | */ |
||
4691 | public function has_primary_key_field() |
||
4703 | |||
4704 | |||
4705 | |||
4706 | /** |
||
4707 | * Finds the first field of type $field_class_name. |
||
4708 | * |
||
4709 | * @param string $field_class_name class name of field that you want to find. Eg, EE_Datetime_Field, |
||
4710 | * EE_Foreign_Key_Field, etc |
||
4711 | * @return EE_Model_Field_Base or null if none is found |
||
4712 | */ |
||
4713 | public function get_a_field_of_type($field_class_name) |
||
4722 | |||
4723 | |||
4724 | |||
4725 | /** |
||
4726 | * Gets a foreign key field pointing to model. |
||
4727 | * |
||
4728 | * @param string $model_name eg Event, Registration, not EEM_Event |
||
4729 | * @return EE_Foreign_Key_Field_Base |
||
4730 | * @throws EE_Error |
||
4731 | */ |
||
4732 | public function get_foreign_key_to($model_name) |
||
4751 | |||
4752 | |||
4753 | |||
4754 | /** |
||
4755 | * Gets the actual table for the table alias |
||
4756 | * |
||
4757 | * @param string $table_alias eg Event, Event_Meta, Registration, Transaction, but maybe |
||
4758 | * a table alias with a model chain prefix, like 'Venue__Event_Venue___Event_Meta'. |
||
4759 | * Either one works |
||
4760 | * @return EE_Table_Base |
||
4761 | */ |
||
4762 | public function get_table_for_alias($table_alias) |
||
4767 | |||
4768 | |||
4769 | |||
4770 | /** |
||
4771 | * Returns a flat array of all field son this model, instead of organizing them |
||
4772 | * by table_alias as they are in the constructor. |
||
4773 | * |
||
4774 | * @param bool $include_db_only_fields flag indicating whether or not to include the db-only fields |
||
4775 | * @return EE_Model_Field_Base[] where the keys are the field's name |
||
4776 | */ |
||
4777 | public function field_settings($include_db_only_fields = false) |
||
4804 | |||
4805 | |||
4806 | |||
4807 | /** |
||
4808 | * cycle though array of attendees and create objects out of each item |
||
4809 | * |
||
4810 | * @access private |
||
4811 | * @param array $rows of results of $wpdb->get_results($query,ARRAY_A) |
||
4812 | * @return \EE_Base_Class[] array keys are primary keys (if there is a primary key on the model. if not, |
||
4813 | * numerically indexed) |
||
4814 | * @throws \EE_Error |
||
4815 | */ |
||
4816 | protected function _create_objects($rows = array()) |
||
4878 | |||
4879 | |||
4880 | |||
4881 | /** |
||
4882 | * The purpose of this method is to allow us to create a model object that is not in the db that holds default |
||
4883 | * values. A typical example of where this is used is when creating a new item and the initial load of a form. We |
||
4884 | * dont' necessarily want to test for if the object is present but just assume it is BUT load the defaults from the |
||
4885 | * object (as set in the model_field!). |
||
4886 | * |
||
4887 | * @return EE_Base_Class single EE_Base_Class object with default values for the properties. |
||
4888 | */ |
||
4889 | public function create_default_object() |
||
4901 | |||
4902 | |||
4903 | |||
4904 | /** |
||
4905 | * @param mixed $cols_n_values either an array of where each key is the name of a field, and the value is its value |
||
4906 | * or an stdClass where each property is the name of a column, |
||
4907 | * @return EE_Base_Class |
||
4908 | * @throws \EE_Error |
||
4909 | */ |
||
4910 | public function instantiate_class_from_array_or_object($cols_n_values) |
||
4953 | |||
4954 | |||
4955 | |||
4956 | /** |
||
4957 | * Gets the model object from the entity map if it exists |
||
4958 | * |
||
4959 | * @param int|string $id the ID of the model object |
||
4960 | * @return EE_Base_Class |
||
4961 | */ |
||
4962 | public function get_from_entity_map($id) |
||
4967 | |||
4968 | |||
4969 | |||
4970 | /** |
||
4971 | * add_to_entity_map |
||
4972 | * Adds the object to the model's entity mappings |
||
4973 | * Effectively tells the models "Hey, this model object is the most up-to-date representation of the data, |
||
4974 | * and for the remainder of the request, it's even more up-to-date than what's in the database. |
||
4975 | * So, if the database doesn't agree with what's in the entity mapper, ignore the database" |
||
4976 | * If the database gets updated directly and you want the entity mapper to reflect that change, |
||
4977 | * then this method should be called immediately after the update query |
||
4978 | * Note: The map is indexed by whatever the current blog id is set (via EEM_Base::$_model_query_blog_id). This is |
||
4979 | * so on multisite, the entity map is specific to the query being done for a specific site. |
||
4980 | * |
||
4981 | * @param EE_Base_Class $object |
||
4982 | * @throws EE_Error |
||
4983 | * @return \EE_Base_Class |
||
4984 | */ |
||
4985 | public function add_to_entity_map(EE_Base_Class $object) |
||
5006 | |||
5007 | |||
5008 | |||
5009 | /** |
||
5010 | * if a valid identifier is provided, then that entity is unset from the entity map, |
||
5011 | * if no identifier is provided, then the entire entity map is emptied |
||
5012 | * |
||
5013 | * @param int|string $id the ID of the model object |
||
5014 | * @return boolean |
||
5015 | */ |
||
5016 | public function clear_entity_map($id = null) |
||
5028 | |||
5029 | |||
5030 | |||
5031 | /** |
||
5032 | * Public wrapper for _deduce_fields_n_values_from_cols_n_values. |
||
5033 | * Given an array where keys are column (or column alias) names and values, |
||
5034 | * returns an array of their corresponding field names and database values |
||
5035 | * |
||
5036 | * @param array $cols_n_values |
||
5037 | * @return array |
||
5038 | */ |
||
5039 | public function deduce_fields_n_values_from_cols_n_values($cols_n_values) |
||
5043 | |||
5044 | |||
5045 | |||
5046 | /** |
||
5047 | * _deduce_fields_n_values_from_cols_n_values |
||
5048 | * Given an array where keys are column (or column alias) names and values, |
||
5049 | * returns an array of their corresponding field names and database values |
||
5050 | * |
||
5051 | * @param string $cols_n_values |
||
5052 | * @return array |
||
5053 | */ |
||
5054 | protected function _deduce_fields_n_values_from_cols_n_values($cols_n_values) |
||
5083 | |||
5084 | |||
5085 | |||
5086 | /** |
||
5087 | * @param $cols_n_values |
||
5088 | * @param $qualified_column |
||
5089 | * @param $regular_column |
||
5090 | * @return null |
||
5091 | */ |
||
5092 | protected function _get_column_value_with_table_alias_or_not($cols_n_values, $qualified_column, $regular_column) |
||
5105 | |||
5106 | |||
5107 | |||
5108 | /** |
||
5109 | * refresh_entity_map_from_db |
||
5110 | * Makes sure the model object in the entity map at $id assumes the values |
||
5111 | * of the database (opposite of EE_base_Class::save()) |
||
5112 | * |
||
5113 | * @param int|string $id |
||
5114 | * @return EE_Base_Class |
||
5115 | * @throws \EE_Error |
||
5116 | */ |
||
5117 | public function refresh_entity_map_from_db($id) |
||
5140 | |||
5141 | |||
5142 | |||
5143 | /** |
||
5144 | * refresh_entity_map_with |
||
5145 | * Leaves the entry in the entity map alone, but updates it to match the provided |
||
5146 | * $replacing_model_obj (which we assume to be its equivalent but somehow NOT in the entity map). |
||
5147 | * This is useful if you have a model object you want to make authoritative over what's in the entity map currently. |
||
5148 | * Note: The old $replacing_model_obj should now be destroyed as it's now un-authoritative |
||
5149 | * |
||
5150 | * @param int|string $id |
||
5151 | * @param EE_Base_Class $replacing_model_obj |
||
5152 | * @return \EE_Base_Class |
||
5153 | * @throws \EE_Error |
||
5154 | */ |
||
5155 | public function refresh_entity_map_with($id, $replacing_model_obj) |
||
5177 | |||
5178 | |||
5179 | |||
5180 | /** |
||
5181 | * Gets the EE class that corresponds to this model. Eg, for EEM_Answer that |
||
5182 | * would be EE_Answer.To import that class, you'd just add ".class.php" to the name, like so |
||
5183 | * require_once($this->_getClassName().".class.php"); |
||
5184 | * |
||
5185 | * @return string |
||
5186 | */ |
||
5187 | private function _get_class_name() |
||
5191 | |||
5192 | |||
5193 | |||
5194 | /** |
||
5195 | * Get the name of the items this model represents, for the quantity specified. Eg, |
||
5196 | * if $quantity==1, on EEM_Event, it would 'Event' (internationalized), otherwise |
||
5197 | * it would be 'Events'. |
||
5198 | * |
||
5199 | * @param int $quantity |
||
5200 | * @return string |
||
5201 | */ |
||
5202 | public function item_name($quantity = 1) |
||
5206 | |||
5207 | |||
5208 | |||
5209 | /** |
||
5210 | * Very handy general function to allow for plugins to extend any child of EE_TempBase. |
||
5211 | * If a method is called on a child of EE_TempBase that doesn't exist, this function is called |
||
5212 | * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. Instead of |
||
5213 | * requiring a plugin to extend the EE_TempBase (which works fine is there's only 1 plugin, but when will that |
||
5214 | * happen?) they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' (eg, |
||
5215 | * filters_hook_espresso__EE_Answer__my_great_function) and accepts 2 arguments: the object on which the function |
||
5216 | * was called, and an array of the original arguments passed to the function. Whatever their callback function |
||
5217 | * returns will be returned by this function. Example: in functions.php (or in a plugin): |
||
5218 | * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); function |
||
5219 | * my_callback($previousReturnValue,EE_TempBase $object,$argsArray){ |
||
5220 | * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
||
5221 | * return $previousReturnValue.$returnString; |
||
5222 | * } |
||
5223 | * require('EEM_Answer.model.php'); |
||
5224 | * $answer=EEM_Answer::instance(); |
||
5225 | * echo $answer->my_callback('monkeys',100); |
||
5226 | * //will output "you called my_callback! and passed args:monkeys,100" |
||
5227 | * |
||
5228 | * @param string $methodName name of method which was called on a child of EE_TempBase, but which |
||
5229 | * @param array $args array of original arguments passed to the function |
||
5230 | * @throws EE_Error |
||
5231 | * @return mixed whatever the plugin which calls add_filter decides |
||
5232 | */ |
||
5233 | View Code Duplication | public function __call($methodName, $args) |
|
5251 | |||
5252 | |||
5253 | |||
5254 | /** |
||
5255 | * Ensures $base_class_obj_or_id is of the EE_Base_Class child that corresponds ot this model. |
||
5256 | * If not, assumes its an ID, and uses $this->get_one_by_ID() to get the EE_Base_Class. |
||
5257 | * |
||
5258 | * @param EE_Base_Class|string|int $base_class_obj_or_id either: |
||
5259 | * the EE_Base_Class object that corresponds to this Model, |
||
5260 | * the object's class name |
||
5261 | * or object's ID |
||
5262 | * @param boolean $ensure_is_in_db if set, we will also verify this model object |
||
5263 | * exists in the database. If it does not, we add it |
||
5264 | * @throws EE_Error |
||
5265 | * @return EE_Base_Class |
||
5266 | */ |
||
5267 | public function ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db = false) |
||
5309 | |||
5310 | |||
5311 | |||
5312 | /** |
||
5313 | * Similar to ensure_is_obj(), this method makes sure $base_class_obj_or_id |
||
5314 | * is a value of the this model's primary key. If it's an EE_Base_Class child, |
||
5315 | * returns it ID. |
||
5316 | * |
||
5317 | * @param EE_Base_Class|int|string $base_class_obj_or_id |
||
5318 | * @return int|string depending on the type of this model object's ID |
||
5319 | * @throws EE_Error |
||
5320 | */ |
||
5321 | public function ensure_is_ID($base_class_obj_or_id) |
||
5340 | |||
5341 | |||
5342 | |||
5343 | /** |
||
5344 | * Sets whether the values passed to the model (eg, values in WHERE, values in INSERT, UPDATE, etc) |
||
5345 | * have already been ran through the appropriate model field's prepare_for_use_in_db method. IE, they have |
||
5346 | * been sanitized and converted into the appropriate domain. |
||
5347 | * Usually the only place you'll want to change the default (which is to assume values have NOT been sanitized by |
||
5348 | * the model object/model field) is when making a method call from WITHIN a model object, which has direct access |
||
5349 | * to its sanitized values. Note: after changing this setting, you should set it back to its previous value (using |
||
5350 | * get_assumption_concerning_values_already_prepared_by_model_object()) eg. |
||
5351 | * $EVT = EEM_Event::instance(); $old_setting = |
||
5352 | * $EVT->get_assumption_concerning_values_already_prepared_by_model_object(); |
||
5353 | * $EVT->assume_values_already_prepared_by_model_object(true); |
||
5354 | * $EVT->update(array('foo'=>'bar'),array(array('foo'=>'monkey'))); |
||
5355 | * $EVT->assume_values_already_prepared_by_model_object($old_setting); |
||
5356 | * |
||
5357 | * @param int $values_already_prepared like one of the constants on EEM_Base |
||
5358 | * @return void |
||
5359 | */ |
||
5360 | public function assume_values_already_prepared_by_model_object( |
||
5365 | |||
5366 | |||
5367 | |||
5368 | /** |
||
5369 | * Read comments for assume_values_already_prepared_by_model_object() |
||
5370 | * |
||
5371 | * @return int |
||
5372 | */ |
||
5373 | public function get_assumption_concerning_values_already_prepared_by_model_object() |
||
5377 | |||
5378 | |||
5379 | |||
5380 | /** |
||
5381 | * Gets all the indexes on this model |
||
5382 | * |
||
5383 | * @return EE_Index[] |
||
5384 | */ |
||
5385 | public function indexes() |
||
5389 | |||
5390 | |||
5391 | |||
5392 | /** |
||
5393 | * Gets all the Unique Indexes on this model |
||
5394 | * |
||
5395 | * @return EE_Unique_Index[] |
||
5396 | */ |
||
5397 | public function unique_indexes() |
||
5407 | |||
5408 | |||
5409 | |||
5410 | /** |
||
5411 | * Gets all the fields which, when combined, make the primary key. |
||
5412 | * This is usually just an array with 1 element (the primary key), but in cases |
||
5413 | * where there is no primary key, it's a combination of fields as defined |
||
5414 | * on a primary index |
||
5415 | * |
||
5416 | * @return EE_Model_Field_Base[] indexed by the field's name |
||
5417 | * @throws \EE_Error |
||
5418 | */ |
||
5419 | public function get_combined_primary_key_fields() |
||
5428 | |||
5429 | |||
5430 | |||
5431 | /** |
||
5432 | * Used to build a primary key string (when the model has no primary key), |
||
5433 | * which can be used a unique string to identify this model object. |
||
5434 | * |
||
5435 | * @param array $cols_n_values keys are field names, values are their values |
||
5436 | * @return string |
||
5437 | * @throws \EE_Error |
||
5438 | */ |
||
5439 | public function get_index_primary_key_string($cols_n_values) |
||
5445 | |||
5446 | |||
5447 | |||
5448 | /** |
||
5449 | * Gets the field values from the primary key string |
||
5450 | * |
||
5451 | * @see EEM_Base::get_combined_primary_key_fields() and EEM_Base::get_index_primary_key_string() |
||
5452 | * @param string $index_primary_key_string |
||
5453 | * @return null|array |
||
5454 | * @throws \EE_Error |
||
5455 | */ |
||
5456 | public function parse_index_primary_key_string($index_primary_key_string) |
||
5469 | |||
5470 | |||
5471 | |||
5472 | /** |
||
5473 | * verifies that an array of key-value pairs for model fields has a key |
||
5474 | * for each field comprising the primary key index |
||
5475 | * |
||
5476 | * @param array $key_vals |
||
5477 | * @return boolean |
||
5478 | * @throws \EE_Error |
||
5479 | */ |
||
5480 | public function has_all_combined_primary_key_fields($key_vals) |
||
5490 | |||
5491 | |||
5492 | |||
5493 | /** |
||
5494 | * Finds all model objects in the DB that appear to be a copy of $model_object_or_attributes_array. |
||
5495 | * We consider something to be a copy if all the attributes match (except the ID, of course). |
||
5496 | * |
||
5497 | * @param array|EE_Base_Class $model_object_or_attributes_array If its an array, it's field-value pairs |
||
5498 | * @param array $query_params like EEM_Base::get_all's query_params. |
||
5499 | * @throws EE_Error |
||
5500 | * @return \EE_Base_Class[] Array keys are object IDs (if there is a primary key on the model. if not, numerically |
||
5501 | * indexed) |
||
5502 | */ |
||
5503 | public function get_all_copies($model_object_or_attributes_array, $query_params = array()) |
||
5525 | |||
5526 | |||
5527 | |||
5528 | /** |
||
5529 | * Gets the first copy we find. See get_all_copies for more details |
||
5530 | * |
||
5531 | * @param mixed EE_Base_Class | array $model_object_or_attributes_array |
||
5532 | * @param array $query_params |
||
5533 | * @return EE_Base_Class |
||
5534 | * @throws \EE_Error |
||
5535 | */ |
||
5536 | View Code Duplication | public function get_one_copy($model_object_or_attributes_array, $query_params = array()) |
|
5552 | |||
5553 | |||
5554 | |||
5555 | /** |
||
5556 | * Updates the item with the specified id. Ignores default query parameters because |
||
5557 | * we have specified the ID, and its assumed we KNOW what we're doing |
||
5558 | * |
||
5559 | * @param array $fields_n_values keys are field names, values are their new values |
||
5560 | * @param int|string $id the value of the primary key to update |
||
5561 | * @return int number of rows updated |
||
5562 | * @throws \EE_Error |
||
5563 | */ |
||
5564 | public function update_by_ID($fields_n_values, $id) |
||
5572 | |||
5573 | |||
5574 | |||
5575 | /** |
||
5576 | * Changes an operator which was supplied to the models into one usable in SQL |
||
5577 | * |
||
5578 | * @param string $operator_supplied |
||
5579 | * @return string an operator which can be used in SQL |
||
5580 | * @throws EE_Error |
||
5581 | */ |
||
5582 | private function _prepare_operator_for_sql($operator_supplied) |
||
5593 | |||
5594 | |||
5595 | |||
5596 | /** |
||
5597 | * Gets an array where keys are the primary keys and values are their 'names' |
||
5598 | * (as determined by the model object's name() function, which is often overridden) |
||
5599 | * |
||
5600 | * @param array $query_params like get_all's |
||
5601 | * @return string[] |
||
5602 | * @throws \EE_Error |
||
5603 | */ |
||
5604 | public function get_all_names($query_params = array()) |
||
5613 | |||
5614 | |||
5615 | |||
5616 | /** |
||
5617 | * Gets an array of primary keys from the model objects. If you acquired the model objects |
||
5618 | * using EEM_Base::get_all() you don't need to call this (and probably shouldn't because |
||
5619 | * this is duplicated effort and reduces efficiency) you would be better to use |
||
5620 | * array_keys() on $model_objects. |
||
5621 | * |
||
5622 | * @param \EE_Base_Class[] $model_objects |
||
5623 | * @param boolean $filter_out_empty_ids if a model object has an ID of '' or 0, don't bother including it |
||
5624 | * in the returned array |
||
5625 | * @return array |
||
5626 | * @throws \EE_Error |
||
5627 | */ |
||
5628 | public function get_IDs($model_objects, $filter_out_empty_ids = false) |
||
5663 | |||
5664 | |||
5665 | |||
5666 | /** |
||
5667 | * Returns the string used in capabilities relating to this model. If there |
||
5668 | * are no capabilities that relate to this model returns false |
||
5669 | * |
||
5670 | * @return string|false |
||
5671 | */ |
||
5672 | public function cap_slug() |
||
5676 | |||
5677 | |||
5678 | |||
5679 | /** |
||
5680 | * Returns the capability-restrictions array (@see EEM_Base::_cap_restrictions). |
||
5681 | * If $context is provided (which should be set to one of EEM_Base::valid_cap_contexts()) |
||
5682 | * only returns the cap restrictions array in that context (ie, the array |
||
5683 | * at that key) |
||
5684 | * |
||
5685 | * @param string $context |
||
5686 | * @return EE_Default_Where_Conditions[] indexed by associated capability |
||
5687 | * @throws \EE_Error |
||
5688 | */ |
||
5689 | public function cap_restrictions($context = EEM_Base::caps_read) |
||
5711 | |||
5712 | |||
5713 | |||
5714 | /** |
||
5715 | * Indicating whether or not this model thinks its a wp core model |
||
5716 | * |
||
5717 | * @return boolean |
||
5718 | */ |
||
5719 | public function is_wp_core_model() |
||
5723 | |||
5724 | |||
5725 | |||
5726 | /** |
||
5727 | * Gets all the caps that are missing which impose a restriction on |
||
5728 | * queries made in this context |
||
5729 | * |
||
5730 | * @param string $context one of EEM_Base::caps_ constants |
||
5731 | * @return EE_Default_Where_Conditions[] indexed by capability name |
||
5732 | * @throws \EE_Error |
||
5733 | */ |
||
5734 | public function caps_missing($context = EEM_Base::caps_read) |
||
5747 | |||
5748 | |||
5749 | |||
5750 | /** |
||
5751 | * Gets the mapping from capability contexts to action strings used in capability names |
||
5752 | * |
||
5753 | * @return array keys are one of EEM_Base::valid_cap_contexts(), and values are usually |
||
5754 | * one of 'read', 'edit', or 'delete' |
||
5755 | */ |
||
5756 | public function cap_contexts_to_cap_action_map() |
||
5761 | |||
5762 | |||
5763 | |||
5764 | /** |
||
5765 | * Gets the action string for the specified capability context |
||
5766 | * |
||
5767 | * @param string $context |
||
5768 | * @return string one of EEM_Base::cap_contexts_to_cap_action_map() values |
||
5769 | * @throws \EE_Error |
||
5770 | */ |
||
5771 | public function cap_action_for_context($context) |
||
5788 | |||
5789 | |||
5790 | |||
5791 | /** |
||
5792 | * Returns all the capability contexts which are valid when querying models |
||
5793 | * |
||
5794 | * @return array |
||
5795 | */ |
||
5796 | public static function valid_cap_contexts() |
||
5805 | |||
5806 | |||
5807 | |||
5808 | /** |
||
5809 | * Returns all valid options for 'default_where_conditions' |
||
5810 | * |
||
5811 | * @return array |
||
5812 | */ |
||
5813 | public static function valid_default_where_conditions() |
||
5824 | |||
5825 | // public static function default_where_conditions_full |
||
5826 | /** |
||
5827 | * Verifies $context is one of EEM_Base::valid_cap_contexts(), if not it throws an exception |
||
5828 | * |
||
5829 | * @param string $context |
||
5830 | * @return bool |
||
5831 | * @throws \EE_Error |
||
5832 | */ |
||
5833 | static public function verify_is_valid_cap_context($context) |
||
5850 | |||
5851 | |||
5852 | |||
5853 | /** |
||
5854 | * Clears all the models field caches. This is only useful when a sub-class |
||
5855 | * might have added a field or something and these caches might be invalidated |
||
5856 | */ |
||
5857 | protected function _invalidate_field_caches() |
||
5863 | |||
5864 | |||
5865 | |||
5866 | /** |
||
5867 | * Gets the list of all the where query param keys that relate to logic instead of field names |
||
5868 | * (eg "and", "or", "not"). |
||
5869 | * |
||
5870 | * @return array |
||
5871 | */ |
||
5872 | public function logic_query_param_keys() |
||
5876 | |||
5877 | |||
5878 | |||
5879 | /** |
||
5880 | * Determines whether or not the where query param array key is for a logic query param. |
||
5881 | * Eg 'OR', 'not*', and 'and*because-i-say-so' shoudl all return true, whereas |
||
5882 | * 'ATT_fname', 'EVT_name*not-you-or-me', and 'ORG_name' should return false |
||
5883 | * |
||
5884 | * @param $query_param_key |
||
5885 | * @return bool |
||
5886 | */ |
||
5887 | public function is_logic_query_param_key($query_param_key) |
||
5898 | |||
5899 | |||
5900 | |||
5901 | } |
||
5902 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: