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 |
||
| 15 | abstract class EE_Base_Class |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * This is an array of the original properties and values provided during construction |
||
| 20 | * of this model object. (keys are model field names, values are their values). |
||
| 21 | * This list is important to remember so that when we are merging data from the db, we know |
||
| 22 | * which values to override and which to not override. |
||
| 23 | * |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $_props_n_values_provided_in_constructor; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Timezone |
||
| 30 | * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in. |
||
| 31 | * This can also be used before a get to set what timezone you want strings coming out of the object to be in. NOT |
||
| 32 | * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have |
||
| 33 | * access to it. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $_timezone; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * date format |
||
| 41 | * pattern or format for displaying dates |
||
| 42 | * |
||
| 43 | * @var string $_dt_frmt |
||
| 44 | */ |
||
| 45 | protected $_dt_frmt; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * time format |
||
| 49 | * pattern or format for displaying time |
||
| 50 | * |
||
| 51 | * @var string $_tm_frmt |
||
| 52 | */ |
||
| 53 | protected $_tm_frmt; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * This property is for holding a cached array of object properties indexed by property name as the key. |
||
| 57 | * The purpose of this is for setting a cache on properties that may have calculated values after a |
||
| 58 | * prepare_for_get. That way the cache can be checked first and the calculated property returned instead of having |
||
| 59 | * to recalculate. Used by _set_cached_property() and _get_cached_property() methods. |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $_cached_properties = array(); |
||
| 64 | |||
| 65 | /** |
||
| 66 | * An array containing keys of the related model, and values are either an array of related mode objects or a |
||
| 67 | * single |
||
| 68 | * related model object. see the model's _model_relations. The keys should match those specified. And if the |
||
| 69 | * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object, |
||
| 70 | * all others have an array) |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $_model_relations = array(); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Array where keys are field names (see the model's _fields property) and values are their values. To see what |
||
| 78 | * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods) |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | protected $_fields = array(); |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var boolean indicating whether or not this model object is intended to ever be saved |
||
| 86 | * For example, we might create model objects intended to only be used for the duration |
||
| 87 | * of this request and to be thrown away, and if they were accidentally saved |
||
| 88 | * it would be a bug. |
||
| 89 | */ |
||
| 90 | protected $_allow_persist = true; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var boolean indicating whether or not this model object's properties have changed since construction |
||
| 94 | */ |
||
| 95 | protected $_has_changes = false; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var EEM_Base |
||
| 99 | */ |
||
| 100 | protected $_model; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * This is a cache of results from custom selections done on a query that constructs this entity. The only purpose |
||
| 104 | * for these values is for retrieval of the results, they are not further queryable and they are not persisted to |
||
| 105 | * the db. They also do not automatically update if there are any changes to the data that produced their results. |
||
| 106 | * The format is a simple array of field_alias => field_value. So for instance if a custom select was something |
||
| 107 | * like, "Select COUNT(Registration.REG_ID) as Registration_Count ...", then the resulting value will be in this |
||
| 108 | * array as: |
||
| 109 | * array( |
||
| 110 | * 'Registration_Count' => 24 |
||
| 111 | * ); |
||
| 112 | * Note: if the custom select configuration for the query included a data type, the value will be in the data type |
||
| 113 | * provided for the query (@see EventEspresso\core\domain\values\model\CustomSelects::__construct phpdocs for more |
||
| 114 | * info) |
||
| 115 | * |
||
| 116 | * @var array |
||
| 117 | */ |
||
| 118 | protected $custom_selection_results = array(); |
||
| 119 | |||
| 120 | |||
| 121 | /** |
||
| 122 | * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
||
| 123 | * play nice |
||
| 124 | * |
||
| 125 | * @param array $fieldValues where each key is a field (ie, array key in the 2nd |
||
| 126 | * layer of the model's _fields array, (eg, EVT_ID, |
||
| 127 | * TXN_amount, QST_name, etc) and values are their values |
||
| 128 | * @param boolean $bydb a flag for setting if the class is instantiated by the |
||
| 129 | * corresponding db model or not. |
||
| 130 | * @param string $timezone indicate what timezone you want any datetime fields to |
||
| 131 | * be in when instantiating a EE_Base_Class object. |
||
| 132 | * @param array $date_formats An array of date formats to set on construct where first |
||
| 133 | * value is the date_format and second value is the time |
||
| 134 | * format. |
||
| 135 | * @throws InvalidArgumentException |
||
| 136 | * @throws InvalidInterfaceException |
||
| 137 | * @throws InvalidDataTypeException |
||
| 138 | * @throws EE_Error |
||
| 139 | * @throws ReflectionException |
||
| 140 | */ |
||
| 141 | protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array()) |
||
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * Gets whether or not this model object is allowed to persist/be saved to the database. |
||
| 217 | * |
||
| 218 | * @return boolean |
||
| 219 | */ |
||
| 220 | public function allow_persist() |
||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * Sets whether or not this model object should be allowed to be saved to the DB. |
||
| 228 | * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless |
||
| 229 | * you got new information that somehow made you change your mind. |
||
| 230 | * |
||
| 231 | * @param boolean $allow_persist |
||
| 232 | * @return boolean |
||
| 233 | */ |
||
| 234 | public function set_allow_persist($allow_persist) |
||
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * Gets the field's original value when this object was constructed during this request. |
||
| 242 | * This can be helpful when determining if a model object has changed or not |
||
| 243 | * |
||
| 244 | * @param string $field_name |
||
| 245 | * @return mixed|null |
||
| 246 | * @throws ReflectionException |
||
| 247 | * @throws InvalidArgumentException |
||
| 248 | * @throws InvalidInterfaceException |
||
| 249 | * @throws InvalidDataTypeException |
||
| 250 | * @throws EE_Error |
||
| 251 | */ |
||
| 252 | public function get_original($field_name) |
||
| 261 | |||
| 262 | |||
| 263 | /** |
||
| 264 | * @param EE_Base_Class $obj |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public function get_class($obj) |
||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * Overrides parent because parent expects old models. |
||
| 275 | * This also doesn't do any validation, and won't work for serialized arrays |
||
| 276 | * |
||
| 277 | * @param string $field_name |
||
| 278 | * @param mixed $field_value |
||
| 279 | * @param bool $use_default |
||
| 280 | * @throws InvalidArgumentException |
||
| 281 | * @throws InvalidInterfaceException |
||
| 282 | * @throws InvalidDataTypeException |
||
| 283 | * @throws EE_Error |
||
| 284 | * @throws ReflectionException |
||
| 285 | * @throws ReflectionException |
||
| 286 | * @throws ReflectionException |
||
| 287 | */ |
||
| 288 | public function set($field_name, $field_value, $use_default = false) |
||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * Set custom select values for model. |
||
| 375 | * |
||
| 376 | * @param array $custom_select_values |
||
| 377 | */ |
||
| 378 | public function setCustomSelectsValues(array $custom_select_values) |
||
| 382 | |||
| 383 | |||
| 384 | /** |
||
| 385 | * Returns the custom select value for the provided alias if its set. |
||
| 386 | * If not set, returns null. |
||
| 387 | * |
||
| 388 | * @param string $alias |
||
| 389 | * @return string|int|float|null |
||
| 390 | */ |
||
| 391 | public function getCustomSelect($alias) |
||
| 397 | |||
| 398 | |||
| 399 | /** |
||
| 400 | * This sets the field value on the db column if it exists for the given $column_name or |
||
| 401 | * saves it to EE_Extra_Meta if the given $column_name does not match a db column. |
||
| 402 | * |
||
| 403 | * @see EE_message::get_column_value for related documentation on the necessity of this method. |
||
| 404 | * @param string $field_name Must be the exact column name. |
||
| 405 | * @param mixed $field_value The value to set. |
||
| 406 | * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs. |
||
| 407 | * @throws InvalidArgumentException |
||
| 408 | * @throws InvalidInterfaceException |
||
| 409 | * @throws InvalidDataTypeException |
||
| 410 | * @throws EE_Error |
||
| 411 | * @throws ReflectionException |
||
| 412 | */ |
||
| 413 | public function set_field_or_extra_meta($field_name, $field_value) |
||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * This retrieves the value of the db column set on this class or if that's not present |
||
| 427 | * it will attempt to retrieve from extra_meta if found. |
||
| 428 | * Example Usage: |
||
| 429 | * Via EE_Message child class: |
||
| 430 | * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to", |
||
| 431 | * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may |
||
| 432 | * also have additional main fields specific to the messenger. The system accommodates those extra |
||
| 433 | * fields through the EE_Extra_Meta table. This method allows for EE_messengers to retrieve the |
||
| 434 | * value for those extra fields dynamically via the EE_message object. |
||
| 435 | * |
||
| 436 | * @param string $field_name expecting the fully qualified field name. |
||
| 437 | * @return mixed|null value for the field if found. null if not found. |
||
| 438 | * @throws ReflectionException |
||
| 439 | * @throws InvalidArgumentException |
||
| 440 | * @throws InvalidInterfaceException |
||
| 441 | * @throws InvalidDataTypeException |
||
| 442 | * @throws EE_Error |
||
| 443 | */ |
||
| 444 | public function get_field_or_extra_meta($field_name) |
||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
||
| 458 | * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
||
| 459 | * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is |
||
| 460 | * available to all child classes that may be using the EE_Datetime_Field for a field data type. |
||
| 461 | * |
||
| 462 | * @access public |
||
| 463 | * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php |
||
| 464 | * @return void |
||
| 465 | * @throws InvalidArgumentException |
||
| 466 | * @throws InvalidInterfaceException |
||
| 467 | * @throws InvalidDataTypeException |
||
| 468 | * @throws EE_Error |
||
| 469 | * @throws ReflectionException |
||
| 470 | */ |
||
| 471 | public function set_timezone($timezone = '') |
||
| 487 | |||
| 488 | |||
| 489 | /** |
||
| 490 | * This just returns whatever is set for the current timezone. |
||
| 491 | * |
||
| 492 | * @access public |
||
| 493 | * @return string timezone string |
||
| 494 | */ |
||
| 495 | public function get_timezone() |
||
| 499 | |||
| 500 | |||
| 501 | /** |
||
| 502 | * This sets the internal date format to what is sent in to be used as the new default for the class |
||
| 503 | * internally instead of wp set date format options |
||
| 504 | * |
||
| 505 | * @since 4.6 |
||
| 506 | * @param string $format should be a format recognizable by PHP date() functions. |
||
| 507 | */ |
||
| 508 | public function set_date_format($format) |
||
| 514 | |||
| 515 | |||
| 516 | /** |
||
| 517 | * This sets the internal time format string to what is sent in to be used as the new default for the |
||
| 518 | * class internally instead of wp set time format options. |
||
| 519 | * |
||
| 520 | * @since 4.6 |
||
| 521 | * @param string $format should be a format recognizable by PHP date() functions. |
||
| 522 | */ |
||
| 523 | public function set_time_format($format) |
||
| 529 | |||
| 530 | |||
| 531 | /** |
||
| 532 | * This returns the current internal set format for the date and time formats. |
||
| 533 | * |
||
| 534 | * @param bool $full if true (default), then return the full format. Otherwise will return an array |
||
| 535 | * where the first value is the date format and the second value is the time format. |
||
| 536 | * @return mixed string|array |
||
| 537 | */ |
||
| 538 | public function get_format($full = true) |
||
| 542 | |||
| 543 | |||
| 544 | /** |
||
| 545 | * cache |
||
| 546 | * stores the passed model object on the current model object. |
||
| 547 | * In certain circumstances, we can use this cached model object instead of querying for another one entirely. |
||
| 548 | * |
||
| 549 | * @param string $relationName one of the keys in the _model_relations array on the model. Eg |
||
| 550 | * 'Registration' associated with this model object |
||
| 551 | * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction, |
||
| 552 | * that could be a payment or a registration) |
||
| 553 | * @param null $cache_id a string or number that will be used as the key for any Belongs_To_Many |
||
| 554 | * items which will be stored in an array on this object |
||
| 555 | * @throws ReflectionException |
||
| 556 | * @throws InvalidArgumentException |
||
| 557 | * @throws InvalidInterfaceException |
||
| 558 | * @throws InvalidDataTypeException |
||
| 559 | * @throws EE_Error |
||
| 560 | * @return mixed index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one |
||
| 561 | * related thing, no array) |
||
| 562 | */ |
||
| 563 | public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
||
| 620 | |||
| 621 | |||
| 622 | /** |
||
| 623 | * For adding an item to the cached_properties property. |
||
| 624 | * |
||
| 625 | * @access protected |
||
| 626 | * @param string $fieldname the property item the corresponding value is for. |
||
| 627 | * @param mixed $value The value we are caching. |
||
| 628 | * @param string|null $cache_type |
||
| 629 | * @return void |
||
| 630 | * @throws ReflectionException |
||
| 631 | * @throws InvalidArgumentException |
||
| 632 | * @throws InvalidInterfaceException |
||
| 633 | * @throws InvalidDataTypeException |
||
| 634 | * @throws EE_Error |
||
| 635 | */ |
||
| 636 | protected function _set_cached_property($fieldname, $value, $cache_type = null) |
||
| 643 | |||
| 644 | |||
| 645 | /** |
||
| 646 | * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist. |
||
| 647 | * This also SETS the cache if we return the actual property! |
||
| 648 | * |
||
| 649 | * @param string $fieldname the name of the property we're trying to retrieve |
||
| 650 | * @param bool $pretty |
||
| 651 | * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
||
| 652 | * (in cases where the same property may be used for different outputs |
||
| 653 | * - i.e. datetime, money etc.) |
||
| 654 | * It can also accept certain pre-defined "schema" strings |
||
| 655 | * to define how to output the property. |
||
| 656 | * see the field's prepare_for_pretty_echoing for what strings can be used |
||
| 657 | * @return mixed whatever the value for the property is we're retrieving |
||
| 658 | * @throws ReflectionException |
||
| 659 | * @throws InvalidArgumentException |
||
| 660 | * @throws InvalidInterfaceException |
||
| 661 | * @throws InvalidDataTypeException |
||
| 662 | * @throws EE_Error |
||
| 663 | */ |
||
| 664 | protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null) |
||
| 678 | |||
| 679 | |||
| 680 | /** |
||
| 681 | * If the cache didn't fetch the needed item, this fetches it. |
||
| 682 | * |
||
| 683 | * @param string $fieldname |
||
| 684 | * @param bool $pretty |
||
| 685 | * @param string $extra_cache_ref |
||
| 686 | * @return mixed |
||
| 687 | * @throws InvalidArgumentException |
||
| 688 | * @throws InvalidInterfaceException |
||
| 689 | * @throws InvalidDataTypeException |
||
| 690 | * @throws EE_Error |
||
| 691 | * @throws ReflectionException |
||
| 692 | */ |
||
| 693 | protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null) |
||
| 708 | |||
| 709 | |||
| 710 | /** |
||
| 711 | * set timezone, formats, and output for EE_Datetime_Field objects |
||
| 712 | * |
||
| 713 | * @param \EE_Datetime_Field $datetime_field |
||
| 714 | * @param bool $pretty |
||
| 715 | * @param null $date_or_time |
||
| 716 | * @return void |
||
| 717 | * @throws InvalidArgumentException |
||
| 718 | * @throws InvalidInterfaceException |
||
| 719 | * @throws InvalidDataTypeException |
||
| 720 | * @throws EE_Error |
||
| 721 | */ |
||
| 722 | protected function _prepare_datetime_field( |
||
| 742 | |||
| 743 | |||
| 744 | /** |
||
| 745 | * This just takes care of clearing out the cached_properties |
||
| 746 | * |
||
| 747 | * @return void |
||
| 748 | */ |
||
| 749 | protected function _clear_cached_properties() |
||
| 753 | |||
| 754 | |||
| 755 | /** |
||
| 756 | * This just clears out ONE property if it exists in the cache |
||
| 757 | * |
||
| 758 | * @param string $property_name the property to remove if it exists (from the _cached_properties array) |
||
| 759 | * @return void |
||
| 760 | */ |
||
| 761 | protected function _clear_cached_property($property_name) |
||
| 767 | |||
| 768 | |||
| 769 | /** |
||
| 770 | * Ensures that this related thing is a model object. |
||
| 771 | * |
||
| 772 | * @param mixed $object_or_id EE_base_Class/int/string either a related model object, or its ID |
||
| 773 | * @param string $model_name name of the related thing, eg 'Attendee', |
||
| 774 | * @return EE_Base_Class |
||
| 775 | * @throws ReflectionException |
||
| 776 | * @throws InvalidArgumentException |
||
| 777 | * @throws InvalidInterfaceException |
||
| 778 | * @throws InvalidDataTypeException |
||
| 779 | * @throws EE_Error |
||
| 780 | */ |
||
| 781 | protected function ensure_related_thing_is_model_obj($object_or_id, $model_name) |
||
| 789 | |||
| 790 | |||
| 791 | /** |
||
| 792 | * Forgets the cached model of the given relation Name. So the next time we request it, |
||
| 793 | * we will fetch it again from the database. (Handy if you know it's changed somehow). |
||
| 794 | * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM, |
||
| 795 | * then only remove that one object from our cached array. Otherwise, clear the entire list |
||
| 796 | * |
||
| 797 | * @param string $relationName one of the keys in the _model_relations array on the model. |
||
| 798 | * Eg 'Registration' |
||
| 799 | * @param mixed $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL |
||
| 800 | * if you intend to use $clear_all = TRUE, or the relation only |
||
| 801 | * has 1 object anyways (ie, it's a BelongsToRelation) |
||
| 802 | * @param bool $clear_all This flags clearing the entire cache relation property if |
||
| 803 | * this is HasMany or HABTM. |
||
| 804 | * @throws ReflectionException |
||
| 805 | * @throws InvalidArgumentException |
||
| 806 | * @throws InvalidInterfaceException |
||
| 807 | * @throws InvalidDataTypeException |
||
| 808 | * @throws EE_Error |
||
| 809 | * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a |
||
| 810 | * relation from all |
||
| 811 | */ |
||
| 812 | public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false) |
||
| 885 | |||
| 886 | |||
| 887 | /** |
||
| 888 | * update_cache_after_object_save |
||
| 889 | * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has |
||
| 890 | * obtained after being saved to the db |
||
| 891 | * |
||
| 892 | * @param string $relationName - the type of object that is cached |
||
| 893 | * @param EE_Base_Class $newly_saved_object - the newly saved object to be re-cached |
||
| 894 | * @param string $current_cache_id - the ID that was used when originally caching the object |
||
| 895 | * @return boolean TRUE on success, FALSE on fail |
||
| 896 | * @throws ReflectionException |
||
| 897 | * @throws InvalidArgumentException |
||
| 898 | * @throws InvalidInterfaceException |
||
| 899 | * @throws InvalidDataTypeException |
||
| 900 | * @throws EE_Error |
||
| 901 | */ |
||
| 902 | public function update_cache_after_object_save( |
||
| 932 | |||
| 933 | |||
| 934 | /** |
||
| 935 | * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
||
| 936 | * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
||
| 937 | * |
||
| 938 | * @param string $relationName |
||
| 939 | * @return EE_Base_Class |
||
| 940 | */ |
||
| 941 | public function get_one_from_cache($relationName) |
||
| 951 | |||
| 952 | |||
| 953 | /** |
||
| 954 | * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
||
| 955 | * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
||
| 956 | * |
||
| 957 | * @param string $relationName |
||
| 958 | * @throws ReflectionException |
||
| 959 | * @throws InvalidArgumentException |
||
| 960 | * @throws InvalidInterfaceException |
||
| 961 | * @throws InvalidDataTypeException |
||
| 962 | * @throws EE_Error |
||
| 963 | * @return EE_Base_Class[] NOT necessarily indexed by primary keys |
||
| 964 | */ |
||
| 965 | public function get_all_from_cache($relationName) |
||
| 997 | |||
| 998 | |||
| 999 | /** |
||
| 1000 | * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database |
||
| 1001 | * matching the given query conditions. |
||
| 1002 | * |
||
| 1003 | * @param null $field_to_order_by What field is being used as the reference point. |
||
| 1004 | * @param int $limit How many objects to return. |
||
| 1005 | * @param array $query_params Any additional conditions on the query. |
||
| 1006 | * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
||
| 1007 | * you can indicate just the columns you want returned |
||
| 1008 | * @return array|EE_Base_Class[] |
||
| 1009 | * @throws ReflectionException |
||
| 1010 | * @throws InvalidArgumentException |
||
| 1011 | * @throws InvalidInterfaceException |
||
| 1012 | * @throws InvalidDataTypeException |
||
| 1013 | * @throws EE_Error |
||
| 1014 | */ |
||
| 1015 | View Code Duplication | public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null) |
|
| 1027 | |||
| 1028 | |||
| 1029 | /** |
||
| 1030 | * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database |
||
| 1031 | * matching the given query conditions. |
||
| 1032 | * |
||
| 1033 | * @param null $field_to_order_by What field is being used as the reference point. |
||
| 1034 | * @param int $limit How many objects to return. |
||
| 1035 | * @param array $query_params Any additional conditions on the query. |
||
| 1036 | * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
||
| 1037 | * you can indicate just the columns you want returned |
||
| 1038 | * @return array|EE_Base_Class[] |
||
| 1039 | * @throws ReflectionException |
||
| 1040 | * @throws InvalidArgumentException |
||
| 1041 | * @throws InvalidInterfaceException |
||
| 1042 | * @throws InvalidDataTypeException |
||
| 1043 | * @throws EE_Error |
||
| 1044 | */ |
||
| 1045 | View Code Duplication | public function previous_x( |
|
| 1061 | |||
| 1062 | |||
| 1063 | /** |
||
| 1064 | * Returns the next EE_Base_Class object in sequence from this object as found in the database |
||
| 1065 | * matching the given query conditions. |
||
| 1066 | * |
||
| 1067 | * @param null $field_to_order_by What field is being used as the reference point. |
||
| 1068 | * @param array $query_params Any additional conditions on the query. |
||
| 1069 | * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
||
| 1070 | * you can indicate just the columns you want returned |
||
| 1071 | * @return array|EE_Base_Class |
||
| 1072 | * @throws ReflectionException |
||
| 1073 | * @throws InvalidArgumentException |
||
| 1074 | * @throws InvalidInterfaceException |
||
| 1075 | * @throws InvalidDataTypeException |
||
| 1076 | * @throws EE_Error |
||
| 1077 | */ |
||
| 1078 | View Code Duplication | public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1090 | |||
| 1091 | |||
| 1092 | /** |
||
| 1093 | * Returns the previous EE_Base_Class object in sequence from this object as found in the database |
||
| 1094 | * matching the given query conditions. |
||
| 1095 | * |
||
| 1096 | * @param null $field_to_order_by What field is being used as the reference point. |
||
| 1097 | * @param array $query_params Any additional conditions on the query. |
||
| 1098 | * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
||
| 1099 | * you can indicate just the column you want returned |
||
| 1100 | * @return array|EE_Base_Class |
||
| 1101 | * @throws ReflectionException |
||
| 1102 | * @throws InvalidArgumentException |
||
| 1103 | * @throws InvalidInterfaceException |
||
| 1104 | * @throws InvalidDataTypeException |
||
| 1105 | * @throws EE_Error |
||
| 1106 | */ |
||
| 1107 | View Code Duplication | public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1119 | |||
| 1120 | |||
| 1121 | /** |
||
| 1122 | * Overrides parent because parent expects old models. |
||
| 1123 | * This also doesn't do any validation, and won't work for serialized arrays |
||
| 1124 | * |
||
| 1125 | * @param string $field_name |
||
| 1126 | * @param mixed $field_value_from_db |
||
| 1127 | * @throws ReflectionException |
||
| 1128 | * @throws InvalidArgumentException |
||
| 1129 | * @throws InvalidInterfaceException |
||
| 1130 | * @throws InvalidDataTypeException |
||
| 1131 | * @throws EE_Error |
||
| 1132 | */ |
||
| 1133 | public function set_from_db($field_name, $field_value_from_db) |
||
| 1155 | |||
| 1156 | |||
| 1157 | /** |
||
| 1158 | * verifies that the specified field is of the correct type |
||
| 1159 | * |
||
| 1160 | * @param string $field_name |
||
| 1161 | * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
||
| 1162 | * (in cases where the same property may be used for different outputs |
||
| 1163 | * - i.e. datetime, money etc.) |
||
| 1164 | * @return mixed |
||
| 1165 | * @throws ReflectionException |
||
| 1166 | * @throws InvalidArgumentException |
||
| 1167 | * @throws InvalidInterfaceException |
||
| 1168 | * @throws InvalidDataTypeException |
||
| 1169 | * @throws EE_Error |
||
| 1170 | */ |
||
| 1171 | public function get($field_name, $extra_cache_ref = null) |
||
| 1175 | |||
| 1176 | |||
| 1177 | /** |
||
| 1178 | * This method simply returns the RAW unprocessed value for the given property in this class |
||
| 1179 | * |
||
| 1180 | * @param string $field_name A valid fieldname |
||
| 1181 | * @return mixed Whatever the raw value stored on the property is. |
||
| 1182 | * @throws ReflectionException |
||
| 1183 | * @throws InvalidArgumentException |
||
| 1184 | * @throws InvalidInterfaceException |
||
| 1185 | * @throws InvalidDataTypeException |
||
| 1186 | * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
||
| 1187 | */ |
||
| 1188 | public function get_raw($field_name) |
||
| 1195 | |||
| 1196 | |||
| 1197 | /** |
||
| 1198 | * This is used to return the internal DateTime object used for a field that is a |
||
| 1199 | * EE_Datetime_Field. |
||
| 1200 | * |
||
| 1201 | * @param string $field_name The field name retrieving the DateTime object. |
||
| 1202 | * @return mixed null | false | DateTime If the requested field is NOT a EE_Datetime_Field then |
||
| 1203 | * @throws ReflectionException |
||
| 1204 | * @throws InvalidArgumentException |
||
| 1205 | * @throws InvalidInterfaceException |
||
| 1206 | * @throws InvalidDataTypeException |
||
| 1207 | * @throws EE_Error |
||
| 1208 | * an error is set and false returned. If the field IS an |
||
| 1209 | * EE_Datetime_Field and but the field value is null, then |
||
| 1210 | * just null is returned (because that indicates that likely |
||
| 1211 | * this field is nullable). |
||
| 1212 | */ |
||
| 1213 | public function get_DateTime_object($field_name) |
||
| 1233 | |||
| 1234 | |||
| 1235 | /** |
||
| 1236 | * To be used in template to immediately echo out the value, and format it for output. |
||
| 1237 | * Eg, should call stripslashes and whatnot before echoing |
||
| 1238 | * |
||
| 1239 | * @param string $field_name the name of the field as it appears in the DB |
||
| 1240 | * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
||
| 1241 | * (in cases where the same property may be used for different outputs |
||
| 1242 | * - i.e. datetime, money etc.) |
||
| 1243 | * @return void |
||
| 1244 | * @throws ReflectionException |
||
| 1245 | * @throws InvalidArgumentException |
||
| 1246 | * @throws InvalidInterfaceException |
||
| 1247 | * @throws InvalidDataTypeException |
||
| 1248 | * @throws EE_Error |
||
| 1249 | */ |
||
| 1250 | public function e($field_name, $extra_cache_ref = null) |
||
| 1254 | |||
| 1255 | |||
| 1256 | /** |
||
| 1257 | * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it |
||
| 1258 | * can be easily used as the value of form input. |
||
| 1259 | * |
||
| 1260 | * @param string $field_name |
||
| 1261 | * @return void |
||
| 1262 | * @throws ReflectionException |
||
| 1263 | * @throws InvalidArgumentException |
||
| 1264 | * @throws InvalidInterfaceException |
||
| 1265 | * @throws InvalidDataTypeException |
||
| 1266 | * @throws EE_Error |
||
| 1267 | */ |
||
| 1268 | public function f($field_name) |
||
| 1272 | |||
| 1273 | |||
| 1274 | /** |
||
| 1275 | * Same as `f()` but just returns the value instead of echoing it |
||
| 1276 | * |
||
| 1277 | * @param string $field_name |
||
| 1278 | * @return string |
||
| 1279 | * @throws ReflectionException |
||
| 1280 | * @throws InvalidArgumentException |
||
| 1281 | * @throws InvalidInterfaceException |
||
| 1282 | * @throws InvalidDataTypeException |
||
| 1283 | * @throws EE_Error |
||
| 1284 | */ |
||
| 1285 | public function get_f($field_name) |
||
| 1289 | |||
| 1290 | |||
| 1291 | /** |
||
| 1292 | * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this. |
||
| 1293 | * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class |
||
| 1294 | * to see what options are available. |
||
| 1295 | * |
||
| 1296 | * @param string $field_name |
||
| 1297 | * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
||
| 1298 | * (in cases where the same property may be used for different outputs |
||
| 1299 | * - i.e. datetime, money etc.) |
||
| 1300 | * @return mixed |
||
| 1301 | * @throws ReflectionException |
||
| 1302 | * @throws InvalidArgumentException |
||
| 1303 | * @throws InvalidInterfaceException |
||
| 1304 | * @throws InvalidDataTypeException |
||
| 1305 | * @throws EE_Error |
||
| 1306 | */ |
||
| 1307 | public function get_pretty($field_name, $extra_cache_ref = null) |
||
| 1311 | |||
| 1312 | |||
| 1313 | /** |
||
| 1314 | * This simply returns the datetime for the given field name |
||
| 1315 | * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions |
||
| 1316 | * (and the equivalent e_date, e_time, e_datetime). |
||
| 1317 | * |
||
| 1318 | * @access protected |
||
| 1319 | * @param string $field_name Field on the instantiated EE_Base_Class child object |
||
| 1320 | * @param string $dt_frmt valid datetime format used for date |
||
| 1321 | * (if '' then we just use the default on the field, |
||
| 1322 | * if NULL we use the last-used format) |
||
| 1323 | * @param string $tm_frmt Same as above except this is for time format |
||
| 1324 | * @param string $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time. |
||
| 1325 | * @param boolean $echo Whether the dtt is echoing using pretty echoing or just returned using vanilla get |
||
| 1326 | * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown |
||
| 1327 | * if field is not a valid dtt field, or void if echoing |
||
| 1328 | * @throws ReflectionException |
||
| 1329 | * @throws InvalidArgumentException |
||
| 1330 | * @throws InvalidInterfaceException |
||
| 1331 | * @throws InvalidDataTypeException |
||
| 1332 | * @throws EE_Error |
||
| 1333 | */ |
||
| 1334 | protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false) |
||
| 1347 | |||
| 1348 | |||
| 1349 | /** |
||
| 1350 | * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date |
||
| 1351 | * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
||
| 1352 | * other echoes the pretty value for dtt) |
||
| 1353 | * |
||
| 1354 | * @param string $field_name name of model object datetime field holding the value |
||
| 1355 | * @param string $format format for the date returned (if NULL we use default in dt_frmt property) |
||
| 1356 | * @return string datetime value formatted |
||
| 1357 | * @throws ReflectionException |
||
| 1358 | * @throws InvalidArgumentException |
||
| 1359 | * @throws InvalidInterfaceException |
||
| 1360 | * @throws InvalidDataTypeException |
||
| 1361 | * @throws EE_Error |
||
| 1362 | */ |
||
| 1363 | public function get_date($field_name, $format = '') |
||
| 1367 | |||
| 1368 | |||
| 1369 | /** |
||
| 1370 | * @param $field_name |
||
| 1371 | * @param string $format |
||
| 1372 | * @throws ReflectionException |
||
| 1373 | * @throws InvalidArgumentException |
||
| 1374 | * @throws InvalidInterfaceException |
||
| 1375 | * @throws InvalidDataTypeException |
||
| 1376 | * @throws EE_Error |
||
| 1377 | */ |
||
| 1378 | public function e_date($field_name, $format = '') |
||
| 1382 | |||
| 1383 | |||
| 1384 | /** |
||
| 1385 | * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time |
||
| 1386 | * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
||
| 1387 | * other echoes the pretty value for dtt) |
||
| 1388 | * |
||
| 1389 | * @param string $field_name name of model object datetime field holding the value |
||
| 1390 | * @param string $format format for the time returned ( if NULL we use default in tm_frmt property) |
||
| 1391 | * @return string datetime value formatted |
||
| 1392 | * @throws ReflectionException |
||
| 1393 | * @throws InvalidArgumentException |
||
| 1394 | * @throws InvalidInterfaceException |
||
| 1395 | * @throws InvalidDataTypeException |
||
| 1396 | * @throws EE_Error |
||
| 1397 | */ |
||
| 1398 | public function get_time($field_name, $format = '') |
||
| 1402 | |||
| 1403 | |||
| 1404 | /** |
||
| 1405 | * @param $field_name |
||
| 1406 | * @param string $format |
||
| 1407 | * @throws ReflectionException |
||
| 1408 | * @throws InvalidArgumentException |
||
| 1409 | * @throws InvalidInterfaceException |
||
| 1410 | * @throws InvalidDataTypeException |
||
| 1411 | * @throws EE_Error |
||
| 1412 | */ |
||
| 1413 | public function e_time($field_name, $format = '') |
||
| 1417 | |||
| 1418 | |||
| 1419 | /** |
||
| 1420 | * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND |
||
| 1421 | * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
||
| 1422 | * other echoes the pretty value for dtt) |
||
| 1423 | * |
||
| 1424 | * @param string $field_name name of model object datetime field holding the value |
||
| 1425 | * @param string $dt_frmt format for the date returned (if NULL we use default in dt_frmt property) |
||
| 1426 | * @param string $tm_frmt format for the time returned (if NULL we use default in tm_frmt property) |
||
| 1427 | * @return string datetime value formatted |
||
| 1428 | * @throws ReflectionException |
||
| 1429 | * @throws InvalidArgumentException |
||
| 1430 | * @throws InvalidInterfaceException |
||
| 1431 | * @throws InvalidDataTypeException |
||
| 1432 | * @throws EE_Error |
||
| 1433 | */ |
||
| 1434 | public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
||
| 1438 | |||
| 1439 | |||
| 1440 | /** |
||
| 1441 | * @param string $field_name |
||
| 1442 | * @param string $dt_frmt |
||
| 1443 | * @param string $tm_frmt |
||
| 1444 | * @throws ReflectionException |
||
| 1445 | * @throws InvalidArgumentException |
||
| 1446 | * @throws InvalidInterfaceException |
||
| 1447 | * @throws InvalidDataTypeException |
||
| 1448 | * @throws EE_Error |
||
| 1449 | */ |
||
| 1450 | public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
||
| 1454 | |||
| 1455 | |||
| 1456 | /** |
||
| 1457 | * Get the i8ln value for a date using the WordPress @see date_i18n function. |
||
| 1458 | * |
||
| 1459 | * @param string $field_name The EE_Datetime_Field reference for the date being retrieved. |
||
| 1460 | * @param string $format PHP valid date/time string format. If none is provided then the internal set format |
||
| 1461 | * on the object will be used. |
||
| 1462 | * @return string Date and time string in set locale or false if no field exists for the given |
||
| 1463 | * @throws ReflectionException |
||
| 1464 | * @throws InvalidArgumentException |
||
| 1465 | * @throws InvalidInterfaceException |
||
| 1466 | * @throws InvalidDataTypeException |
||
| 1467 | * @throws EE_Error |
||
| 1468 | * field name. |
||
| 1469 | */ |
||
| 1470 | public function get_i18n_datetime($field_name, $format = '') |
||
| 1481 | |||
| 1482 | |||
| 1483 | /** |
||
| 1484 | * This method validates whether the given field name is a valid field on the model object as well as it is of a |
||
| 1485 | * type EE_Datetime_Field. On success there will be returned the field settings. On fail an EE_Error exception is |
||
| 1486 | * thrown. |
||
| 1487 | * |
||
| 1488 | * @param string $field_name The field name being checked |
||
| 1489 | * @throws ReflectionException |
||
| 1490 | * @throws InvalidArgumentException |
||
| 1491 | * @throws InvalidInterfaceException |
||
| 1492 | * @throws InvalidDataTypeException |
||
| 1493 | * @throws EE_Error |
||
| 1494 | * @return EE_Datetime_Field |
||
| 1495 | */ |
||
| 1496 | protected function _get_dtt_field_settings($field_name) |
||
| 1514 | |||
| 1515 | |||
| 1516 | |||
| 1517 | |||
| 1518 | /** |
||
| 1519 | * NOTE ABOUT BELOW: |
||
| 1520 | * These convenience date and time setters are for setting date and time independently. In other words you might |
||
| 1521 | * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand |
||
| 1522 | * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value) |
||
| 1523 | * method and make sure you send the entire datetime value for setting. |
||
| 1524 | */ |
||
| 1525 | /** |
||
| 1526 | * sets the time on a datetime property |
||
| 1527 | * |
||
| 1528 | * @access protected |
||
| 1529 | * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
||
| 1530 | * @param string $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field) |
||
| 1531 | * @throws ReflectionException |
||
| 1532 | * @throws InvalidArgumentException |
||
| 1533 | * @throws InvalidInterfaceException |
||
| 1534 | * @throws InvalidDataTypeException |
||
| 1535 | * @throws EE_Error |
||
| 1536 | */ |
||
| 1537 | protected function _set_time_for($time, $fieldname) |
||
| 1541 | |||
| 1542 | |||
| 1543 | /** |
||
| 1544 | * sets the date on a datetime property |
||
| 1545 | * |
||
| 1546 | * @access protected |
||
| 1547 | * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
||
| 1548 | * @param string $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field) |
||
| 1549 | * @throws ReflectionException |
||
| 1550 | * @throws InvalidArgumentException |
||
| 1551 | * @throws InvalidInterfaceException |
||
| 1552 | * @throws InvalidDataTypeException |
||
| 1553 | * @throws EE_Error |
||
| 1554 | */ |
||
| 1555 | protected function _set_date_for($date, $fieldname) |
||
| 1559 | |||
| 1560 | |||
| 1561 | /** |
||
| 1562 | * This takes care of setting a date or time independently on a given model object property. This method also |
||
| 1563 | * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field |
||
| 1564 | * |
||
| 1565 | * @access protected |
||
| 1566 | * @param string $what "T" for time, 'B' for both, 'D' for Date. |
||
| 1567 | * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object) |
||
| 1568 | * @param string $fieldname the name of the field the date OR time is being set on (must match a |
||
| 1569 | * EE_Datetime_Field property) |
||
| 1570 | * @throws ReflectionException |
||
| 1571 | * @throws InvalidArgumentException |
||
| 1572 | * @throws InvalidInterfaceException |
||
| 1573 | * @throws InvalidDataTypeException |
||
| 1574 | * @throws EE_Error |
||
| 1575 | */ |
||
| 1576 | protected function _set_date_time($what = 'T', $datetime_value, $fieldname) |
||
| 1601 | |||
| 1602 | |||
| 1603 | /** |
||
| 1604 | * This will return a timestamp for the website timezone but ONLY when the current website timezone is different |
||
| 1605 | * than the timezone set for the website. NOTE, this currently only works well with methods that return values. If |
||
| 1606 | * you use it with methods that echo values the $_timestamp property may not get reset to its original value and |
||
| 1607 | * that could lead to some unexpected results! |
||
| 1608 | * |
||
| 1609 | * @access public |
||
| 1610 | * @param string $field_name This is the name of the field on the object that contains the date/time |
||
| 1611 | * value being returned. |
||
| 1612 | * @param string $callback must match a valid method in this class (defaults to get_datetime) |
||
| 1613 | * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
||
| 1614 | * @param string $prepend You can include something to prepend on the timestamp |
||
| 1615 | * @param string $append You can include something to append on the timestamp |
||
| 1616 | * @throws ReflectionException |
||
| 1617 | * @throws InvalidArgumentException |
||
| 1618 | * @throws InvalidInterfaceException |
||
| 1619 | * @throws InvalidDataTypeException |
||
| 1620 | * @throws EE_Error |
||
| 1621 | * @return string timestamp |
||
| 1622 | */ |
||
| 1623 | public function display_in_my_timezone( |
||
| 1654 | |||
| 1655 | |||
| 1656 | /** |
||
| 1657 | * Deletes this model object. |
||
| 1658 | * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
||
| 1659 | * override |
||
| 1660 | * `EE_Base_Class::_delete` NOT this class. |
||
| 1661 | * |
||
| 1662 | * @return boolean | int |
||
| 1663 | * @throws ReflectionException |
||
| 1664 | * @throws InvalidArgumentException |
||
| 1665 | * @throws InvalidInterfaceException |
||
| 1666 | * @throws InvalidDataTypeException |
||
| 1667 | * @throws EE_Error |
||
| 1668 | */ |
||
| 1669 | public function delete() |
||
| 1697 | |||
| 1698 | |||
| 1699 | /** |
||
| 1700 | * Calls the specific delete method for the instantiated class. |
||
| 1701 | * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
||
| 1702 | * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
||
| 1703 | * `EE_Base_Class::delete` |
||
| 1704 | * |
||
| 1705 | * @return bool|int |
||
| 1706 | * @throws ReflectionException |
||
| 1707 | * @throws InvalidArgumentException |
||
| 1708 | * @throws InvalidInterfaceException |
||
| 1709 | * @throws InvalidDataTypeException |
||
| 1710 | * @throws EE_Error |
||
| 1711 | */ |
||
| 1712 | protected function _delete() |
||
| 1716 | |||
| 1717 | |||
| 1718 | /** |
||
| 1719 | * Deletes this model object permanently from db |
||
| 1720 | * (but keep in mind related models may block the delete and return an error) |
||
| 1721 | * |
||
| 1722 | * @return bool | int |
||
| 1723 | * @throws ReflectionException |
||
| 1724 | * @throws InvalidArgumentException |
||
| 1725 | * @throws InvalidInterfaceException |
||
| 1726 | * @throws InvalidDataTypeException |
||
| 1727 | * @throws EE_Error |
||
| 1728 | */ |
||
| 1729 | public function delete_permanently() |
||
| 1749 | |||
| 1750 | |||
| 1751 | /** |
||
| 1752 | * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
||
| 1753 | * related model objects |
||
| 1754 | * |
||
| 1755 | * @throws ReflectionException |
||
| 1756 | * @throws InvalidArgumentException |
||
| 1757 | * @throws InvalidInterfaceException |
||
| 1758 | * @throws InvalidDataTypeException |
||
| 1759 | * @throws EE_Error |
||
| 1760 | */ |
||
| 1761 | public function refresh_cache_of_related_objects() |
||
| 1784 | |||
| 1785 | |||
| 1786 | /** |
||
| 1787 | * Saves this object to the database. An array may be supplied to set some values on this |
||
| 1788 | * object just before saving. |
||
| 1789 | * |
||
| 1790 | * @access public |
||
| 1791 | * @param array $set_cols_n_values keys are field names, values are their new values, |
||
| 1792 | * if provided during the save() method (often client code will change the fields' |
||
| 1793 | * values before calling save) |
||
| 1794 | * @throws InvalidArgumentException |
||
| 1795 | * @throws InvalidInterfaceException |
||
| 1796 | * @throws InvalidDataTypeException |
||
| 1797 | * @throws EE_Error |
||
| 1798 | * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object |
||
| 1799 | * isn't allowed to persist (as determined by EE_Base_Class::allow_persist()) |
||
| 1800 | * @throws ReflectionException |
||
| 1801 | * @throws ReflectionException |
||
| 1802 | * @throws ReflectionException |
||
| 1803 | */ |
||
| 1804 | public function save($set_cols_n_values = array()) |
||
| 1924 | |||
| 1925 | |||
| 1926 | /** |
||
| 1927 | * Updates the foreign key on related models objects pointing to this to have this model object's ID |
||
| 1928 | * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
||
| 1929 | * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
||
| 1930 | * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
||
| 1931 | * 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 |
||
| 1932 | * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
||
| 1933 | * or not they exist in the DB (if they do, their DB records will be automatically updated) |
||
| 1934 | * |
||
| 1935 | * @return void |
||
| 1936 | * @throws ReflectionException |
||
| 1937 | * @throws InvalidArgumentException |
||
| 1938 | * @throws InvalidInterfaceException |
||
| 1939 | * @throws InvalidDataTypeException |
||
| 1940 | * @throws EE_Error |
||
| 1941 | */ |
||
| 1942 | protected function _update_cached_related_model_objs_fks() |
||
| 1959 | |||
| 1960 | |||
| 1961 | /** |
||
| 1962 | * Saves this model object and its NEW cached relations to the database. |
||
| 1963 | * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
||
| 1964 | * In order for that to work, we would need to mark model objects as dirty/clean... |
||
| 1965 | * because otherwise, there's a potential for infinite looping of saving |
||
| 1966 | * Saves the cached related model objects, and ensures the relation between them |
||
| 1967 | * and this object and properly setup |
||
| 1968 | * |
||
| 1969 | * @return int ID of new model object on save; 0 on failure+ |
||
| 1970 | * @throws ReflectionException |
||
| 1971 | * @throws InvalidArgumentException |
||
| 1972 | * @throws InvalidInterfaceException |
||
| 1973 | * @throws InvalidDataTypeException |
||
| 1974 | * @throws EE_Error |
||
| 1975 | */ |
||
| 1976 | public function save_new_cached_related_model_objs() |
||
| 2012 | |||
| 2013 | |||
| 2014 | /** |
||
| 2015 | * for getting a model while instantiated. |
||
| 2016 | * |
||
| 2017 | * @return EEM_Base | EEM_CPT_Base |
||
| 2018 | * @throws ReflectionException |
||
| 2019 | * @throws InvalidArgumentException |
||
| 2020 | * @throws InvalidInterfaceException |
||
| 2021 | * @throws InvalidDataTypeException |
||
| 2022 | * @throws EE_Error |
||
| 2023 | */ |
||
| 2024 | public function get_model() |
||
| 2034 | |||
| 2035 | |||
| 2036 | /** |
||
| 2037 | * @param $props_n_values |
||
| 2038 | * @param $classname |
||
| 2039 | * @return mixed bool|EE_Base_Class|EEM_CPT_Base |
||
| 2040 | * @throws ReflectionException |
||
| 2041 | * @throws InvalidArgumentException |
||
| 2042 | * @throws InvalidInterfaceException |
||
| 2043 | * @throws InvalidDataTypeException |
||
| 2044 | * @throws EE_Error |
||
| 2045 | */ |
||
| 2046 | protected static function _get_object_from_entity_mapper($props_n_values, $classname) |
||
| 2059 | |||
| 2060 | |||
| 2061 | /** |
||
| 2062 | * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
||
| 2063 | * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
||
| 2064 | * 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 |
||
| 2065 | * we return false. |
||
| 2066 | * |
||
| 2067 | * @param array $props_n_values incoming array of properties and their values |
||
| 2068 | * @param string $classname the classname of the child class |
||
| 2069 | * @param null $timezone |
||
| 2070 | * @param array $date_formats incoming date_formats in an array where the first value is the |
||
| 2071 | * date_format and the second value is the time format |
||
| 2072 | * @return mixed (EE_Base_Class|bool) |
||
| 2073 | * @throws InvalidArgumentException |
||
| 2074 | * @throws InvalidInterfaceException |
||
| 2075 | * @throws InvalidDataTypeException |
||
| 2076 | * @throws EE_Error |
||
| 2077 | * @throws ReflectionException |
||
| 2078 | * @throws ReflectionException |
||
| 2079 | * @throws ReflectionException |
||
| 2080 | */ |
||
| 2081 | protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array()) |
||
| 2118 | |||
| 2119 | |||
| 2120 | /** |
||
| 2121 | * Gets the EEM_*_Model for this class |
||
| 2122 | * |
||
| 2123 | * @access public now, as this is more convenient |
||
| 2124 | * @param $classname |
||
| 2125 | * @param null $timezone |
||
| 2126 | * @throws ReflectionException |
||
| 2127 | * @throws InvalidArgumentException |
||
| 2128 | * @throws InvalidInterfaceException |
||
| 2129 | * @throws InvalidDataTypeException |
||
| 2130 | * @throws EE_Error |
||
| 2131 | * @return EEM_Base |
||
| 2132 | */ |
||
| 2133 | protected static function _get_model($classname, $timezone = null) |
||
| 2150 | |||
| 2151 | |||
| 2152 | /** |
||
| 2153 | * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
||
| 2154 | * |
||
| 2155 | * @param string $model_classname |
||
| 2156 | * @param null $timezone |
||
| 2157 | * @return EEM_Base |
||
| 2158 | * @throws ReflectionException |
||
| 2159 | * @throws InvalidArgumentException |
||
| 2160 | * @throws InvalidInterfaceException |
||
| 2161 | * @throws InvalidDataTypeException |
||
| 2162 | * @throws EE_Error |
||
| 2163 | */ |
||
| 2164 | protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
||
| 2171 | |||
| 2172 | |||
| 2173 | /** |
||
| 2174 | * If a model name is provided (eg Registration), gets the model classname for that model. |
||
| 2175 | * Also works if a model class's classname is provided (eg EE_Registration). |
||
| 2176 | * |
||
| 2177 | * @param null $model_name |
||
| 2178 | * @return string like EEM_Attendee |
||
| 2179 | */ |
||
| 2180 | private static function _get_model_classname($model_name = null) |
||
| 2189 | |||
| 2190 | |||
| 2191 | /** |
||
| 2192 | * returns the name of the primary key attribute |
||
| 2193 | * |
||
| 2194 | * @param null $classname |
||
| 2195 | * @throws ReflectionException |
||
| 2196 | * @throws InvalidArgumentException |
||
| 2197 | * @throws InvalidInterfaceException |
||
| 2198 | * @throws InvalidDataTypeException |
||
| 2199 | * @throws EE_Error |
||
| 2200 | * @return string |
||
| 2201 | */ |
||
| 2202 | protected static function _get_primary_key_name($classname = null) |
||
| 2214 | |||
| 2215 | |||
| 2216 | /** |
||
| 2217 | * Gets the value of the primary key. |
||
| 2218 | * If the object hasn't yet been saved, it should be whatever the model field's default was |
||
| 2219 | * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
||
| 2220 | * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
||
| 2221 | * |
||
| 2222 | * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
||
| 2223 | * @throws ReflectionException |
||
| 2224 | * @throws InvalidArgumentException |
||
| 2225 | * @throws InvalidInterfaceException |
||
| 2226 | * @throws InvalidDataTypeException |
||
| 2227 | * @throws EE_Error |
||
| 2228 | */ |
||
| 2229 | public function ID() |
||
| 2238 | |||
| 2239 | |||
| 2240 | /** |
||
| 2241 | * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
||
| 2242 | * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
||
| 2243 | * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
||
| 2244 | * |
||
| 2245 | * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
||
| 2246 | * @param string $relationName eg 'Events','Question',etc. |
||
| 2247 | * an attendee to a group, you also want to specify which role they |
||
| 2248 | * will have in that group. So you would use this parameter to |
||
| 2249 | * specify array('role-column-name'=>'role-id') |
||
| 2250 | * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
||
| 2251 | * allow you to further constrict the relation to being added. |
||
| 2252 | * However, keep in mind that the columns (keys) given must match a |
||
| 2253 | * column on the JOIN table and currently only the HABTM models |
||
| 2254 | * accept these additional conditions. Also remember that if an |
||
| 2255 | * exact match isn't found for these extra cols/val pairs, then a |
||
| 2256 | * NEW row is created in the join table. |
||
| 2257 | * @param null $cache_id |
||
| 2258 | * @throws ReflectionException |
||
| 2259 | * @throws InvalidArgumentException |
||
| 2260 | * @throws InvalidInterfaceException |
||
| 2261 | * @throws InvalidDataTypeException |
||
| 2262 | * @throws EE_Error |
||
| 2263 | * @return EE_Base_Class the object the relation was added to |
||
| 2264 | */ |
||
| 2265 | public function _add_relation_to( |
||
| 2319 | |||
| 2320 | |||
| 2321 | /** |
||
| 2322 | * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
||
| 2323 | * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
||
| 2324 | * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
||
| 2325 | * from the cache |
||
| 2326 | * |
||
| 2327 | * @param mixed $otherObjectModelObjectOrID |
||
| 2328 | * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
||
| 2329 | * to the DB yet |
||
| 2330 | * @param string $relationName |
||
| 2331 | * @param array $where_query |
||
| 2332 | * You can optionally include an array of key=>value pairs that allow you to further constrict the |
||
| 2333 | * relation to being added. However, keep in mind that the columns (keys) given must match a column |
||
| 2334 | * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
||
| 2335 | * remember that if an exact match isn't found for these extra cols/val pairs, then a NEW row is |
||
| 2336 | * created in the join table. |
||
| 2337 | * @return EE_Base_Class the relation was removed from |
||
| 2338 | * @throws ReflectionException |
||
| 2339 | * @throws InvalidArgumentException |
||
| 2340 | * @throws InvalidInterfaceException |
||
| 2341 | * @throws InvalidDataTypeException |
||
| 2342 | * @throws EE_Error |
||
| 2343 | */ |
||
| 2344 | public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array()) |
||
| 2373 | |||
| 2374 | |||
| 2375 | /** |
||
| 2376 | * Removes ALL the related things for the $relationName. |
||
| 2377 | * |
||
| 2378 | * @param string $relationName |
||
| 2379 | * @param array $where_query_params like EEM_Base::get_all's $query_params[0] (where conditions) |
||
| 2380 | * @return EE_Base_Class |
||
| 2381 | * @throws ReflectionException |
||
| 2382 | * @throws InvalidArgumentException |
||
| 2383 | * @throws InvalidInterfaceException |
||
| 2384 | * @throws InvalidDataTypeException |
||
| 2385 | * @throws EE_Error |
||
| 2386 | */ |
||
| 2387 | public function _remove_relations($relationName, $where_query_params = array()) |
||
| 2419 | |||
| 2420 | |||
| 2421 | /** |
||
| 2422 | * Gets all the related model objects of the specified type. Eg, if the current class if |
||
| 2423 | * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
||
| 2424 | * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
||
| 2425 | * because we want to get even deleted items etc. |
||
| 2426 | * |
||
| 2427 | * @param string $relationName key in the model's _model_relations array |
||
| 2428 | * @param array $query_params like EEM_Base::get_all |
||
| 2429 | * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary |
||
| 2430 | * keys or might not be saved yet. Consider using EEM_Base::get_IDs() on these |
||
| 2431 | * results if you want IDs |
||
| 2432 | * @throws ReflectionException |
||
| 2433 | * @throws InvalidArgumentException |
||
| 2434 | * @throws InvalidInterfaceException |
||
| 2435 | * @throws InvalidDataTypeException |
||
| 2436 | * @throws EE_Error |
||
| 2437 | */ |
||
| 2438 | public function get_many_related($relationName, $query_params = array()) |
||
| 2473 | |||
| 2474 | |||
| 2475 | /** |
||
| 2476 | * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
||
| 2477 | * unless otherwise specified in the $query_params |
||
| 2478 | * |
||
| 2479 | * @param string $relation_name model_name like 'Event', or 'Registration' |
||
| 2480 | * @param array $query_params like EEM_Base::get_all's |
||
| 2481 | * @param string $field_to_count name of field to count by. By default, uses primary key |
||
| 2482 | * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
||
| 2483 | * that by the setting $distinct to TRUE; |
||
| 2484 | * @return int |
||
| 2485 | * @throws ReflectionException |
||
| 2486 | * @throws InvalidArgumentException |
||
| 2487 | * @throws InvalidInterfaceException |
||
| 2488 | * @throws InvalidDataTypeException |
||
| 2489 | * @throws EE_Error |
||
| 2490 | */ |
||
| 2491 | public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false) |
||
| 2501 | |||
| 2502 | |||
| 2503 | /** |
||
| 2504 | * Instead of getting the related model objects, simply sums up the values of the specified field. |
||
| 2505 | * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
||
| 2506 | * |
||
| 2507 | * @param string $relation_name model_name like 'Event', or 'Registration' |
||
| 2508 | * @param array $query_params like EEM_Base::get_all's |
||
| 2509 | * @param string $field_to_sum name of field to count by. |
||
| 2510 | * By default, uses primary key |
||
| 2511 | * (which doesn't make much sense, so you should probably change it) |
||
| 2512 | * @return int |
||
| 2513 | * @throws ReflectionException |
||
| 2514 | * @throws InvalidArgumentException |
||
| 2515 | * @throws InvalidInterfaceException |
||
| 2516 | * @throws InvalidDataTypeException |
||
| 2517 | * @throws EE_Error |
||
| 2518 | */ |
||
| 2519 | public function sum_related($relation_name, $query_params = array(), $field_to_sum = null) |
||
| 2528 | |||
| 2529 | |||
| 2530 | /** |
||
| 2531 | * Gets the first (ie, one) related model object of the specified type. |
||
| 2532 | * |
||
| 2533 | * @param string $relationName key in the model's _model_relations array |
||
| 2534 | * @param array $query_params like EEM_Base::get_all |
||
| 2535 | * @return EE_Base_Class (not an array, a single object) |
||
| 2536 | * @throws ReflectionException |
||
| 2537 | * @throws InvalidArgumentException |
||
| 2538 | * @throws InvalidInterfaceException |
||
| 2539 | * @throws InvalidDataTypeException |
||
| 2540 | * @throws EE_Error |
||
| 2541 | */ |
||
| 2542 | public function get_first_related($relationName, $query_params = array()) |
||
| 2592 | |||
| 2593 | |||
| 2594 | /** |
||
| 2595 | * Does a delete on all related objects of type $relationName and removes |
||
| 2596 | * the current model object's relation to them. If they can't be deleted (because |
||
| 2597 | * of blocking related model objects) does nothing. If the related model objects are |
||
| 2598 | * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
||
| 2599 | * If this model object doesn't exist yet in the DB, just removes its related things |
||
| 2600 | * |
||
| 2601 | * @param string $relationName |
||
| 2602 | * @param array $query_params like EEM_Base::get_all's |
||
| 2603 | * @return int how many deleted |
||
| 2604 | * @throws ReflectionException |
||
| 2605 | * @throws InvalidArgumentException |
||
| 2606 | * @throws InvalidInterfaceException |
||
| 2607 | * @throws InvalidDataTypeException |
||
| 2608 | * @throws EE_Error |
||
| 2609 | */ |
||
| 2610 | View Code Duplication | public function delete_related($relationName, $query_params = array()) |
|
| 2624 | |||
| 2625 | |||
| 2626 | /** |
||
| 2627 | * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
||
| 2628 | * the current model object's relation to them. If they can't be deleted (because |
||
| 2629 | * of blocking related model objects) just does a soft delete on it instead, if possible. |
||
| 2630 | * If the related thing isn't a soft-deletable model object, this function is identical |
||
| 2631 | * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
||
| 2632 | * |
||
| 2633 | * @param string $relationName |
||
| 2634 | * @param array $query_params like EEM_Base::get_all's |
||
| 2635 | * @return int how many deleted (including those soft deleted) |
||
| 2636 | * @throws ReflectionException |
||
| 2637 | * @throws InvalidArgumentException |
||
| 2638 | * @throws InvalidInterfaceException |
||
| 2639 | * @throws InvalidDataTypeException |
||
| 2640 | * @throws EE_Error |
||
| 2641 | */ |
||
| 2642 | View Code Duplication | public function delete_related_permanently($relationName, $query_params = array()) |
|
| 2656 | |||
| 2657 | |||
| 2658 | /** |
||
| 2659 | * is_set |
||
| 2660 | * Just a simple utility function children can use for checking if property exists |
||
| 2661 | * |
||
| 2662 | * @access public |
||
| 2663 | * @param string $field_name property to check |
||
| 2664 | * @return bool TRUE if existing,FALSE if not. |
||
| 2665 | */ |
||
| 2666 | public function is_set($field_name) |
||
| 2670 | |||
| 2671 | |||
| 2672 | /** |
||
| 2673 | * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
||
| 2674 | * EE_Error exception if they don't |
||
| 2675 | * |
||
| 2676 | * @param mixed (string|array) $properties properties to check |
||
| 2677 | * @throws EE_Error |
||
| 2678 | * @return bool TRUE if existing, throw EE_Error if not. |
||
| 2679 | */ |
||
| 2680 | protected function _property_exists($properties) |
||
| 2698 | |||
| 2699 | |||
| 2700 | /** |
||
| 2701 | * This simply returns an array of model fields for this object |
||
| 2702 | * |
||
| 2703 | * @return array |
||
| 2704 | * @throws ReflectionException |
||
| 2705 | * @throws InvalidArgumentException |
||
| 2706 | * @throws InvalidInterfaceException |
||
| 2707 | * @throws InvalidDataTypeException |
||
| 2708 | * @throws EE_Error |
||
| 2709 | */ |
||
| 2710 | public function model_field_array() |
||
| 2720 | |||
| 2721 | |||
| 2722 | /** |
||
| 2723 | * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
||
| 2724 | * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
||
| 2725 | * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. |
||
| 2726 | * Instead of requiring a plugin to extend the EE_Base_Class |
||
| 2727 | * (which works fine is there's only 1 plugin, but when will that happen?) |
||
| 2728 | * they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' |
||
| 2729 | * (eg, filters_hook_espresso__EE_Answer__my_great_function) |
||
| 2730 | * and accepts 2 arguments: the object on which the function was called, |
||
| 2731 | * and an array of the original arguments passed to the function. |
||
| 2732 | * Whatever their callback function returns will be returned by this function. |
||
| 2733 | * Example: in functions.php (or in a plugin): |
||
| 2734 | * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); |
||
| 2735 | * function my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
||
| 2736 | * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
||
| 2737 | * return $previousReturnValue.$returnString; |
||
| 2738 | * } |
||
| 2739 | * require('EE_Answer.class.php'); |
||
| 2740 | * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
||
| 2741 | * echo $answer->my_callback('monkeys',100); |
||
| 2742 | * //will output "you called my_callback! and passed args:monkeys,100" |
||
| 2743 | * |
||
| 2744 | * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
||
| 2745 | * @param array $args array of original arguments passed to the function |
||
| 2746 | * @throws EE_Error |
||
| 2747 | * @return mixed whatever the plugin which calls add_filter decides |
||
| 2748 | */ |
||
| 2749 | public function __call($methodName, $args) |
||
| 2768 | |||
| 2769 | |||
| 2770 | /** |
||
| 2771 | * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
||
| 2772 | * A $previous_value can be specified in case there are many meta rows with the same key |
||
| 2773 | * |
||
| 2774 | * @param string $meta_key |
||
| 2775 | * @param mixed $meta_value |
||
| 2776 | * @param mixed $previous_value |
||
| 2777 | * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
||
| 2778 | * NOTE: if the values haven't changed, returns 0 |
||
| 2779 | * @throws InvalidArgumentException |
||
| 2780 | * @throws InvalidInterfaceException |
||
| 2781 | * @throws InvalidDataTypeException |
||
| 2782 | * @throws EE_Error |
||
| 2783 | * @throws ReflectionException |
||
| 2784 | */ |
||
| 2785 | public function update_extra_meta($meta_key, $meta_value, $previous_value = null) |
||
| 2806 | |||
| 2807 | |||
| 2808 | /** |
||
| 2809 | * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
||
| 2810 | * no other extra meta for this model object have the same key. Returns TRUE if the |
||
| 2811 | * extra meta row was entered, false if not |
||
| 2812 | * |
||
| 2813 | * @param string $meta_key |
||
| 2814 | * @param mixed $meta_value |
||
| 2815 | * @param boolean $unique |
||
| 2816 | * @return boolean |
||
| 2817 | * @throws InvalidArgumentException |
||
| 2818 | * @throws InvalidInterfaceException |
||
| 2819 | * @throws InvalidDataTypeException |
||
| 2820 | * @throws EE_Error |
||
| 2821 | * @throws ReflectionException |
||
| 2822 | * @throws ReflectionException |
||
| 2823 | */ |
||
| 2824 | public function add_extra_meta($meta_key, $meta_value, $unique = false) |
||
| 2851 | |||
| 2852 | |||
| 2853 | /** |
||
| 2854 | * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
||
| 2855 | * is specified, only deletes extra meta records with that value. |
||
| 2856 | * |
||
| 2857 | * @param string $meta_key |
||
| 2858 | * @param mixed $meta_value |
||
| 2859 | * @return int number of extra meta rows deleted |
||
| 2860 | * @throws InvalidArgumentException |
||
| 2861 | * @throws InvalidInterfaceException |
||
| 2862 | * @throws InvalidDataTypeException |
||
| 2863 | * @throws EE_Error |
||
| 2864 | * @throws ReflectionException |
||
| 2865 | */ |
||
| 2866 | public function delete_extra_meta($meta_key, $meta_value = null) |
||
| 2880 | |||
| 2881 | |||
| 2882 | /** |
||
| 2883 | * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
||
| 2884 | * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
||
| 2885 | * You can specify $default is case you haven't found the extra meta |
||
| 2886 | * |
||
| 2887 | * @param string $meta_key |
||
| 2888 | * @param boolean $single |
||
| 2889 | * @param mixed $default if we don't find anything, what should we return? |
||
| 2890 | * @return mixed single value if $single; array if ! $single |
||
| 2891 | * @throws ReflectionException |
||
| 2892 | * @throws InvalidArgumentException |
||
| 2893 | * @throws InvalidInterfaceException |
||
| 2894 | * @throws InvalidDataTypeException |
||
| 2895 | * @throws EE_Error |
||
| 2896 | */ |
||
| 2897 | public function get_extra_meta($meta_key, $single = false, $default = null) |
||
| 2931 | |||
| 2932 | |||
| 2933 | /** |
||
| 2934 | * Returns a simple array of all the extra meta associated with this model object. |
||
| 2935 | * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
||
| 2936 | * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
||
| 2937 | * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
||
| 2938 | * If $one_of_each_key is false, it will return an array with the top-level keys being |
||
| 2939 | * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
||
| 2940 | * finally the extra meta's value as each sub-value. (eg |
||
| 2941 | * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
||
| 2942 | * |
||
| 2943 | * @param boolean $one_of_each_key |
||
| 2944 | * @return array |
||
| 2945 | * @throws ReflectionException |
||
| 2946 | * @throws InvalidArgumentException |
||
| 2947 | * @throws InvalidInterfaceException |
||
| 2948 | * @throws InvalidDataTypeException |
||
| 2949 | * @throws EE_Error |
||
| 2950 | */ |
||
| 2951 | public function all_extra_meta_array($one_of_each_key = true) |
||
| 2977 | |||
| 2978 | |||
| 2979 | /** |
||
| 2980 | * Gets a pretty nice displayable nice for this model object. Often overridden |
||
| 2981 | * |
||
| 2982 | * @return string |
||
| 2983 | * @throws ReflectionException |
||
| 2984 | * @throws InvalidArgumentException |
||
| 2985 | * @throws InvalidInterfaceException |
||
| 2986 | * @throws InvalidDataTypeException |
||
| 2987 | * @throws EE_Error |
||
| 2988 | */ |
||
| 2989 | public function name() |
||
| 3004 | |||
| 3005 | |||
| 3006 | /** |
||
| 3007 | * in_entity_map |
||
| 3008 | * Checks if this model object has been proven to already be in the entity map |
||
| 3009 | * |
||
| 3010 | * @return boolean |
||
| 3011 | * @throws ReflectionException |
||
| 3012 | * @throws InvalidArgumentException |
||
| 3013 | * @throws InvalidInterfaceException |
||
| 3014 | * @throws InvalidDataTypeException |
||
| 3015 | * @throws EE_Error |
||
| 3016 | */ |
||
| 3017 | public function in_entity_map() |
||
| 3022 | |||
| 3023 | |||
| 3024 | /** |
||
| 3025 | * refresh_from_db |
||
| 3026 | * Makes sure the fields and values on this model object are in-sync with what's in the database. |
||
| 3027 | * |
||
| 3028 | * @throws ReflectionException |
||
| 3029 | * @throws InvalidArgumentException |
||
| 3030 | * @throws InvalidInterfaceException |
||
| 3031 | * @throws InvalidDataTypeException |
||
| 3032 | * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
||
| 3033 | * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
||
| 3034 | */ |
||
| 3035 | public function refresh_from_db() |
||
| 3057 | |||
| 3058 | |||
| 3059 | /** |
||
| 3060 | * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
||
| 3061 | * (probably a bad assumption they have made, oh well) |
||
| 3062 | * |
||
| 3063 | * @return string |
||
| 3064 | */ |
||
| 3065 | public function __toString() |
||
| 3074 | |||
| 3075 | |||
| 3076 | /** |
||
| 3077 | * Clear related model objects if they're already in the DB, because otherwise when we |
||
| 3078 | * UN-serialize this model object we'll need to be careful to add them to the entity map. |
||
| 3079 | * This means if we have made changes to those related model objects, and want to unserialize |
||
| 3080 | * the this model object on a subsequent request, changes to those related model objects will be lost. |
||
| 3081 | * Instead, those related model objects should be directly serialized and stored. |
||
| 3082 | * Eg, the following won't work: |
||
| 3083 | * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
||
| 3084 | * $att = $reg->attendee(); |
||
| 3085 | * $att->set( 'ATT_fname', 'Dirk' ); |
||
| 3086 | * update_option( 'my_option', serialize( $reg ) ); |
||
| 3087 | * //END REQUEST |
||
| 3088 | * //START NEXT REQUEST |
||
| 3089 | * $reg = get_option( 'my_option' ); |
||
| 3090 | * $reg->attendee()->save(); |
||
| 3091 | * And would need to be replace with: |
||
| 3092 | * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
||
| 3093 | * $att = $reg->attendee(); |
||
| 3094 | * $att->set( 'ATT_fname', 'Dirk' ); |
||
| 3095 | * update_option( 'my_option', serialize( $reg ) ); |
||
| 3096 | * //END REQUEST |
||
| 3097 | * //START NEXT REQUEST |
||
| 3098 | * $att = get_option( 'my_option' ); |
||
| 3099 | * $att->save(); |
||
| 3100 | * |
||
| 3101 | * @return array |
||
| 3102 | * @throws ReflectionException |
||
| 3103 | * @throws InvalidArgumentException |
||
| 3104 | * @throws InvalidInterfaceException |
||
| 3105 | * @throws InvalidDataTypeException |
||
| 3106 | * @throws EE_Error |
||
| 3107 | */ |
||
| 3108 | public function __sleep() |
||
| 3131 | |||
| 3132 | |||
| 3133 | /** |
||
| 3134 | * restore _props_n_values_provided_in_constructor |
||
| 3135 | * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
||
| 3136 | * and therefore should NOT be used to determine if state change has occurred since initial construction. |
||
| 3137 | * At best, you would only be able to detect if state change has occurred during THIS request. |
||
| 3138 | */ |
||
| 3139 | public function __wakeup() |
||
| 3143 | } |
||
| 3144 | |||
| 3146 |
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.