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 EE_Base_Class 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 EE_Base_Class, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | abstract class EE_Base_Class |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * This is an array of the original properties and values provided during construction |
||
18 | * of this model object. (keys are model field names, values are their values). |
||
19 | * This list is important to remember so that when we are merging data from the db, we know |
||
20 | * which values to override and which to not override. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $_props_n_values_provided_in_constructor; |
||
25 | |||
26 | /** |
||
27 | * Timezone |
||
28 | * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in. |
||
29 | * This can also be used before a get to set what timezone you want strings coming out of the object to be in. NOT |
||
30 | * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have |
||
31 | * access to it. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $_timezone; |
||
36 | |||
37 | /** |
||
38 | * date format |
||
39 | * pattern or format for displaying dates |
||
40 | * |
||
41 | * @var string $_dt_frmt |
||
42 | */ |
||
43 | protected $_dt_frmt; |
||
44 | |||
45 | /** |
||
46 | * time format |
||
47 | * pattern or format for displaying time |
||
48 | * |
||
49 | * @var string $_tm_frmt |
||
50 | */ |
||
51 | protected $_tm_frmt; |
||
52 | |||
53 | /** |
||
54 | * This property is for holding a cached array of object properties indexed by property name as the key. |
||
55 | * The purpose of this is for setting a cache on properties that may have calculated values after a |
||
56 | * prepare_for_get. That way the cache can be checked first and the calculated property returned instead of having |
||
57 | * to recalculate. Used by _set_cached_property() and _get_cached_property() methods. |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $_cached_properties = array(); |
||
62 | |||
63 | /** |
||
64 | * An array containing keys of the related model, and values are either an array of related mode objects or a |
||
65 | * single |
||
66 | * related model object. see the model's _model_relations. The keys should match those specified. And if the |
||
67 | * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object, |
||
68 | * all others have an array) |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $_model_relations = array(); |
||
73 | |||
74 | /** |
||
75 | * Array where keys are field names (see the model's _fields property) and values are their values. To see what |
||
76 | * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods) |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $_fields = array(); |
||
81 | |||
82 | /** |
||
83 | * @var boolean indicating whether or not this model object is intended to ever be saved |
||
84 | * For example, we might create model objects intended to only be used for the duration |
||
85 | * of this request and to be thrown away, and if they were accidentally saved |
||
86 | * it would be a bug. |
||
87 | */ |
||
88 | protected $_allow_persist = true; |
||
89 | |||
90 | /** |
||
91 | * @var boolean indicating whether or not this model object's properties have changed since construction |
||
92 | */ |
||
93 | protected $_has_changes = false; |
||
94 | |||
95 | /** |
||
96 | * @var EEM_Base |
||
97 | */ |
||
98 | protected $_model; |
||
99 | |||
100 | /** |
||
101 | * This is a cache of results from custom selections done on a query that constructs this entity. The only purpose |
||
102 | * for these values is for retrieval of the results, they are not further queryable and they are not persisted to |
||
103 | * the db. They also do not automatically update if there are any changes to the data that produced their results. |
||
104 | * The format is a simple array of field_alias => field_value. So for instance if a custom select was something |
||
105 | * like, "Select COUNT(Registration.REG_ID) as Registration_Count ...", then the resulting value will be in this |
||
106 | * array as: |
||
107 | * array( |
||
108 | * 'Registration_Count' => 24 |
||
109 | * ); |
||
110 | * Note: if the custom select configuration for the query included a data type, the value will be in the data type |
||
111 | * provided for the query (@see EventEspresso\core\domain\values\model\CustomSelects::__construct phpdocs for more |
||
112 | * info) |
||
113 | * |
||
114 | * @var array |
||
115 | */ |
||
116 | protected $custom_selection_results = array(); |
||
117 | |||
118 | |||
119 | /** |
||
120 | * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
||
121 | * play nice |
||
122 | * |
||
123 | * @param array $fieldValues where each key is a field (ie, array key in the 2nd |
||
124 | * layer of the model's _fields array, (eg, EVT_ID, |
||
125 | * TXN_amount, QST_name, etc) and values are their values |
||
126 | * @param boolean $bydb a flag for setting if the class is instantiated by the |
||
127 | * corresponding db model or not. |
||
128 | * @param string $timezone indicate what timezone you want any datetime fields to |
||
129 | * be in when instantiating a EE_Base_Class object. |
||
130 | * @param array $date_formats An array of date formats to set on construct where first |
||
131 | * value is the date_format and second value is the time |
||
132 | * format. |
||
133 | * @throws InvalidArgumentException |
||
134 | * @throws InvalidInterfaceException |
||
135 | * @throws InvalidDataTypeException |
||
136 | * @throws EE_Error |
||
137 | * @throws ReflectionException |
||
138 | */ |
||
139 | protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array()) |
||
212 | |||
213 | |||
214 | /** |
||
215 | * Gets whether or not this model object is allowed to persist/be saved to the database. |
||
216 | * |
||
217 | * @return boolean |
||
218 | */ |
||
219 | public function allow_persist() |
||
223 | |||
224 | |||
225 | /** |
||
226 | * Sets whether or not this model object should be allowed to be saved to the DB. |
||
227 | * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless |
||
228 | * you got new information that somehow made you change your mind. |
||
229 | * |
||
230 | * @param boolean $allow_persist |
||
231 | * @return boolean |
||
232 | */ |
||
233 | public function set_allow_persist($allow_persist) |
||
237 | |||
238 | |||
239 | /** |
||
240 | * Gets the field's original value when this object was constructed during this request. |
||
241 | * This can be helpful when determining if a model object has changed or not |
||
242 | * |
||
243 | * @param string $field_name |
||
244 | * @return mixed|null |
||
245 | * @throws ReflectionException |
||
246 | * @throws InvalidArgumentException |
||
247 | * @throws InvalidInterfaceException |
||
248 | * @throws InvalidDataTypeException |
||
249 | * @throws EE_Error |
||
250 | */ |
||
251 | public function get_original($field_name) |
||
260 | |||
261 | |||
262 | /** |
||
263 | * @param EE_Base_Class $obj |
||
264 | * @return string |
||
265 | */ |
||
266 | public function get_class($obj) |
||
270 | |||
271 | |||
272 | /** |
||
273 | * Overrides parent because parent expects old models. |
||
274 | * This also doesn't do any validation, and won't work for serialized arrays |
||
275 | * |
||
276 | * @param string $field_name |
||
277 | * @param mixed $field_value |
||
278 | * @param bool $use_default |
||
279 | * @throws InvalidArgumentException |
||
280 | * @throws InvalidInterfaceException |
||
281 | * @throws InvalidDataTypeException |
||
282 | * @throws EE_Error |
||
283 | * @throws ReflectionException |
||
284 | * @throws ReflectionException |
||
285 | * @throws ReflectionException |
||
286 | */ |
||
287 | public function set($field_name, $field_value, $use_default = false) |
||
367 | |||
368 | |||
369 | /** |
||
370 | * Set custom select values for model. |
||
371 | * |
||
372 | * @param array $custom_select_values |
||
373 | */ |
||
374 | public function setCustomSelectsValues(array $custom_select_values) |
||
378 | |||
379 | |||
380 | /** |
||
381 | * Returns the custom select value for the provided alias if its set. |
||
382 | * If not set, returns null. |
||
383 | * |
||
384 | * @param string $alias |
||
385 | * @return string|int|float|null |
||
386 | */ |
||
387 | public function getCustomSelect($alias) |
||
393 | |||
394 | |||
395 | /** |
||
396 | * This sets the field value on the db column if it exists for the given $column_name or |
||
397 | * saves it to EE_Extra_Meta if the given $column_name does not match a db column. |
||
398 | * |
||
399 | * @see EE_message::get_column_value for related documentation on the necessity of this method. |
||
400 | * @param string $field_name Must be the exact column name. |
||
401 | * @param mixed $field_value The value to set. |
||
402 | * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs. |
||
403 | * @throws InvalidArgumentException |
||
404 | * @throws InvalidInterfaceException |
||
405 | * @throws InvalidDataTypeException |
||
406 | * @throws EE_Error |
||
407 | * @throws ReflectionException |
||
408 | */ |
||
409 | public function set_field_or_extra_meta($field_name, $field_value) |
||
419 | |||
420 | |||
421 | /** |
||
422 | * This retrieves the value of the db column set on this class or if that's not present |
||
423 | * it will attempt to retrieve from extra_meta if found. |
||
424 | * Example Usage: |
||
425 | * Via EE_Message child class: |
||
426 | * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to", |
||
427 | * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may |
||
428 | * also have additional main fields specific to the messenger. The system accommodates those extra |
||
429 | * fields through the EE_Extra_Meta table. This method allows for EE_messengers to retrieve the |
||
430 | * value for those extra fields dynamically via the EE_message object. |
||
431 | * |
||
432 | * @param string $field_name expecting the fully qualified field name. |
||
433 | * @return mixed|null value for the field if found. null if not found. |
||
434 | * @throws ReflectionException |
||
435 | * @throws InvalidArgumentException |
||
436 | * @throws InvalidInterfaceException |
||
437 | * @throws InvalidDataTypeException |
||
438 | * @throws EE_Error |
||
439 | */ |
||
440 | public function get_field_or_extra_meta($field_name) |
||
450 | |||
451 | |||
452 | /** |
||
453 | * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
||
454 | * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
||
455 | * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is |
||
456 | * available to all child classes that may be using the EE_Datetime_Field for a field data type. |
||
457 | * |
||
458 | * @access public |
||
459 | * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php |
||
460 | * @return void |
||
461 | * @throws InvalidArgumentException |
||
462 | * @throws InvalidInterfaceException |
||
463 | * @throws InvalidDataTypeException |
||
464 | * @throws EE_Error |
||
465 | * @throws ReflectionException |
||
466 | */ |
||
467 | public function set_timezone($timezone = '') |
||
483 | |||
484 | |||
485 | /** |
||
486 | * This just returns whatever is set for the current timezone. |
||
487 | * |
||
488 | * @access public |
||
489 | * @return string timezone string |
||
490 | */ |
||
491 | public function get_timezone() |
||
495 | |||
496 | |||
497 | /** |
||
498 | * This sets the internal date format to what is sent in to be used as the new default for the class |
||
499 | * internally instead of wp set date format options |
||
500 | * |
||
501 | * @since 4.6 |
||
502 | * @param string $format should be a format recognizable by PHP date() functions. |
||
503 | */ |
||
504 | public function set_date_format($format) |
||
510 | |||
511 | |||
512 | /** |
||
513 | * This sets the internal time format string to what is sent in to be used as the new default for the |
||
514 | * class internally instead of wp set time format options. |
||
515 | * |
||
516 | * @since 4.6 |
||
517 | * @param string $format should be a format recognizable by PHP date() functions. |
||
518 | */ |
||
519 | public function set_time_format($format) |
||
525 | |||
526 | |||
527 | /** |
||
528 | * This returns the current internal set format for the date and time formats. |
||
529 | * |
||
530 | * @param bool $full if true (default), then return the full format. Otherwise will return an array |
||
531 | * where the first value is the date format and the second value is the time format. |
||
532 | * @return mixed string|array |
||
533 | */ |
||
534 | public function get_format($full = true) |
||
538 | |||
539 | |||
540 | /** |
||
541 | * cache |
||
542 | * stores the passed model object on the current model object. |
||
543 | * In certain circumstances, we can use this cached model object instead of querying for another one entirely. |
||
544 | * |
||
545 | * @param string $relationName one of the keys in the _model_relations array on the model. Eg |
||
546 | * 'Registration' associated with this model object |
||
547 | * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction, |
||
548 | * that could be a payment or a registration) |
||
549 | * @param null $cache_id a string or number that will be used as the key for any Belongs_To_Many |
||
550 | * items which will be stored in an array on this object |
||
551 | * @throws ReflectionException |
||
552 | * @throws InvalidArgumentException |
||
553 | * @throws InvalidInterfaceException |
||
554 | * @throws InvalidDataTypeException |
||
555 | * @throws EE_Error |
||
556 | * @return mixed index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one |
||
557 | * related thing, no array) |
||
558 | */ |
||
559 | public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
||
616 | |||
617 | |||
618 | /** |
||
619 | * For adding an item to the cached_properties property. |
||
620 | * |
||
621 | * @access protected |
||
622 | * @param string $fieldname the property item the corresponding value is for. |
||
623 | * @param mixed $value The value we are caching. |
||
624 | * @param string|null $cache_type |
||
625 | * @return void |
||
626 | * @throws ReflectionException |
||
627 | * @throws InvalidArgumentException |
||
628 | * @throws InvalidInterfaceException |
||
629 | * @throws InvalidDataTypeException |
||
630 | * @throws EE_Error |
||
631 | */ |
||
632 | protected function _set_cached_property($fieldname, $value, $cache_type = null) |
||
639 | |||
640 | |||
641 | /** |
||
642 | * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist. |
||
643 | * This also SETS the cache if we return the actual property! |
||
644 | * |
||
645 | * @param string $fieldname the name of the property we're trying to retrieve |
||
646 | * @param bool $pretty |
||
647 | * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
||
648 | * (in cases where the same property may be used for different outputs |
||
649 | * - i.e. datetime, money etc.) |
||
650 | * It can also accept certain pre-defined "schema" strings |
||
651 | * to define how to output the property. |
||
652 | * see the field's prepare_for_pretty_echoing for what strings can be used |
||
653 | * @return mixed whatever the value for the property is we're retrieving |
||
654 | * @throws ReflectionException |
||
655 | * @throws InvalidArgumentException |
||
656 | * @throws InvalidInterfaceException |
||
657 | * @throws InvalidDataTypeException |
||
658 | * @throws EE_Error |
||
659 | */ |
||
660 | protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null) |
||
674 | |||
675 | |||
676 | /** |
||
677 | * If the cache didn't fetch the needed item, this fetches it. |
||
678 | * |
||
679 | * @param string $fieldname |
||
680 | * @param bool $pretty |
||
681 | * @param string $extra_cache_ref |
||
682 | * @return mixed |
||
683 | * @throws InvalidArgumentException |
||
684 | * @throws InvalidInterfaceException |
||
685 | * @throws InvalidDataTypeException |
||
686 | * @throws EE_Error |
||
687 | * @throws ReflectionException |
||
688 | */ |
||
689 | protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null) |
||
704 | |||
705 | |||
706 | /** |
||
707 | * set timezone, formats, and output for EE_Datetime_Field objects |
||
708 | * |
||
709 | * @param \EE_Datetime_Field $datetime_field |
||
710 | * @param bool $pretty |
||
711 | * @param null $date_or_time |
||
712 | * @return void |
||
713 | * @throws InvalidArgumentException |
||
714 | * @throws InvalidInterfaceException |
||
715 | * @throws InvalidDataTypeException |
||
716 | * @throws EE_Error |
||
717 | */ |
||
718 | protected function _prepare_datetime_field( |
||
738 | |||
739 | |||
740 | /** |
||
741 | * This just takes care of clearing out the cached_properties |
||
742 | * |
||
743 | * @return void |
||
744 | */ |
||
745 | protected function _clear_cached_properties() |
||
749 | |||
750 | |||
751 | /** |
||
752 | * This just clears out ONE property if it exists in the cache |
||
753 | * |
||
754 | * @param string $property_name the property to remove if it exists (from the _cached_properties array) |
||
755 | * @return void |
||
756 | */ |
||
757 | protected function _clear_cached_property($property_name) |
||
763 | |||
764 | |||
765 | /** |
||
766 | * Ensures that this related thing is a model object. |
||
767 | * |
||
768 | * @param mixed $object_or_id EE_base_Class/int/string either a related model object, or its ID |
||
769 | * @param string $model_name name of the related thing, eg 'Attendee', |
||
770 | * @return EE_Base_Class |
||
771 | * @throws ReflectionException |
||
772 | * @throws InvalidArgumentException |
||
773 | * @throws InvalidInterfaceException |
||
774 | * @throws InvalidDataTypeException |
||
775 | * @throws EE_Error |
||
776 | */ |
||
777 | protected function ensure_related_thing_is_model_obj($object_or_id, $model_name) |
||
785 | |||
786 | |||
787 | /** |
||
788 | * Forgets the cached model of the given relation Name. So the next time we request it, |
||
789 | * we will fetch it again from the database. (Handy if you know it's changed somehow). |
||
790 | * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM, |
||
791 | * then only remove that one object from our cached array. Otherwise, clear the entire list |
||
792 | * |
||
793 | * @param string $relationName one of the keys in the _model_relations array on the model. |
||
794 | * Eg 'Registration' |
||
795 | * @param mixed $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL |
||
796 | * if you intend to use $clear_all = TRUE, or the relation only |
||
797 | * has 1 object anyways (ie, it's a BelongsToRelation) |
||
798 | * @param bool $clear_all This flags clearing the entire cache relation property if |
||
799 | * this is HasMany or HABTM. |
||
800 | * @throws ReflectionException |
||
801 | * @throws InvalidArgumentException |
||
802 | * @throws InvalidInterfaceException |
||
803 | * @throws InvalidDataTypeException |
||
804 | * @throws EE_Error |
||
805 | * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a |
||
806 | * relation from all |
||
807 | */ |
||
808 | public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false) |
||
880 | |||
881 | |||
882 | /** |
||
883 | * update_cache_after_object_save |
||
884 | * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has |
||
885 | * obtained after being saved to the db |
||
886 | * |
||
887 | * @param string $relationName - the type of object that is cached |
||
888 | * @param EE_Base_Class $newly_saved_object - the newly saved object to be re-cached |
||
889 | * @param string $current_cache_id - the ID that was used when originally caching the object |
||
890 | * @return boolean TRUE on success, FALSE on fail |
||
891 | * @throws ReflectionException |
||
892 | * @throws InvalidArgumentException |
||
893 | * @throws InvalidInterfaceException |
||
894 | * @throws InvalidDataTypeException |
||
895 | * @throws EE_Error |
||
896 | */ |
||
897 | public function update_cache_after_object_save( |
||
927 | |||
928 | |||
929 | /** |
||
930 | * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
||
931 | * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
||
932 | * |
||
933 | * @param string $relationName |
||
934 | * @return EE_Base_Class |
||
935 | */ |
||
936 | public function get_one_from_cache($relationName) |
||
946 | |||
947 | |||
948 | /** |
||
949 | * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
||
950 | * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
||
951 | * |
||
952 | * @param string $relationName |
||
953 | * @throws ReflectionException |
||
954 | * @throws InvalidArgumentException |
||
955 | * @throws InvalidInterfaceException |
||
956 | * @throws InvalidDataTypeException |
||
957 | * @throws EE_Error |
||
958 | * @return EE_Base_Class[] NOT necessarily indexed by primary keys |
||
959 | */ |
||
960 | public function get_all_from_cache($relationName) |
||
992 | |||
993 | |||
994 | /** |
||
995 | * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database |
||
996 | * matching the given query conditions. |
||
997 | * |
||
998 | * @param null $field_to_order_by What field is being used as the reference point. |
||
999 | * @param int $limit How many objects to return. |
||
1000 | * @param array $query_params Any additional conditions on the query. |
||
1001 | * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
||
1002 | * you can indicate just the columns you want returned |
||
1003 | * @return array|EE_Base_Class[] |
||
1004 | * @throws ReflectionException |
||
1005 | * @throws InvalidArgumentException |
||
1006 | * @throws InvalidInterfaceException |
||
1007 | * @throws InvalidDataTypeException |
||
1008 | * @throws EE_Error |
||
1009 | */ |
||
1010 | View Code Duplication | public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null) |
|
1022 | |||
1023 | |||
1024 | /** |
||
1025 | * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database |
||
1026 | * matching the given query conditions. |
||
1027 | * |
||
1028 | * @param null $field_to_order_by What field is being used as the reference point. |
||
1029 | * @param int $limit How many objects to return. |
||
1030 | * @param array $query_params Any additional conditions on the query. |
||
1031 | * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
||
1032 | * you can indicate just the columns you want returned |
||
1033 | * @return array|EE_Base_Class[] |
||
1034 | * @throws ReflectionException |
||
1035 | * @throws InvalidArgumentException |
||
1036 | * @throws InvalidInterfaceException |
||
1037 | * @throws InvalidDataTypeException |
||
1038 | * @throws EE_Error |
||
1039 | */ |
||
1040 | View Code Duplication | public function previous_x( |
|
1056 | |||
1057 | |||
1058 | /** |
||
1059 | * Returns the next EE_Base_Class object in sequence from this object as found in the database |
||
1060 | * matching the given query conditions. |
||
1061 | * |
||
1062 | * @param null $field_to_order_by What field is being used as the reference point. |
||
1063 | * @param array $query_params Any additional conditions on the query. |
||
1064 | * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
||
1065 | * you can indicate just the columns you want returned |
||
1066 | * @return array|EE_Base_Class |
||
1067 | * @throws ReflectionException |
||
1068 | * @throws InvalidArgumentException |
||
1069 | * @throws InvalidInterfaceException |
||
1070 | * @throws InvalidDataTypeException |
||
1071 | * @throws EE_Error |
||
1072 | */ |
||
1073 | View Code Duplication | public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
1085 | |||
1086 | |||
1087 | /** |
||
1088 | * Returns the previous EE_Base_Class object in sequence from this object as found in the database |
||
1089 | * matching the given query conditions. |
||
1090 | * |
||
1091 | * @param null $field_to_order_by What field is being used as the reference point. |
||
1092 | * @param array $query_params Any additional conditions on the query. |
||
1093 | * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
||
1094 | * you can indicate just the column you want returned |
||
1095 | * @return array|EE_Base_Class |
||
1096 | * @throws ReflectionException |
||
1097 | * @throws InvalidArgumentException |
||
1098 | * @throws InvalidInterfaceException |
||
1099 | * @throws InvalidDataTypeException |
||
1100 | * @throws EE_Error |
||
1101 | */ |
||
1102 | View Code Duplication | public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
1114 | |||
1115 | |||
1116 | /** |
||
1117 | * Overrides parent because parent expects old models. |
||
1118 | * This also doesn't do any validation, and won't work for serialized arrays |
||
1119 | * |
||
1120 | * @param string $field_name |
||
1121 | * @param mixed $field_value_from_db |
||
1122 | * @throws ReflectionException |
||
1123 | * @throws InvalidArgumentException |
||
1124 | * @throws InvalidInterfaceException |
||
1125 | * @throws InvalidDataTypeException |
||
1126 | * @throws EE_Error |
||
1127 | */ |
||
1128 | public function set_from_db($field_name, $field_value_from_db) |
||
1150 | |||
1151 | |||
1152 | /** |
||
1153 | * verifies that the specified field is of the correct type |
||
1154 | * |
||
1155 | * @param string $field_name |
||
1156 | * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
||
1157 | * (in cases where the same property may be used for different outputs |
||
1158 | * - i.e. datetime, money etc.) |
||
1159 | * @return mixed |
||
1160 | * @throws ReflectionException |
||
1161 | * @throws InvalidArgumentException |
||
1162 | * @throws InvalidInterfaceException |
||
1163 | * @throws InvalidDataTypeException |
||
1164 | * @throws EE_Error |
||
1165 | */ |
||
1166 | public function get($field_name, $extra_cache_ref = null) |
||
1170 | |||
1171 | |||
1172 | /** |
||
1173 | * This method simply returns the RAW unprocessed value for the given property in this class |
||
1174 | * |
||
1175 | * @param string $field_name A valid fieldname |
||
1176 | * @return mixed Whatever the raw value stored on the property is. |
||
1177 | * @throws ReflectionException |
||
1178 | * @throws InvalidArgumentException |
||
1179 | * @throws InvalidInterfaceException |
||
1180 | * @throws InvalidDataTypeException |
||
1181 | * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
||
1182 | */ |
||
1183 | public function get_raw($field_name) |
||
1190 | |||
1191 | |||
1192 | /** |
||
1193 | * This is used to return the internal DateTime object used for a field that is a |
||
1194 | * EE_Datetime_Field. |
||
1195 | * |
||
1196 | * @param string $field_name The field name retrieving the DateTime object. |
||
1197 | * @return mixed null | false | DateTime If the requested field is NOT a EE_Datetime_Field then |
||
1198 | * @throws EE_Error an error is set and false returned. If the field IS an |
||
1199 | * EE_Datetime_Field and but the field value is null, then |
||
1200 | * just null is returned (because that indicates that likely |
||
1201 | * this field is nullable). |
||
1202 | * @throws InvalidArgumentException |
||
1203 | * @throws InvalidDataTypeException |
||
1204 | * @throws InvalidInterfaceException |
||
1205 | * @throws ReflectionException |
||
1206 | */ |
||
1207 | public function get_DateTime_object($field_name) |
||
1229 | |||
1230 | |||
1231 | /** |
||
1232 | * To be used in template to immediately echo out the value, and format it for output. |
||
1233 | * Eg, should call stripslashes and whatnot before echoing |
||
1234 | * |
||
1235 | * @param string $field_name the name of the field as it appears in the DB |
||
1236 | * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
||
1237 | * (in cases where the same property may be used for different outputs |
||
1238 | * - i.e. datetime, money etc.) |
||
1239 | * @return void |
||
1240 | * @throws ReflectionException |
||
1241 | * @throws InvalidArgumentException |
||
1242 | * @throws InvalidInterfaceException |
||
1243 | * @throws InvalidDataTypeException |
||
1244 | * @throws EE_Error |
||
1245 | */ |
||
1246 | public function e($field_name, $extra_cache_ref = null) |
||
1250 | |||
1251 | |||
1252 | /** |
||
1253 | * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it |
||
1254 | * can be easily used as the value of form input. |
||
1255 | * |
||
1256 | * @param string $field_name |
||
1257 | * @return void |
||
1258 | * @throws ReflectionException |
||
1259 | * @throws InvalidArgumentException |
||
1260 | * @throws InvalidInterfaceException |
||
1261 | * @throws InvalidDataTypeException |
||
1262 | * @throws EE_Error |
||
1263 | */ |
||
1264 | public function f($field_name) |
||
1268 | |||
1269 | |||
1270 | /** |
||
1271 | * Same as `f()` but just returns the value instead of echoing it |
||
1272 | * |
||
1273 | * @param string $field_name |
||
1274 | * @return string |
||
1275 | * @throws ReflectionException |
||
1276 | * @throws InvalidArgumentException |
||
1277 | * @throws InvalidInterfaceException |
||
1278 | * @throws InvalidDataTypeException |
||
1279 | * @throws EE_Error |
||
1280 | */ |
||
1281 | public function get_f($field_name) |
||
1285 | |||
1286 | |||
1287 | /** |
||
1288 | * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this. |
||
1289 | * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class |
||
1290 | * to see what options are available. |
||
1291 | * |
||
1292 | * @param string $field_name |
||
1293 | * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
||
1294 | * (in cases where the same property may be used for different outputs |
||
1295 | * - i.e. datetime, money etc.) |
||
1296 | * @return mixed |
||
1297 | * @throws ReflectionException |
||
1298 | * @throws InvalidArgumentException |
||
1299 | * @throws InvalidInterfaceException |
||
1300 | * @throws InvalidDataTypeException |
||
1301 | * @throws EE_Error |
||
1302 | */ |
||
1303 | public function get_pretty($field_name, $extra_cache_ref = null) |
||
1307 | |||
1308 | |||
1309 | /** |
||
1310 | * This simply returns the datetime for the given field name |
||
1311 | * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions |
||
1312 | * (and the equivalent e_date, e_time, e_datetime). |
||
1313 | * |
||
1314 | * @access protected |
||
1315 | * @param string $field_name Field on the instantiated EE_Base_Class child object |
||
1316 | * @param string $dt_frmt valid datetime format used for date |
||
1317 | * (if '' then we just use the default on the field, |
||
1318 | * if NULL we use the last-used format) |
||
1319 | * @param string $tm_frmt Same as above except this is for time format |
||
1320 | * @param string $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time. |
||
1321 | * @param boolean $echo Whether the dtt is echoing using pretty echoing or just returned using vanilla get |
||
1322 | * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown |
||
1323 | * if field is not a valid dtt field, or void if echoing |
||
1324 | * @throws ReflectionException |
||
1325 | * @throws InvalidArgumentException |
||
1326 | * @throws InvalidInterfaceException |
||
1327 | * @throws InvalidDataTypeException |
||
1328 | * @throws EE_Error |
||
1329 | */ |
||
1330 | protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false) |
||
1343 | |||
1344 | |||
1345 | /** |
||
1346 | * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date |
||
1347 | * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
||
1348 | * other echoes the pretty value for dtt) |
||
1349 | * |
||
1350 | * @param string $field_name name of model object datetime field holding the value |
||
1351 | * @param string $format format for the date returned (if NULL we use default in dt_frmt property) |
||
1352 | * @return string datetime value formatted |
||
1353 | * @throws ReflectionException |
||
1354 | * @throws InvalidArgumentException |
||
1355 | * @throws InvalidInterfaceException |
||
1356 | * @throws InvalidDataTypeException |
||
1357 | * @throws EE_Error |
||
1358 | */ |
||
1359 | public function get_date($field_name, $format = '') |
||
1363 | |||
1364 | |||
1365 | /** |
||
1366 | * @param $field_name |
||
1367 | * @param string $format |
||
1368 | * @throws ReflectionException |
||
1369 | * @throws InvalidArgumentException |
||
1370 | * @throws InvalidInterfaceException |
||
1371 | * @throws InvalidDataTypeException |
||
1372 | * @throws EE_Error |
||
1373 | */ |
||
1374 | public function e_date($field_name, $format = '') |
||
1378 | |||
1379 | |||
1380 | /** |
||
1381 | * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time |
||
1382 | * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
||
1383 | * other echoes the pretty value for dtt) |
||
1384 | * |
||
1385 | * @param string $field_name name of model object datetime field holding the value |
||
1386 | * @param string $format format for the time returned ( if NULL we use default in tm_frmt property) |
||
1387 | * @return string datetime value formatted |
||
1388 | * @throws ReflectionException |
||
1389 | * @throws InvalidArgumentException |
||
1390 | * @throws InvalidInterfaceException |
||
1391 | * @throws InvalidDataTypeException |
||
1392 | * @throws EE_Error |
||
1393 | */ |
||
1394 | public function get_time($field_name, $format = '') |
||
1398 | |||
1399 | |||
1400 | /** |
||
1401 | * @param $field_name |
||
1402 | * @param string $format |
||
1403 | * @throws ReflectionException |
||
1404 | * @throws InvalidArgumentException |
||
1405 | * @throws InvalidInterfaceException |
||
1406 | * @throws InvalidDataTypeException |
||
1407 | * @throws EE_Error |
||
1408 | */ |
||
1409 | public function e_time($field_name, $format = '') |
||
1413 | |||
1414 | |||
1415 | /** |
||
1416 | * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND |
||
1417 | * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
||
1418 | * other echoes the pretty value for dtt) |
||
1419 | * |
||
1420 | * @param string $field_name name of model object datetime field holding the value |
||
1421 | * @param string $dt_frmt format for the date returned (if NULL we use default in dt_frmt property) |
||
1422 | * @param string $tm_frmt format for the time returned (if NULL we use default in tm_frmt property) |
||
1423 | * @return string datetime value formatted |
||
1424 | * @throws ReflectionException |
||
1425 | * @throws InvalidArgumentException |
||
1426 | * @throws InvalidInterfaceException |
||
1427 | * @throws InvalidDataTypeException |
||
1428 | * @throws EE_Error |
||
1429 | */ |
||
1430 | public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
||
1434 | |||
1435 | |||
1436 | /** |
||
1437 | * @param string $field_name |
||
1438 | * @param string $dt_frmt |
||
1439 | * @param string $tm_frmt |
||
1440 | * @throws ReflectionException |
||
1441 | * @throws InvalidArgumentException |
||
1442 | * @throws InvalidInterfaceException |
||
1443 | * @throws InvalidDataTypeException |
||
1444 | * @throws EE_Error |
||
1445 | */ |
||
1446 | public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
||
1450 | |||
1451 | |||
1452 | /** |
||
1453 | * Get the i8ln value for a date using the WordPress @see date_i18n function. |
||
1454 | * |
||
1455 | * @param string $field_name The EE_Datetime_Field reference for the date being retrieved. |
||
1456 | * @param string $format PHP valid date/time string format. If none is provided then the internal set format |
||
1457 | * on the object will be used. |
||
1458 | * @return string Date and time string in set locale or false if no field exists for the given |
||
1459 | * @throws ReflectionException |
||
1460 | * @throws InvalidArgumentException |
||
1461 | * @throws InvalidInterfaceException |
||
1462 | * @throws InvalidDataTypeException |
||
1463 | * @throws EE_Error |
||
1464 | * field name. |
||
1465 | */ |
||
1466 | public function get_i18n_datetime($field_name, $format = '') |
||
1477 | |||
1478 | |||
1479 | /** |
||
1480 | * This method validates whether the given field name is a valid field on the model object as well as it is of a |
||
1481 | * type EE_Datetime_Field. On success there will be returned the field settings. On fail an EE_Error exception is |
||
1482 | * thrown. |
||
1483 | * |
||
1484 | * @param string $field_name The field name being checked |
||
1485 | * @throws ReflectionException |
||
1486 | * @throws InvalidArgumentException |
||
1487 | * @throws InvalidInterfaceException |
||
1488 | * @throws InvalidDataTypeException |
||
1489 | * @throws EE_Error |
||
1490 | * @return EE_Datetime_Field |
||
1491 | */ |
||
1492 | protected function _get_dtt_field_settings($field_name) |
||
1510 | |||
1511 | |||
1512 | |||
1513 | |||
1514 | /** |
||
1515 | * NOTE ABOUT BELOW: |
||
1516 | * These convenience date and time setters are for setting date and time independently. In other words you might |
||
1517 | * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand |
||
1518 | * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value) |
||
1519 | * method and make sure you send the entire datetime value for setting. |
||
1520 | */ |
||
1521 | /** |
||
1522 | * sets the time on a datetime property |
||
1523 | * |
||
1524 | * @access protected |
||
1525 | * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
||
1526 | * @param string $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field) |
||
1527 | * @throws ReflectionException |
||
1528 | * @throws InvalidArgumentException |
||
1529 | * @throws InvalidInterfaceException |
||
1530 | * @throws InvalidDataTypeException |
||
1531 | * @throws EE_Error |
||
1532 | */ |
||
1533 | protected function _set_time_for($time, $fieldname) |
||
1537 | |||
1538 | |||
1539 | /** |
||
1540 | * sets the date on a datetime property |
||
1541 | * |
||
1542 | * @access protected |
||
1543 | * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
||
1544 | * @param string $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field) |
||
1545 | * @throws ReflectionException |
||
1546 | * @throws InvalidArgumentException |
||
1547 | * @throws InvalidInterfaceException |
||
1548 | * @throws InvalidDataTypeException |
||
1549 | * @throws EE_Error |
||
1550 | */ |
||
1551 | protected function _set_date_for($date, $fieldname) |
||
1555 | |||
1556 | |||
1557 | /** |
||
1558 | * This takes care of setting a date or time independently on a given model object property. This method also |
||
1559 | * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field |
||
1560 | * |
||
1561 | * @access protected |
||
1562 | * @param string $what "T" for time, 'B' for both, 'D' for Date. |
||
1563 | * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object) |
||
1564 | * @param string $fieldname the name of the field the date OR time is being set on (must match a |
||
1565 | * EE_Datetime_Field property) |
||
1566 | * @throws ReflectionException |
||
1567 | * @throws InvalidArgumentException |
||
1568 | * @throws InvalidInterfaceException |
||
1569 | * @throws InvalidDataTypeException |
||
1570 | * @throws EE_Error |
||
1571 | */ |
||
1572 | protected function _set_date_time($what = 'T', $datetime_value, $fieldname) |
||
1597 | |||
1598 | |||
1599 | /** |
||
1600 | * This will return a timestamp for the website timezone but ONLY when the current website timezone is different |
||
1601 | * than the timezone set for the website. NOTE, this currently only works well with methods that return values. If |
||
1602 | * you use it with methods that echo values the $_timestamp property may not get reset to its original value and |
||
1603 | * that could lead to some unexpected results! |
||
1604 | * |
||
1605 | * @access public |
||
1606 | * @param string $field_name This is the name of the field on the object that contains the date/time |
||
1607 | * value being returned. |
||
1608 | * @param string $callback must match a valid method in this class (defaults to get_datetime) |
||
1609 | * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
||
1610 | * @param string $prepend You can include something to prepend on the timestamp |
||
1611 | * @param string $append You can include something to append on the timestamp |
||
1612 | * @throws ReflectionException |
||
1613 | * @throws InvalidArgumentException |
||
1614 | * @throws InvalidInterfaceException |
||
1615 | * @throws InvalidDataTypeException |
||
1616 | * @throws EE_Error |
||
1617 | * @return string timestamp |
||
1618 | */ |
||
1619 | public function display_in_my_timezone( |
||
1650 | |||
1651 | |||
1652 | /** |
||
1653 | * Deletes this model object. |
||
1654 | * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
||
1655 | * override |
||
1656 | * `EE_Base_Class::_delete` NOT this class. |
||
1657 | * |
||
1658 | * @return boolean | int |
||
1659 | * @throws ReflectionException |
||
1660 | * @throws InvalidArgumentException |
||
1661 | * @throws InvalidInterfaceException |
||
1662 | * @throws InvalidDataTypeException |
||
1663 | * @throws EE_Error |
||
1664 | */ |
||
1665 | public function delete() |
||
1693 | |||
1694 | |||
1695 | /** |
||
1696 | * Calls the specific delete method for the instantiated class. |
||
1697 | * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
||
1698 | * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
||
1699 | * `EE_Base_Class::delete` |
||
1700 | * |
||
1701 | * @return bool|int |
||
1702 | * @throws ReflectionException |
||
1703 | * @throws InvalidArgumentException |
||
1704 | * @throws InvalidInterfaceException |
||
1705 | * @throws InvalidDataTypeException |
||
1706 | * @throws EE_Error |
||
1707 | */ |
||
1708 | protected function _delete() |
||
1712 | |||
1713 | |||
1714 | /** |
||
1715 | * Deletes this model object permanently from db |
||
1716 | * (but keep in mind related models may block the delete and return an error) |
||
1717 | * |
||
1718 | * @return bool | int |
||
1719 | * @throws ReflectionException |
||
1720 | * @throws InvalidArgumentException |
||
1721 | * @throws InvalidInterfaceException |
||
1722 | * @throws InvalidDataTypeException |
||
1723 | * @throws EE_Error |
||
1724 | */ |
||
1725 | public function delete_permanently() |
||
1745 | |||
1746 | |||
1747 | /** |
||
1748 | * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
||
1749 | * related model objects |
||
1750 | * |
||
1751 | * @throws ReflectionException |
||
1752 | * @throws InvalidArgumentException |
||
1753 | * @throws InvalidInterfaceException |
||
1754 | * @throws InvalidDataTypeException |
||
1755 | * @throws EE_Error |
||
1756 | */ |
||
1757 | public function refresh_cache_of_related_objects() |
||
1780 | |||
1781 | |||
1782 | /** |
||
1783 | * Saves this object to the database. An array may be supplied to set some values on this |
||
1784 | * object just before saving. |
||
1785 | * |
||
1786 | * @access public |
||
1787 | * @param array $set_cols_n_values keys are field names, values are their new values, |
||
1788 | * if provided during the save() method (often client code will change the fields' |
||
1789 | * values before calling save) |
||
1790 | * @throws InvalidArgumentException |
||
1791 | * @throws InvalidInterfaceException |
||
1792 | * @throws InvalidDataTypeException |
||
1793 | * @throws EE_Error |
||
1794 | * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object |
||
1795 | * isn't allowed to persist (as determined by EE_Base_Class::allow_persist()) |
||
1796 | * @throws ReflectionException |
||
1797 | * @throws ReflectionException |
||
1798 | * @throws ReflectionException |
||
1799 | */ |
||
1800 | public function save($set_cols_n_values = array()) |
||
1922 | |||
1923 | |||
1924 | /** |
||
1925 | * Updates the foreign key on related models objects pointing to this to have this model object's ID |
||
1926 | * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
||
1927 | * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
||
1928 | * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
||
1929 | * saved it to the db. We also create a registration and don't save it to the DB, but we DO cache it on the |
||
1930 | * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
||
1931 | * or not they exist in the DB (if they do, their DB records will be automatically updated) |
||
1932 | * |
||
1933 | * @return void |
||
1934 | * @throws ReflectionException |
||
1935 | * @throws InvalidArgumentException |
||
1936 | * @throws InvalidInterfaceException |
||
1937 | * @throws InvalidDataTypeException |
||
1938 | * @throws EE_Error |
||
1939 | */ |
||
1940 | protected function _update_cached_related_model_objs_fks() |
||
1957 | |||
1958 | |||
1959 | /** |
||
1960 | * Saves this model object and its NEW cached relations to the database. |
||
1961 | * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
||
1962 | * In order for that to work, we would need to mark model objects as dirty/clean... |
||
1963 | * because otherwise, there's a potential for infinite looping of saving |
||
1964 | * Saves the cached related model objects, and ensures the relation between them |
||
1965 | * and this object and properly setup |
||
1966 | * |
||
1967 | * @return int ID of new model object on save; 0 on failure+ |
||
1968 | * @throws ReflectionException |
||
1969 | * @throws InvalidArgumentException |
||
1970 | * @throws InvalidInterfaceException |
||
1971 | * @throws InvalidDataTypeException |
||
1972 | * @throws EE_Error |
||
1973 | */ |
||
1974 | public function save_new_cached_related_model_objs() |
||
2010 | |||
2011 | |||
2012 | /** |
||
2013 | * for getting a model while instantiated. |
||
2014 | * |
||
2015 | * @return EEM_Base | EEM_CPT_Base |
||
2016 | * @throws ReflectionException |
||
2017 | * @throws InvalidArgumentException |
||
2018 | * @throws InvalidInterfaceException |
||
2019 | * @throws InvalidDataTypeException |
||
2020 | * @throws EE_Error |
||
2021 | */ |
||
2022 | public function get_model() |
||
2032 | |||
2033 | |||
2034 | /** |
||
2035 | * @param $props_n_values |
||
2036 | * @param $classname |
||
2037 | * @return mixed bool|EE_Base_Class|EEM_CPT_Base |
||
2038 | * @throws ReflectionException |
||
2039 | * @throws InvalidArgumentException |
||
2040 | * @throws InvalidInterfaceException |
||
2041 | * @throws InvalidDataTypeException |
||
2042 | * @throws EE_Error |
||
2043 | */ |
||
2044 | protected static function _get_object_from_entity_mapper($props_n_values, $classname) |
||
2056 | |||
2057 | |||
2058 | /** |
||
2059 | * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
||
2060 | * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
||
2061 | * primary key for the model AND it is not null, then we check the db. If there's a an object we return it. If not |
||
2062 | * we return false. |
||
2063 | * |
||
2064 | * @param array $props_n_values incoming array of properties and their values |
||
2065 | * @param string $classname the classname of the child class |
||
2066 | * @param null $timezone |
||
2067 | * @param array $date_formats incoming date_formats in an array where the first value is the |
||
2068 | * date_format and the second value is the time format |
||
2069 | * @return mixed (EE_Base_Class|bool) |
||
2070 | * @throws InvalidArgumentException |
||
2071 | * @throws InvalidInterfaceException |
||
2072 | * @throws InvalidDataTypeException |
||
2073 | * @throws EE_Error |
||
2074 | * @throws ReflectionException |
||
2075 | * @throws ReflectionException |
||
2076 | * @throws ReflectionException |
||
2077 | */ |
||
2078 | protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array()) |
||
2115 | |||
2116 | |||
2117 | /** |
||
2118 | * Gets the EEM_*_Model for this class |
||
2119 | * |
||
2120 | * @access public now, as this is more convenient |
||
2121 | * @param $classname |
||
2122 | * @param null $timezone |
||
2123 | * @throws ReflectionException |
||
2124 | * @throws InvalidArgumentException |
||
2125 | * @throws InvalidInterfaceException |
||
2126 | * @throws InvalidDataTypeException |
||
2127 | * @throws EE_Error |
||
2128 | * @return EEM_Base |
||
2129 | */ |
||
2130 | protected static function _get_model($classname, $timezone = null) |
||
2147 | |||
2148 | |||
2149 | /** |
||
2150 | * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
||
2151 | * |
||
2152 | * @param string $model_classname |
||
2153 | * @param null $timezone |
||
2154 | * @return EEM_Base |
||
2155 | * @throws ReflectionException |
||
2156 | * @throws InvalidArgumentException |
||
2157 | * @throws InvalidInterfaceException |
||
2158 | * @throws InvalidDataTypeException |
||
2159 | * @throws EE_Error |
||
2160 | */ |
||
2161 | protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
||
2168 | |||
2169 | |||
2170 | /** |
||
2171 | * If a model name is provided (eg Registration), gets the model classname for that model. |
||
2172 | * Also works if a model class's classname is provided (eg EE_Registration). |
||
2173 | * |
||
2174 | * @param null $model_name |
||
2175 | * @return string like EEM_Attendee |
||
2176 | */ |
||
2177 | private static function _get_model_classname($model_name = null) |
||
2186 | |||
2187 | |||
2188 | /** |
||
2189 | * returns the name of the primary key attribute |
||
2190 | * |
||
2191 | * @param null $classname |
||
2192 | * @throws ReflectionException |
||
2193 | * @throws InvalidArgumentException |
||
2194 | * @throws InvalidInterfaceException |
||
2195 | * @throws InvalidDataTypeException |
||
2196 | * @throws EE_Error |
||
2197 | * @return string |
||
2198 | */ |
||
2199 | protected static function _get_primary_key_name($classname = null) |
||
2211 | |||
2212 | |||
2213 | /** |
||
2214 | * Gets the value of the primary key. |
||
2215 | * If the object hasn't yet been saved, it should be whatever the model field's default was |
||
2216 | * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
||
2217 | * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
||
2218 | * |
||
2219 | * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
||
2220 | * @throws ReflectionException |
||
2221 | * @throws InvalidArgumentException |
||
2222 | * @throws InvalidInterfaceException |
||
2223 | * @throws InvalidDataTypeException |
||
2224 | * @throws EE_Error |
||
2225 | */ |
||
2226 | public function ID() |
||
2235 | |||
2236 | |||
2237 | /** |
||
2238 | * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
||
2239 | * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
||
2240 | * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
||
2241 | * |
||
2242 | * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
||
2243 | * @param string $relationName eg 'Events','Question',etc. |
||
2244 | * an attendee to a group, you also want to specify which role they |
||
2245 | * will have in that group. So you would use this parameter to |
||
2246 | * specify array('role-column-name'=>'role-id') |
||
2247 | * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
||
2248 | * allow you to further constrict the relation to being added. |
||
2249 | * However, keep in mind that the columns (keys) given must match a |
||
2250 | * column on the JOIN table and currently only the HABTM models |
||
2251 | * accept these additional conditions. Also remember that if an |
||
2252 | * exact match isn't found for these extra cols/val pairs, then a |
||
2253 | * NEW row is created in the join table. |
||
2254 | * @param null $cache_id |
||
2255 | * @throws ReflectionException |
||
2256 | * @throws InvalidArgumentException |
||
2257 | * @throws InvalidInterfaceException |
||
2258 | * @throws InvalidDataTypeException |
||
2259 | * @throws EE_Error |
||
2260 | * @return EE_Base_Class the object the relation was added to |
||
2261 | */ |
||
2262 | public function _add_relation_to( |
||
2316 | |||
2317 | |||
2318 | /** |
||
2319 | * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
||
2320 | * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
||
2321 | * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
||
2322 | * from the cache |
||
2323 | * |
||
2324 | * @param mixed $otherObjectModelObjectOrID |
||
2325 | * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
||
2326 | * to the DB yet |
||
2327 | * @param string $relationName |
||
2328 | * @param array $where_query |
||
2329 | * You can optionally include an array of key=>value pairs that allow you to further constrict the |
||
2330 | * relation to being added. However, keep in mind that the columns (keys) given must match a column |
||
2331 | * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
||
2332 | * remember that if an exact match isn't found for these extra cols/val pairs, then no row is |
||
2333 | * deleted. |
||
2334 | * @return EE_Base_Class the relation was removed from |
||
2335 | * @throws ReflectionException |
||
2336 | * @throws InvalidArgumentException |
||
2337 | * @throws InvalidInterfaceException |
||
2338 | * @throws InvalidDataTypeException |
||
2339 | * @throws EE_Error |
||
2340 | */ |
||
2341 | public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array()) |
||
2370 | |||
2371 | |||
2372 | /** |
||
2373 | * Removes ALL the related things for the $relationName. |
||
2374 | * |
||
2375 | * @param string $relationName |
||
2376 | * @param array $where_query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
||
2377 | * @return EE_Base_Class |
||
2378 | * @throws ReflectionException |
||
2379 | * @throws InvalidArgumentException |
||
2380 | * @throws InvalidInterfaceException |
||
2381 | * @throws InvalidDataTypeException |
||
2382 | * @throws EE_Error |
||
2383 | */ |
||
2384 | public function _remove_relations($relationName, $where_query_params = array()) |
||
2416 | |||
2417 | |||
2418 | /** |
||
2419 | * Gets all the related model objects of the specified type. Eg, if the current class if |
||
2420 | * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
||
2421 | * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
||
2422 | * because we want to get even deleted items etc. |
||
2423 | * |
||
2424 | * @param string $relationName key in the model's _model_relations array |
||
2425 | * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions |
||
2426 | * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary |
||
2427 | * keys or might not be saved yet. Consider using EEM_Base::get_IDs() on these |
||
2428 | * results if you want IDs |
||
2429 | * @throws ReflectionException |
||
2430 | * @throws InvalidArgumentException |
||
2431 | * @throws InvalidInterfaceException |
||
2432 | * @throws InvalidDataTypeException |
||
2433 | * @throws EE_Error |
||
2434 | */ |
||
2435 | public function get_many_related($relationName, $query_params = array()) |
||
2470 | |||
2471 | |||
2472 | /** |
||
2473 | * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
||
2474 | * unless otherwise specified in the $query_params |
||
2475 | * |
||
2476 | * @param string $relation_name model_name like 'Event', or 'Registration' |
||
2477 | * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
||
2478 | * @param string $field_to_count name of field to count by. By default, uses primary key |
||
2479 | * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
||
2480 | * that by the setting $distinct to TRUE; |
||
2481 | * @return int |
||
2482 | * @throws ReflectionException |
||
2483 | * @throws InvalidArgumentException |
||
2484 | * @throws InvalidInterfaceException |
||
2485 | * @throws InvalidDataTypeException |
||
2486 | * @throws EE_Error |
||
2487 | */ |
||
2488 | public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false) |
||
2498 | |||
2499 | |||
2500 | /** |
||
2501 | * Instead of getting the related model objects, simply sums up the values of the specified field. |
||
2502 | * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
||
2503 | * |
||
2504 | * @param string $relation_name model_name like 'Event', or 'Registration' |
||
2505 | * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
||
2506 | * @param string $field_to_sum name of field to count by. |
||
2507 | * By default, uses primary key |
||
2508 | * (which doesn't make much sense, so you should probably change it) |
||
2509 | * @return int |
||
2510 | * @throws ReflectionException |
||
2511 | * @throws InvalidArgumentException |
||
2512 | * @throws InvalidInterfaceException |
||
2513 | * @throws InvalidDataTypeException |
||
2514 | * @throws EE_Error |
||
2515 | */ |
||
2516 | public function sum_related($relation_name, $query_params = array(), $field_to_sum = null) |
||
2525 | |||
2526 | |||
2527 | /** |
||
2528 | * Gets the first (ie, one) related model object of the specified type. |
||
2529 | * |
||
2530 | * @param string $relationName key in the model's _model_relations array |
||
2531 | * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
||
2532 | * @return EE_Base_Class (not an array, a single object) |
||
2533 | * @throws ReflectionException |
||
2534 | * @throws InvalidArgumentException |
||
2535 | * @throws InvalidInterfaceException |
||
2536 | * @throws InvalidDataTypeException |
||
2537 | * @throws EE_Error |
||
2538 | */ |
||
2539 | public function get_first_related($relationName, $query_params = array()) |
||
2589 | |||
2590 | |||
2591 | /** |
||
2592 | * Does a delete on all related objects of type $relationName and removes |
||
2593 | * the current model object's relation to them. If they can't be deleted (because |
||
2594 | * of blocking related model objects) does nothing. If the related model objects are |
||
2595 | * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
||
2596 | * If this model object doesn't exist yet in the DB, just removes its related things |
||
2597 | * |
||
2598 | * @param string $relationName |
||
2599 | * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
||
2600 | * @return int how many deleted |
||
2601 | * @throws ReflectionException |
||
2602 | * @throws InvalidArgumentException |
||
2603 | * @throws InvalidInterfaceException |
||
2604 | * @throws InvalidDataTypeException |
||
2605 | * @throws EE_Error |
||
2606 | */ |
||
2607 | View Code Duplication | public function delete_related($relationName, $query_params = array()) |
|
2621 | |||
2622 | |||
2623 | /** |
||
2624 | * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
||
2625 | * the current model object's relation to them. If they can't be deleted (because |
||
2626 | * of blocking related model objects) just does a soft delete on it instead, if possible. |
||
2627 | * If the related thing isn't a soft-deletable model object, this function is identical |
||
2628 | * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
||
2629 | * |
||
2630 | * @param string $relationName |
||
2631 | * @param array $query_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md |
||
2632 | * @return int how many deleted (including those soft deleted) |
||
2633 | * @throws ReflectionException |
||
2634 | * @throws InvalidArgumentException |
||
2635 | * @throws InvalidInterfaceException |
||
2636 | * @throws InvalidDataTypeException |
||
2637 | * @throws EE_Error |
||
2638 | */ |
||
2639 | View Code Duplication | public function delete_related_permanently($relationName, $query_params = array()) |
|
2653 | |||
2654 | |||
2655 | /** |
||
2656 | * is_set |
||
2657 | * Just a simple utility function children can use for checking if property exists |
||
2658 | * |
||
2659 | * @access public |
||
2660 | * @param string $field_name property to check |
||
2661 | * @return bool TRUE if existing,FALSE if not. |
||
2662 | */ |
||
2663 | public function is_set($field_name) |
||
2667 | |||
2668 | |||
2669 | /** |
||
2670 | * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
||
2671 | * EE_Error exception if they don't |
||
2672 | * |
||
2673 | * @param mixed (string|array) $properties properties to check |
||
2674 | * @throws EE_Error |
||
2675 | * @return bool TRUE if existing, throw EE_Error if not. |
||
2676 | */ |
||
2677 | protected function _property_exists($properties) |
||
2695 | |||
2696 | |||
2697 | /** |
||
2698 | * This simply returns an array of model fields for this object |
||
2699 | * |
||
2700 | * @return array |
||
2701 | * @throws ReflectionException |
||
2702 | * @throws InvalidArgumentException |
||
2703 | * @throws InvalidInterfaceException |
||
2704 | * @throws InvalidDataTypeException |
||
2705 | * @throws EE_Error |
||
2706 | */ |
||
2707 | public function model_field_array() |
||
2717 | |||
2718 | |||
2719 | /** |
||
2720 | * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
||
2721 | * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
||
2722 | * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. |
||
2723 | * Instead of requiring a plugin to extend the EE_Base_Class |
||
2724 | * (which works fine is there's only 1 plugin, but when will that happen?) |
||
2725 | * they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' |
||
2726 | * (eg, filters_hook_espresso__EE_Answer__my_great_function) |
||
2727 | * and accepts 2 arguments: the object on which the function was called, |
||
2728 | * and an array of the original arguments passed to the function. |
||
2729 | * Whatever their callback function returns will be returned by this function. |
||
2730 | * Example: in functions.php (or in a plugin): |
||
2731 | * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); |
||
2732 | * function my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
||
2733 | * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
||
2734 | * return $previousReturnValue.$returnString; |
||
2735 | * } |
||
2736 | * require('EE_Answer.class.php'); |
||
2737 | * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
||
2738 | * echo $answer->my_callback('monkeys',100); |
||
2739 | * //will output "you called my_callback! and passed args:monkeys,100" |
||
2740 | * |
||
2741 | * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
||
2742 | * @param array $args array of original arguments passed to the function |
||
2743 | * @throws EE_Error |
||
2744 | * @return mixed whatever the plugin which calls add_filter decides |
||
2745 | */ |
||
2746 | public function __call($methodName, $args) |
||
2765 | |||
2766 | |||
2767 | /** |
||
2768 | * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
||
2769 | * A $previous_value can be specified in case there are many meta rows with the same key |
||
2770 | * |
||
2771 | * @param string $meta_key |
||
2772 | * @param mixed $meta_value |
||
2773 | * @param mixed $previous_value |
||
2774 | * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
||
2775 | * NOTE: if the values haven't changed, returns 0 |
||
2776 | * @throws InvalidArgumentException |
||
2777 | * @throws InvalidInterfaceException |
||
2778 | * @throws InvalidDataTypeException |
||
2779 | * @throws EE_Error |
||
2780 | * @throws ReflectionException |
||
2781 | */ |
||
2782 | public function update_extra_meta($meta_key, $meta_value, $previous_value = null) |
||
2803 | |||
2804 | |||
2805 | /** |
||
2806 | * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
||
2807 | * no other extra meta for this model object have the same key. Returns TRUE if the |
||
2808 | * extra meta row was entered, false if not |
||
2809 | * |
||
2810 | * @param string $meta_key |
||
2811 | * @param mixed $meta_value |
||
2812 | * @param boolean $unique |
||
2813 | * @return boolean |
||
2814 | * @throws InvalidArgumentException |
||
2815 | * @throws InvalidInterfaceException |
||
2816 | * @throws InvalidDataTypeException |
||
2817 | * @throws EE_Error |
||
2818 | * @throws ReflectionException |
||
2819 | * @throws ReflectionException |
||
2820 | */ |
||
2821 | public function add_extra_meta($meta_key, $meta_value, $unique = false) |
||
2848 | |||
2849 | |||
2850 | /** |
||
2851 | * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
||
2852 | * is specified, only deletes extra meta records with that value. |
||
2853 | * |
||
2854 | * @param string $meta_key |
||
2855 | * @param mixed $meta_value |
||
2856 | * @return int number of extra meta rows deleted |
||
2857 | * @throws InvalidArgumentException |
||
2858 | * @throws InvalidInterfaceException |
||
2859 | * @throws InvalidDataTypeException |
||
2860 | * @throws EE_Error |
||
2861 | * @throws ReflectionException |
||
2862 | */ |
||
2863 | public function delete_extra_meta($meta_key, $meta_value = null) |
||
2877 | |||
2878 | |||
2879 | /** |
||
2880 | * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
||
2881 | * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
||
2882 | * You can specify $default is case you haven't found the extra meta |
||
2883 | * |
||
2884 | * @param string $meta_key |
||
2885 | * @param boolean $single |
||
2886 | * @param mixed $default if we don't find anything, what should we return? |
||
2887 | * @return mixed single value if $single; array if ! $single |
||
2888 | * @throws ReflectionException |
||
2889 | * @throws InvalidArgumentException |
||
2890 | * @throws InvalidInterfaceException |
||
2891 | * @throws InvalidDataTypeException |
||
2892 | * @throws EE_Error |
||
2893 | */ |
||
2894 | public function get_extra_meta($meta_key, $single = false, $default = null) |
||
2928 | |||
2929 | |||
2930 | /** |
||
2931 | * Returns a simple array of all the extra meta associated with this model object. |
||
2932 | * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
||
2933 | * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
||
2934 | * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
||
2935 | * If $one_of_each_key is false, it will return an array with the top-level keys being |
||
2936 | * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
||
2937 | * finally the extra meta's value as each sub-value. (eg |
||
2938 | * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
||
2939 | * |
||
2940 | * @param boolean $one_of_each_key |
||
2941 | * @return array |
||
2942 | * @throws ReflectionException |
||
2943 | * @throws InvalidArgumentException |
||
2944 | * @throws InvalidInterfaceException |
||
2945 | * @throws InvalidDataTypeException |
||
2946 | * @throws EE_Error |
||
2947 | */ |
||
2948 | public function all_extra_meta_array($one_of_each_key = true) |
||
2974 | |||
2975 | |||
2976 | /** |
||
2977 | * Gets a pretty nice displayable nice for this model object. Often overridden |
||
2978 | * |
||
2979 | * @return string |
||
2980 | * @throws ReflectionException |
||
2981 | * @throws InvalidArgumentException |
||
2982 | * @throws InvalidInterfaceException |
||
2983 | * @throws InvalidDataTypeException |
||
2984 | * @throws EE_Error |
||
2985 | */ |
||
2986 | public function name() |
||
3001 | |||
3002 | |||
3003 | /** |
||
3004 | * in_entity_map |
||
3005 | * Checks if this model object has been proven to already be in the entity map |
||
3006 | * |
||
3007 | * @return boolean |
||
3008 | * @throws ReflectionException |
||
3009 | * @throws InvalidArgumentException |
||
3010 | * @throws InvalidInterfaceException |
||
3011 | * @throws InvalidDataTypeException |
||
3012 | * @throws EE_Error |
||
3013 | */ |
||
3014 | public function in_entity_map() |
||
3019 | |||
3020 | |||
3021 | /** |
||
3022 | * refresh_from_db |
||
3023 | * Makes sure the fields and values on this model object are in-sync with what's in the database. |
||
3024 | * |
||
3025 | * @throws ReflectionException |
||
3026 | * @throws InvalidArgumentException |
||
3027 | * @throws InvalidInterfaceException |
||
3028 | * @throws InvalidDataTypeException |
||
3029 | * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
||
3030 | * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
||
3031 | */ |
||
3032 | public function refresh_from_db() |
||
3056 | |||
3057 | /** |
||
3058 | * Change $fields' values to $new_value_sql (which is a string of raw SQL) |
||
3059 | * @since $VID:$ |
||
3060 | * @param EE_Model_Field_Base[] $fields |
||
3061 | * @param string $new_value_sql eg 'column_name=123', or 'column_name=column_name+1', or |
||
3062 | * 'column_name= CASE |
||
3063 | WHEN (`column_name` + `other_column` + 5) <= `yet_another_column` |
||
3064 | THEN `column_name` + 5 |
||
3065 | ELSE `column_name` |
||
3066 | END' |
||
3067 | * Also updates $field on this model object with the latest value from the database. |
||
3068 | * @return bool |
||
3069 | * @throws EE_Error |
||
3070 | * @throws InvalidArgumentException |
||
3071 | * @throws InvalidDataTypeException |
||
3072 | * @throws InvalidInterfaceException |
||
3073 | * @throws ReflectionException |
||
3074 | */ |
||
3075 | protected function updateFieldsInDB($fields, $new_value_sql) |
||
3140 | |||
3141 | /** |
||
3142 | * Nudges $field_name's value by $quantity, without any conditionals (in comparison to bumpConditionally()). |
||
3143 | * Does not allow negative values, however. |
||
3144 | * @since $VID:$ |
||
3145 | * @param array $fields_n_quantities keys are the field names, and values are the amount by which to bump them |
||
3146 | * (positive or negative). One important gotcha: all these values must be |
||
3147 | * on the same table (eg don't pass in one field for the posts table and |
||
3148 | * another for the event meta table.) |
||
3149 | * @return bool |
||
3150 | * @throws EE_Error |
||
3151 | * @throws InvalidArgumentException |
||
3152 | * @throws InvalidDataTypeException |
||
3153 | * @throws InvalidInterfaceException |
||
3154 | * @throws ReflectionException |
||
3155 | */ |
||
3156 | public function bump(array $fields_n_quantities) |
||
3196 | |||
3197 | /** |
||
3198 | * Increases the value of the field $field_name_to_bump by $quantity, but only if the values of |
||
3199 | * $field_name_to_bump plus $field_name_affecting_total and $quantity won't exceed $limit_field_name's value. |
||
3200 | * For example, this is useful when bumping the value of TKT_reserved, TKT_sold, DTT_reserved or DTT_sold. |
||
3201 | * Returns true if the value was successfully bumped, and updates the value on this model object. |
||
3202 | * Otherwise returns false. |
||
3203 | * @since $VID:$ |
||
3204 | * @param string $field_name_to_bump |
||
3205 | * @param string $field_name_affecting_total |
||
3206 | * @param string $limit_field_name |
||
3207 | * @param int $quantity |
||
3208 | * @return bool |
||
3209 | * @throws EE_Error |
||
3210 | * @throws InvalidArgumentException |
||
3211 | * @throws InvalidDataTypeException |
||
3212 | * @throws InvalidInterfaceException |
||
3213 | * @throws ReflectionException |
||
3214 | */ |
||
3215 | public function bumpConditionally($field_name_to_bump, $field_name_affecting_total, $limit_field_name, $quantity) |
||
3242 | |||
3243 | |||
3244 | /** |
||
3245 | * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
||
3246 | * (probably a bad assumption they have made, oh well) |
||
3247 | * |
||
3248 | * @return string |
||
3249 | */ |
||
3250 | public function __toString() |
||
3259 | |||
3260 | |||
3261 | /** |
||
3262 | * Clear related model objects if they're already in the DB, because otherwise when we |
||
3263 | * UN-serialize this model object we'll need to be careful to add them to the entity map. |
||
3264 | * This means if we have made changes to those related model objects, and want to unserialize |
||
3265 | * the this model object on a subsequent request, changes to those related model objects will be lost. |
||
3266 | * Instead, those related model objects should be directly serialized and stored. |
||
3267 | * Eg, the following won't work: |
||
3268 | * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
||
3269 | * $att = $reg->attendee(); |
||
3270 | * $att->set( 'ATT_fname', 'Dirk' ); |
||
3271 | * update_option( 'my_option', serialize( $reg ) ); |
||
3272 | * //END REQUEST |
||
3273 | * //START NEXT REQUEST |
||
3274 | * $reg = get_option( 'my_option' ); |
||
3275 | * $reg->attendee()->save(); |
||
3276 | * And would need to be replace with: |
||
3277 | * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
||
3278 | * $att = $reg->attendee(); |
||
3279 | * $att->set( 'ATT_fname', 'Dirk' ); |
||
3280 | * update_option( 'my_option', serialize( $reg ) ); |
||
3281 | * //END REQUEST |
||
3282 | * //START NEXT REQUEST |
||
3283 | * $att = get_option( 'my_option' ); |
||
3284 | * $att->save(); |
||
3285 | * |
||
3286 | * @return array |
||
3287 | * @throws ReflectionException |
||
3288 | * @throws InvalidArgumentException |
||
3289 | * @throws InvalidInterfaceException |
||
3290 | * @throws InvalidDataTypeException |
||
3291 | * @throws EE_Error |
||
3292 | */ |
||
3293 | public function __sleep() |
||
3315 | |||
3316 | |||
3317 | /** |
||
3318 | * restore _props_n_values_provided_in_constructor |
||
3319 | * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
||
3320 | * and therefore should NOT be used to determine if state change has occurred since initial construction. |
||
3321 | * At best, you would only be able to detect if state change has occurred during THIS request. |
||
3322 | */ |
||
3323 | public function __wakeup() |
||
3327 | |||
3328 | |||
3329 | /** |
||
3330 | * Usage of this magic method is to ensure any internally cached references to object instances that must remain |
||
3331 | * distinct with the clone host instance are also cloned. |
||
3332 | */ |
||
3333 | public function __clone() |
||
3342 | } |
||
3343 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.