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 if ( ! defined('EVENT_ESPRESSO_VERSION')) { |
||
| 25 | abstract class EE_Base_Class |
||
| 26 | { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * This is an array of the original properties and values provided during construction |
||
| 30 | * of this model object. (keys are model field names, values are their values). |
||
| 31 | * This list is important to remember so that when we are merging data from the db, we know |
||
| 32 | * which values to override and which to not override. |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $_props_n_values_provided_in_constructor; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Timezone |
||
| 40 | * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in. |
||
| 41 | * This can also be used before a get to set what timezone you want strings coming out of the object to be in. NOT |
||
| 42 | * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have |
||
| 43 | * access to it. |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $_timezone; |
||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | /** |
||
| 52 | * date format |
||
| 53 | * pattern or format for displaying dates |
||
| 54 | * |
||
| 55 | * @var string $_dt_frmt |
||
| 56 | */ |
||
| 57 | protected $_dt_frmt; |
||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * time format |
||
| 63 | * pattern or format for displaying time |
||
| 64 | * |
||
| 65 | * @var string $_tm_frmt |
||
| 66 | */ |
||
| 67 | protected $_tm_frmt; |
||
| 68 | |||
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * This property is for holding a cached array of object properties indexed by property name as the key. |
||
| 73 | * The purpose of this is for setting a cache on properties that may have calculated values after a |
||
| 74 | * prepare_for_get. That way the cache can be checked first and the calculated property returned instead of having |
||
| 75 | * to recalculate. Used by _set_cached_property() and _get_cached_property() methods. |
||
| 76 | * |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $_cached_properties = array(); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * An array containing keys of the related model, and values are either an array of related mode objects or a |
||
| 83 | * single |
||
| 84 | * related model object. see the model's _model_relations. The keys should match those specified. And if the |
||
| 85 | * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object, |
||
| 86 | * all others have an array) |
||
| 87 | * |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | protected $_model_relations = array(); |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Array where keys are field names (see the model's _fields property) and values are their values. To see what |
||
| 94 | * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods) |
||
| 95 | * |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | protected $_fields = array(); |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var boolean indicating whether or not this model object is intended to ever be saved |
||
| 102 | * For example, we might create model objects intended to only be used for the duration |
||
| 103 | * of this request and to be thrown away, and if they were accidentally saved |
||
| 104 | * it would be a bug. |
||
| 105 | */ |
||
| 106 | protected $_allow_persist = true; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var boolean indicating whether or not this model object's properties have changed since construction |
||
| 110 | */ |
||
| 111 | protected $_has_changes = false; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var EEM_Base |
||
| 115 | */ |
||
| 116 | protected $_model; |
||
| 117 | |||
| 118 | |||
| 119 | |||
| 120 | /** |
||
| 121 | * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children |
||
| 122 | * play nice |
||
| 123 | * |
||
| 124 | * @param array $fieldValues where each key is a field (ie, array key in the 2nd |
||
| 125 | * layer of the model's _fields array, (eg, EVT_ID, |
||
| 126 | * TXN_amount, QST_name, etc) and values are their values |
||
| 127 | * @param boolean $bydb a flag for setting if the class is instantiated by the |
||
| 128 | * corresponding db model or not. |
||
| 129 | * @param string $timezone indicate what timezone you want any datetime fields to |
||
| 130 | * be in when instantiating a EE_Base_Class object. |
||
| 131 | * @param array $date_formats An array of date formats to set on construct where first |
||
| 132 | * value is the date_format and second value is the time |
||
| 133 | * format. |
||
| 134 | * @throws EE_Error |
||
| 135 | */ |
||
| 136 | protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array()) |
||
| 137 | { |
||
| 138 | $className = get_class($this); |
||
| 139 | do_action("AHEE__{$className}__construct", $this, $fieldValues); |
||
| 140 | $model = $this->get_model(); |
||
| 141 | $model_fields = $model->field_settings(false); |
||
| 142 | // ensure $fieldValues is an array |
||
| 143 | $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues); |
||
| 144 | // EEH_Debug_Tools::printr( $fieldValues, '$fieldValues <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
||
| 145 | // verify client code has not passed any invalid field names |
||
| 146 | foreach ($fieldValues as $field_name => $field_value) { |
||
| 147 | View Code Duplication | if ( ! isset($model_fields[$field_name])) { |
|
| 148 | throw new EE_Error(sprintf(__("Invalid field (%s) passed to constructor of %s. Allowed fields are :%s", |
||
| 149 | "event_espresso"), $field_name, get_class($this), implode(", ", array_keys($model_fields)))); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | // EEH_Debug_Tools::printr( $model_fields, '$model_fields <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
||
| 153 | $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone); |
||
| 154 | if ( ! empty($date_formats) && is_array($date_formats)) { |
||
| 155 | list($this->_dt_frmt, $this->_tm_frmt) = $date_formats; |
||
| 156 | } else { |
||
| 157 | //set default formats for date and time |
||
| 158 | $this->_dt_frmt = (string)get_option('date_format', 'Y-m-d'); |
||
| 159 | $this->_tm_frmt = (string)get_option('time_format', 'g:i a'); |
||
| 160 | } |
||
| 161 | //if db model is instantiating |
||
| 162 | if ($bydb) { |
||
| 163 | //client code has indicated these field values are from the database |
||
| 164 | View Code Duplication | foreach ($model_fields as $fieldName => $field) { |
|
| 165 | $this->set_from_db($fieldName, isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null); |
||
| 166 | } |
||
| 167 | } else { |
||
| 168 | //we're constructing a brand |
||
| 169 | //new instance of the model object. Generally, this means we'll need to do more field validation |
||
| 170 | View Code Duplication | foreach ($model_fields as $fieldName => $field) { |
|
| 171 | $this->set($fieldName, isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null, true); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | //remember what values were passed to this constructor |
||
| 175 | $this->_props_n_values_provided_in_constructor = $fieldValues; |
||
| 176 | //remember in entity mapper |
||
| 177 | if ( ! $bydb && $model->has_primary_key_field() && $this->ID()) { |
||
| 178 | $model->add_to_entity_map($this); |
||
| 179 | } |
||
| 180 | //setup all the relations |
||
| 181 | foreach ($model->relation_settings() as $relation_name => $relation_obj) { |
||
| 182 | if ($relation_obj instanceof EE_Belongs_To_Relation) { |
||
| 183 | $this->_model_relations[$relation_name] = null; |
||
| 184 | } else { |
||
| 185 | $this->_model_relations[$relation_name] = array(); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | /** |
||
| 189 | * Action done at the end of each model object construction |
||
| 190 | * |
||
| 191 | * @param EE_Base_Class $this the model object just created |
||
| 192 | */ |
||
| 193 | do_action('AHEE__EE_Base_Class__construct__finished', $this); |
||
| 194 | } |
||
| 195 | |||
| 196 | |||
| 197 | |||
| 198 | /** |
||
| 199 | * Gets whether or not this model object is allowed to persist/be saved to the database. |
||
| 200 | * |
||
| 201 | * @return boolean |
||
| 202 | */ |
||
| 203 | public function allow_persist() |
||
| 204 | { |
||
| 205 | return $this->_allow_persist; |
||
| 206 | } |
||
| 207 | |||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * Sets whether or not this model object should be allowed to be saved to the DB. |
||
| 212 | * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless |
||
| 213 | * you got new information that somehow made you change your mind. |
||
| 214 | * |
||
| 215 | * @param boolean $allow_persist |
||
| 216 | * @return boolean |
||
| 217 | */ |
||
| 218 | public function set_allow_persist($allow_persist) |
||
| 219 | { |
||
| 220 | return $this->_allow_persist = $allow_persist; |
||
| 221 | } |
||
| 222 | |||
| 223 | |||
| 224 | |||
| 225 | /** |
||
| 226 | * Gets the field's original value when this object was constructed during this request. |
||
| 227 | * This can be helpful when determining if a model object has changed or not |
||
| 228 | * |
||
| 229 | * @param string $field_name |
||
| 230 | * @return mixed|null |
||
| 231 | * @throws \EE_Error |
||
| 232 | */ |
||
| 233 | public function get_original($field_name) |
||
| 234 | { |
||
| 235 | if (isset($this->_props_n_values_provided_in_constructor[$field_name]) |
||
| 236 | && $field_settings = $this->get_model()->field_settings_for($field_name) |
||
| 237 | ) { |
||
| 238 | return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[$field_name]); |
||
| 239 | } else { |
||
| 240 | return null; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | |||
| 245 | |||
| 246 | /** |
||
| 247 | * @param EE_Base_Class $obj |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | public function get_class($obj) |
||
| 251 | { |
||
| 252 | return get_class($obj); |
||
| 253 | } |
||
| 254 | |||
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * Overrides parent because parent expects old models. |
||
| 259 | * This also doesn't do any validation, and won't work for serialized arrays |
||
| 260 | * |
||
| 261 | * @param string $field_name |
||
| 262 | * @param mixed $field_value |
||
| 263 | * @param bool $use_default |
||
| 264 | * @throws \EE_Error |
||
| 265 | */ |
||
| 266 | public function set($field_name, $field_value, $use_default = false) |
||
| 267 | { |
||
| 268 | // if not using default and nothing has changed, and object has already been setup (has ID), |
||
| 269 | // then don't do anything |
||
| 270 | if ( |
||
| 271 | ! $use_default |
||
| 272 | && $this->_fields[$field_name] === $field_value |
||
| 273 | && $this->ID() |
||
| 274 | ) { |
||
| 275 | return; |
||
| 276 | } |
||
| 277 | $model = $this->get_model(); |
||
| 278 | $this->_has_changes = true; |
||
| 279 | $field_obj = $model->field_settings_for($field_name); |
||
| 280 | if ($field_obj instanceof EE_Model_Field_Base) { |
||
| 281 | // if ( method_exists( $field_obj, 'set_timezone' )) { |
||
| 282 | if ($field_obj instanceof EE_Datetime_Field) { |
||
| 283 | $field_obj->set_timezone($this->_timezone); |
||
| 284 | $field_obj->set_date_format($this->_dt_frmt); |
||
| 285 | $field_obj->set_time_format($this->_tm_frmt); |
||
| 286 | } |
||
| 287 | $holder_of_value = $field_obj->prepare_for_set($field_value); |
||
| 288 | //should the value be null? |
||
| 289 | if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) { |
||
| 290 | $this->_fields[$field_name] = $field_obj->get_default_value(); |
||
| 291 | /** |
||
| 292 | * To save having to refactor all the models, if a default value is used for a |
||
| 293 | * EE_Datetime_Field, and that value is not null nor is it a DateTime |
||
| 294 | * object. Then let's do a set again to ensure that it becomes a DateTime |
||
| 295 | * object. |
||
| 296 | * |
||
| 297 | * @since 4.6.10+ |
||
| 298 | */ |
||
| 299 | if ( |
||
| 300 | $field_obj instanceof EE_Datetime_Field |
||
| 301 | && $this->_fields[$field_name] !== null |
||
| 302 | && ! $this->_fields[$field_name] instanceof DateTime |
||
| 303 | ) { |
||
| 304 | empty($this->_fields[$field_name]) |
||
| 305 | ? $this->set($field_name, time()) |
||
| 306 | : $this->set($field_name, $this->_fields[$field_name]); |
||
| 307 | } |
||
| 308 | } else { |
||
| 309 | $this->_fields[$field_name] = $holder_of_value; |
||
| 310 | } |
||
| 311 | //if we're not in the constructor... |
||
| 312 | //now check if what we set was a primary key |
||
| 313 | if ( |
||
| 314 | //note: props_n_values_provided_in_constructor is only set at the END of the constructor |
||
| 315 | $this->_props_n_values_provided_in_constructor |
||
|
|
|||
| 316 | && $field_value |
||
| 317 | && $field_name === $model->primary_key_name() |
||
| 318 | ) { |
||
| 319 | //if so, we want all this object's fields to be filled either with |
||
| 320 | //what we've explicitly set on this model |
||
| 321 | //or what we have in the db |
||
| 322 | // echo "setting primary key!"; |
||
| 323 | $fields_on_model = self::_get_model(get_class($this))->field_settings(); |
||
| 324 | $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value); |
||
| 325 | foreach ($fields_on_model as $field_obj) { |
||
| 326 | if ( ! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor) |
||
| 327 | && $field_obj->get_name() !== $field_name |
||
| 328 | ) { |
||
| 329 | $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name())); |
||
| 330 | } |
||
| 331 | } |
||
| 332 | //oh this model object has an ID? well make sure its in the entity mapper |
||
| 333 | $model->add_to_entity_map($this); |
||
| 334 | } |
||
| 335 | //let's unset any cache for this field_name from the $_cached_properties property. |
||
| 336 | $this->_clear_cached_property($field_name); |
||
| 337 | } else { |
||
| 338 | throw new EE_Error(sprintf(__("A valid EE_Model_Field_Base could not be found for the given field name: %s", |
||
| 339 | "event_espresso"), $field_name)); |
||
| 340 | } |
||
| 341 | } |
||
| 342 | |||
| 343 | |||
| 344 | |||
| 345 | /** |
||
| 346 | * This sets the field value on the db column if it exists for the given $column_name or |
||
| 347 | * saves it to EE_Extra_Meta if the given $column_name does not match a db column. |
||
| 348 | * |
||
| 349 | * @see EE_message::get_column_value for related documentation on the necessity of this method. |
||
| 350 | * @param string $field_name Must be the exact column name. |
||
| 351 | * @param mixed $field_value The value to set. |
||
| 352 | * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs. |
||
| 353 | * @throws \EE_Error |
||
| 354 | */ |
||
| 355 | public function set_field_or_extra_meta($field_name, $field_value) |
||
| 356 | { |
||
| 357 | if ($this->get_model()->has_field($field_name)) { |
||
| 358 | $this->set($field_name, $field_value); |
||
| 359 | return true; |
||
| 360 | } else { |
||
| 361 | //ensure this object is saved first so that extra meta can be properly related. |
||
| 362 | $this->save(); |
||
| 363 | return $this->update_extra_meta($field_name, $field_value); |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * This retrieves the value of the db column set on this class or if that's not present |
||
| 371 | * it will attempt to retrieve from extra_meta if found. |
||
| 372 | * Example Usage: |
||
| 373 | * Via EE_Message child class: |
||
| 374 | * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to", |
||
| 375 | * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may |
||
| 376 | * also have additional main fields specific to the messenger. The system accommodates those extra |
||
| 377 | * fields through the EE_Extra_Meta table. This method allows for EE_messengers to retrieve the |
||
| 378 | * value for those extra fields dynamically via the EE_message object. |
||
| 379 | * |
||
| 380 | * @param string $field_name expecting the fully qualified field name. |
||
| 381 | * @return mixed|null value for the field if found. null if not found. |
||
| 382 | * @throws \EE_Error |
||
| 383 | */ |
||
| 384 | public function get_field_or_extra_meta($field_name) |
||
| 385 | { |
||
| 386 | if ($this->get_model()->has_field($field_name)) { |
||
| 387 | $column_value = $this->get($field_name); |
||
| 388 | } else { |
||
| 389 | //This isn't a column in the main table, let's see if it is in the extra meta. |
||
| 390 | $column_value = $this->get_extra_meta($field_name, true, null); |
||
| 391 | } |
||
| 392 | return $column_value; |
||
| 393 | } |
||
| 394 | |||
| 395 | |||
| 396 | |||
| 397 | /** |
||
| 398 | * See $_timezone property for description of what the timezone property is for. This SETS the timezone internally |
||
| 399 | * for being able to reference what timezone we are running conversions on when converting TO the internal timezone |
||
| 400 | * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is |
||
| 401 | * available to all child classes that may be using the EE_Datetime_Field for a field data type. |
||
| 402 | * |
||
| 403 | * @access public |
||
| 404 | * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php |
||
| 405 | * @return void |
||
| 406 | * @throws \EE_Error |
||
| 407 | */ |
||
| 408 | public function set_timezone($timezone = '') |
||
| 424 | |||
| 425 | |||
| 426 | |||
| 427 | /** |
||
| 428 | * This just returns whatever is set for the current timezone. |
||
| 429 | * |
||
| 430 | * @access public |
||
| 431 | * @return string timezone string |
||
| 432 | */ |
||
| 433 | public function get_timezone() |
||
| 434 | { |
||
| 435 | return $this->_timezone; |
||
| 436 | } |
||
| 437 | |||
| 438 | |||
| 439 | |||
| 440 | /** |
||
| 441 | * This sets the internal date format to what is sent in to be used as the new default for the class |
||
| 442 | * internally instead of wp set date format options |
||
| 443 | * |
||
| 444 | * @since 4.6 |
||
| 445 | * @param string $format should be a format recognizable by PHP date() functions. |
||
| 446 | */ |
||
| 447 | public function set_date_format($format) |
||
| 448 | { |
||
| 449 | $this->_dt_frmt = $format; |
||
| 450 | //clear cached_properties because they won't be relevant now. |
||
| 451 | $this->_clear_cached_properties(); |
||
| 452 | } |
||
| 453 | |||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * This sets the internal time format string to what is sent in to be used as the new default for the |
||
| 458 | * class internally instead of wp set time format options. |
||
| 459 | * |
||
| 460 | * @since 4.6 |
||
| 461 | * @param string $format should be a format recognizable by PHP date() functions. |
||
| 462 | */ |
||
| 463 | public function set_time_format($format) |
||
| 464 | { |
||
| 465 | $this->_tm_frmt = $format; |
||
| 466 | //clear cached_properties because they won't be relevant now. |
||
| 467 | $this->_clear_cached_properties(); |
||
| 468 | } |
||
| 469 | |||
| 470 | |||
| 471 | |||
| 472 | /** |
||
| 473 | * This returns the current internal set format for the date and time formats. |
||
| 474 | * |
||
| 475 | * @param bool $full if true (default), then return the full format. Otherwise will return an array |
||
| 476 | * where the first value is the date format and the second value is the time format. |
||
| 477 | * @return mixed string|array |
||
| 478 | */ |
||
| 479 | public function get_format($full = true) |
||
| 480 | { |
||
| 481 | return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt); |
||
| 482 | } |
||
| 483 | |||
| 484 | |||
| 485 | |||
| 486 | /** |
||
| 487 | * cache |
||
| 488 | * stores the passed model object on the current model object. |
||
| 489 | * In certain circumstances, we can use this cached model object instead of querying for another one entirely. |
||
| 490 | * |
||
| 491 | * @param string $relationName one of the keys in the _model_relations array on the model. Eg |
||
| 492 | * 'Registration' associated with this model object |
||
| 493 | * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction, |
||
| 494 | * that could be a payment or a registration) |
||
| 495 | * @param null $cache_id a string or number that will be used as the key for any Belongs_To_Many |
||
| 496 | * items which will be stored in an array on this object |
||
| 497 | * @throws EE_Error |
||
| 498 | * @return mixed index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one |
||
| 499 | * related thing, no array) |
||
| 500 | */ |
||
| 501 | public function cache($relationName = '', $object_to_cache = null, $cache_id = null) |
||
| 502 | { |
||
| 503 | // its entirely possible that there IS no related object yet in which case there is nothing to cache. |
||
| 504 | if ( ! $object_to_cache instanceof EE_Base_Class) { |
||
| 505 | return false; |
||
| 506 | } |
||
| 507 | // also get "how" the object is related, or throw an error |
||
| 508 | if ( ! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) { |
||
| 509 | throw new EE_Error(sprintf(__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'), |
||
| 510 | $relationName, get_class($this))); |
||
| 511 | } |
||
| 512 | // how many things are related ? |
||
| 513 | if ($relationship_to_model instanceof EE_Belongs_To_Relation) { |
||
| 514 | // if it's a "belongs to" relationship, then there's only one related model object eg, if this is a registration, there's only 1 attendee for it |
||
| 515 | // so for these model objects just set it to be cached |
||
| 516 | $this->_model_relations[$relationName] = $object_to_cache; |
||
| 517 | $return = true; |
||
| 518 | } else { |
||
| 519 | // otherwise, this is the "many" side of a one to many relationship, so we'll add the object to the array of related objects for that type. |
||
| 520 | // eg: if this is an event, there are many registrations for that event, so we cache the registrations in an array |
||
| 521 | if ( ! is_array($this->_model_relations[$relationName])) { |
||
| 522 | // if for some reason, the cached item is a model object, then stick that in the array, otherwise start with an empty array |
||
| 523 | $this->_model_relations[$relationName] = $this->_model_relations[$relationName] instanceof EE_Base_Class |
||
| 524 | ? array($this->_model_relations[$relationName]) : array(); |
||
| 525 | } |
||
| 526 | // first check for a cache_id which is normally empty |
||
| 527 | if ( ! empty($cache_id)) { |
||
| 528 | // if the cache_id exists, then it means we are purposely trying to cache this with a known key that can then be used to retrieve the object later on |
||
| 529 | $this->_model_relations[$relationName][$cache_id] = $object_to_cache; |
||
| 530 | $return = $cache_id; |
||
| 531 | } elseif ($object_to_cache->ID()) { |
||
| 532 | // OR the cached object originally came from the db, so let's just use it's PK for an ID |
||
| 533 | $this->_model_relations[$relationName][$object_to_cache->ID()] = $object_to_cache; |
||
| 534 | $return = $object_to_cache->ID(); |
||
| 535 | } else { |
||
| 536 | // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID |
||
| 537 | $this->_model_relations[$relationName][] = $object_to_cache; |
||
| 538 | // move the internal pointer to the end of the array |
||
| 539 | end($this->_model_relations[$relationName]); |
||
| 540 | // and grab the key so that we can return it |
||
| 541 | $return = key($this->_model_relations[$relationName]); |
||
| 542 | } |
||
| 543 | } |
||
| 544 | return $return; |
||
| 545 | } |
||
| 546 | |||
| 547 | |||
| 548 | |||
| 549 | /** |
||
| 550 | * For adding an item to the cached_properties property. |
||
| 551 | * |
||
| 552 | * @access protected |
||
| 553 | * @param string $fieldname the property item the corresponding value is for. |
||
| 554 | * @param mixed $value The value we are caching. |
||
| 555 | * @param string|null $cache_type |
||
| 556 | * @return void |
||
| 557 | * @throws \EE_Error |
||
| 558 | */ |
||
| 559 | protected function _set_cached_property($fieldname, $value, $cache_type = null) |
||
| 566 | |||
| 567 | |||
| 568 | |||
| 569 | /** |
||
| 570 | * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist. |
||
| 571 | * This also SETS the cache if we return the actual property! |
||
| 572 | * |
||
| 573 | * @param string $fieldname the name of the property we're trying to retrieve |
||
| 574 | * @param bool $pretty |
||
| 575 | * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
||
| 576 | * (in cases where the same property may be used for different outputs |
||
| 577 | * - i.e. datetime, money etc.) |
||
| 578 | * It can also accept certain pre-defined "schema" strings |
||
| 579 | * to define how to output the property. |
||
| 580 | * see the field's prepare_for_pretty_echoing for what strings can be used |
||
| 581 | * @return mixed whatever the value for the property is we're retrieving |
||
| 582 | * @throws \EE_Error |
||
| 583 | */ |
||
| 584 | protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null) |
||
| 598 | |||
| 599 | |||
| 600 | |||
| 601 | /** |
||
| 602 | * If the cache didn't fetch the needed item, this fetches it. |
||
| 603 | * @param string $fieldname |
||
| 604 | * @param bool $pretty |
||
| 605 | * @param string $extra_cache_ref |
||
| 606 | * @return mixed |
||
| 607 | */ |
||
| 608 | protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null) |
||
| 623 | |||
| 624 | |||
| 625 | |||
| 626 | /** |
||
| 627 | * set timezone, formats, and output for EE_Datetime_Field objects |
||
| 628 | * |
||
| 629 | * @param \EE_Datetime_Field $datetime_field |
||
| 630 | * @param bool $pretty |
||
| 631 | * @param null $date_or_time |
||
| 632 | * @return void |
||
| 633 | * @throws \EE_Error |
||
| 634 | */ |
||
| 635 | protected function _prepare_datetime_field( |
||
| 655 | |||
| 656 | |||
| 657 | |||
| 658 | /** |
||
| 659 | * This just takes care of clearing out the cached_properties |
||
| 660 | * |
||
| 661 | * @return void |
||
| 662 | */ |
||
| 663 | protected function _clear_cached_properties() |
||
| 667 | |||
| 668 | |||
| 669 | |||
| 670 | /** |
||
| 671 | * This just clears out ONE property if it exists in the cache |
||
| 672 | * |
||
| 673 | * @param string $property_name the property to remove if it exists (from the _cached_properties array) |
||
| 674 | * @return void |
||
| 675 | */ |
||
| 676 | protected function _clear_cached_property($property_name) |
||
| 682 | |||
| 683 | |||
| 684 | |||
| 685 | /** |
||
| 686 | * Ensures that this related thing is a model object. |
||
| 687 | * |
||
| 688 | * @param mixed $object_or_id EE_base_Class/int/string either a related model object, or its ID |
||
| 689 | * @param string $model_name name of the related thing, eg 'Attendee', |
||
| 690 | * @return EE_Base_Class |
||
| 691 | * @throws \EE_Error |
||
| 692 | */ |
||
| 693 | protected function ensure_related_thing_is_model_obj($object_or_id, $model_name) |
||
| 701 | |||
| 702 | |||
| 703 | |||
| 704 | /** |
||
| 705 | * Forgets the cached model of the given relation Name. So the next time we request it, |
||
| 706 | * we will fetch it again from the database. (Handy if you know it's changed somehow). |
||
| 707 | * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM, |
||
| 708 | * then only remove that one object from our cached array. Otherwise, clear the entire list |
||
| 709 | * |
||
| 710 | * @param string $relationName one of the keys in the _model_relations array on the model. |
||
| 711 | * Eg 'Registration' |
||
| 712 | * @param mixed $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL |
||
| 713 | * if you intend to use $clear_all = TRUE, or the relation only |
||
| 714 | * has 1 object anyways (ie, it's a BelongsToRelation) |
||
| 715 | * @param bool $clear_all This flags clearing the entire cache relation property if |
||
| 716 | * this is HasMany or HABTM. |
||
| 717 | * @throws EE_Error |
||
| 718 | * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a |
||
| 719 | * relation from all |
||
| 720 | */ |
||
| 721 | public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false) |
||
| 797 | |||
| 798 | |||
| 799 | |||
| 800 | /** |
||
| 801 | * update_cache_after_object_save |
||
| 802 | * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has |
||
| 803 | * obtained after being saved to the db |
||
| 804 | * |
||
| 805 | * @param string $relationName - the type of object that is cached |
||
| 806 | * @param \EE_Base_Class $newly_saved_object - the newly saved object to be re-cached |
||
| 807 | * @param string $current_cache_id - the ID that was used when originally caching the object |
||
| 808 | * @return boolean TRUE on success, FALSE on fail |
||
| 809 | * @throws \EE_Error |
||
| 810 | */ |
||
| 811 | public function update_cache_after_object_save( |
||
| 840 | |||
| 841 | |||
| 842 | |||
| 843 | /** |
||
| 844 | * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
||
| 845 | * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
||
| 846 | * |
||
| 847 | * @param string $relationName |
||
| 848 | * @return EE_Base_Class |
||
| 849 | */ |
||
| 850 | public function get_one_from_cache($relationName) |
||
| 860 | |||
| 861 | |||
| 862 | |||
| 863 | /** |
||
| 864 | * Fetches a single EE_Base_Class on that relation. (If the relation is of type |
||
| 865 | * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects) |
||
| 866 | * |
||
| 867 | * @param string $relationName |
||
| 868 | * @throws \EE_Error |
||
| 869 | * @return EE_Base_Class[] NOT necessarily indexed by primary keys |
||
| 870 | */ |
||
| 871 | public function get_all_from_cache($relationName) |
||
| 903 | |||
| 904 | |||
| 905 | |||
| 906 | /** |
||
| 907 | * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database |
||
| 908 | * matching the given query conditions. |
||
| 909 | * |
||
| 910 | * @param null $field_to_order_by What field is being used as the reference point. |
||
| 911 | * @param int $limit How many objects to return. |
||
| 912 | * @param array $query_params Any additional conditions on the query. |
||
| 913 | * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
||
| 914 | * you can indicate just the columns you want returned |
||
| 915 | * @return array|EE_Base_Class[] |
||
| 916 | * @throws \EE_Error |
||
| 917 | */ |
||
| 918 | View Code Duplication | public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null) |
|
| 930 | |||
| 931 | |||
| 932 | |||
| 933 | /** |
||
| 934 | * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database |
||
| 935 | * matching the given query conditions. |
||
| 936 | * |
||
| 937 | * @param null $field_to_order_by What field is being used as the reference point. |
||
| 938 | * @param int $limit How many objects to return. |
||
| 939 | * @param array $query_params Any additional conditions on the query. |
||
| 940 | * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
||
| 941 | * you can indicate just the columns you want returned |
||
| 942 | * @return array|EE_Base_Class[] |
||
| 943 | * @throws \EE_Error |
||
| 944 | */ |
||
| 945 | View Code Duplication | public function previous_x( |
|
| 961 | |||
| 962 | |||
| 963 | |||
| 964 | /** |
||
| 965 | * Returns the next EE_Base_Class object in sequence from this object as found in the database |
||
| 966 | * matching the given query conditions. |
||
| 967 | * |
||
| 968 | * @param null $field_to_order_by What field is being used as the reference point. |
||
| 969 | * @param array $query_params Any additional conditions on the query. |
||
| 970 | * @param null $columns_to_select If left null, then an array of EE_Base_Class objects is returned, otherwise |
||
| 971 | * you can indicate just the columns you want returned |
||
| 972 | * @return array|EE_Base_Class |
||
| 973 | * @throws \EE_Error |
||
| 974 | */ |
||
| 975 | View Code Duplication | public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 987 | |||
| 988 | |||
| 989 | |||
| 990 | /** |
||
| 991 | * Returns the previous EE_Base_Class object in sequence from this object as found in the database |
||
| 992 | * matching the given query conditions. |
||
| 993 | * |
||
| 994 | * @param null $field_to_order_by What field is being used as the reference point. |
||
| 995 | * @param array $query_params Any additional conditions on the query. |
||
| 996 | * @param null $columns_to_select If left null, then an EE_Base_Class object is returned, otherwise |
||
| 997 | * you can indicate just the column you want returned |
||
| 998 | * @return array|EE_Base_Class |
||
| 999 | * @throws \EE_Error |
||
| 1000 | */ |
||
| 1001 | View Code Duplication | public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null) |
|
| 1013 | |||
| 1014 | |||
| 1015 | |||
| 1016 | /** |
||
| 1017 | * Overrides parent because parent expects old models. |
||
| 1018 | * This also doesn't do any validation, and won't work for serialized arrays |
||
| 1019 | * |
||
| 1020 | * @param string $field_name |
||
| 1021 | * @param mixed $field_value_from_db |
||
| 1022 | * @throws \EE_Error |
||
| 1023 | */ |
||
| 1024 | public function set_from_db($field_name, $field_value_from_db) |
||
| 1046 | |||
| 1047 | |||
| 1048 | |||
| 1049 | /** |
||
| 1050 | * verifies that the specified field is of the correct type |
||
| 1051 | * |
||
| 1052 | * @param string $field_name |
||
| 1053 | * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
||
| 1054 | * (in cases where the same property may be used for different outputs |
||
| 1055 | * - i.e. datetime, money etc.) |
||
| 1056 | * @return mixed |
||
| 1057 | * @throws \EE_Error |
||
| 1058 | */ |
||
| 1059 | public function get($field_name, $extra_cache_ref = null) |
||
| 1063 | |||
| 1064 | |||
| 1065 | |||
| 1066 | /** |
||
| 1067 | * This method simply returns the RAW unprocessed value for the given property in this class |
||
| 1068 | * |
||
| 1069 | * @param string $field_name A valid fieldname |
||
| 1070 | * @return mixed Whatever the raw value stored on the property is. |
||
| 1071 | * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist. |
||
| 1072 | */ |
||
| 1073 | public function get_raw($field_name) |
||
| 1080 | |||
| 1081 | |||
| 1082 | |||
| 1083 | /** |
||
| 1084 | * This is used to return the internal DateTime object used for a field that is a |
||
| 1085 | * EE_Datetime_Field. |
||
| 1086 | * |
||
| 1087 | * @param string $field_name The field name retrieving the DateTime object. |
||
| 1088 | * @return mixed null | false | DateTime If the requested field is NOT a EE_Datetime_Field then |
||
| 1089 | * @throws \EE_Error |
||
| 1090 | * an error is set and false returned. If the field IS an |
||
| 1091 | * EE_Datetime_Field and but the field value is null, then |
||
| 1092 | * just null is returned (because that indicates that likely |
||
| 1093 | * this field is nullable). |
||
| 1094 | */ |
||
| 1095 | public function get_DateTime_object($field_name) |
||
| 1096 | { |
||
| 1097 | $field_settings = $this->get_model()->field_settings_for($field_name); |
||
| 1098 | View Code Duplication | if ( ! $field_settings instanceof EE_Datetime_Field) { |
|
| 1099 | EE_Error::add_error( |
||
| 1100 | sprintf( |
||
| 1101 | __( |
||
| 1102 | 'The field %s is not an EE_Datetime_Field field. There is no DateTime object stored on this field type.', |
||
| 1103 | 'event_espresso' |
||
| 1104 | ), |
||
| 1105 | $field_name |
||
| 1106 | ), |
||
| 1107 | __FILE__, |
||
| 1108 | __FUNCTION__, |
||
| 1109 | __LINE__ |
||
| 1110 | ); |
||
| 1111 | return false; |
||
| 1112 | } |
||
| 1113 | return $this->_fields[$field_name]; |
||
| 1114 | } |
||
| 1115 | |||
| 1116 | |||
| 1117 | |||
| 1118 | /** |
||
| 1119 | * To be used in template to immediately echo out the value, and format it for output. |
||
| 1120 | * Eg, should call stripslashes and whatnot before echoing |
||
| 1121 | * |
||
| 1122 | * @param string $field_name the name of the field as it appears in the DB |
||
| 1123 | * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
||
| 1124 | * (in cases where the same property may be used for different outputs |
||
| 1125 | * - i.e. datetime, money etc.) |
||
| 1126 | * @return void |
||
| 1127 | * @throws \EE_Error |
||
| 1128 | */ |
||
| 1129 | public function e($field_name, $extra_cache_ref = null) |
||
| 1133 | |||
| 1134 | |||
| 1135 | |||
| 1136 | /** |
||
| 1137 | * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it |
||
| 1138 | * can be easily used as the value of form input. |
||
| 1139 | * |
||
| 1140 | * @param string $field_name |
||
| 1141 | * @return void |
||
| 1142 | * @throws \EE_Error |
||
| 1143 | */ |
||
| 1144 | public function f($field_name) |
||
| 1148 | |||
| 1149 | /** |
||
| 1150 | * Same as `f()` but just returns the value instead of echoing it |
||
| 1151 | * @param string $field_name |
||
| 1152 | * @return string |
||
| 1153 | */ |
||
| 1154 | public function get_f($field_name) |
||
| 1158 | |||
| 1159 | |||
| 1160 | |||
| 1161 | /** |
||
| 1162 | * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this. |
||
| 1163 | * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class |
||
| 1164 | * to see what options are available. |
||
| 1165 | * @param string $field_name |
||
| 1166 | * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property |
||
| 1167 | * (in cases where the same property may be used for different outputs |
||
| 1168 | * - i.e. datetime, money etc.) |
||
| 1169 | * @return mixed |
||
| 1170 | * @throws \EE_Error |
||
| 1171 | */ |
||
| 1172 | public function get_pretty($field_name, $extra_cache_ref = null) |
||
| 1176 | |||
| 1177 | |||
| 1178 | |||
| 1179 | /** |
||
| 1180 | * This simply returns the datetime for the given field name |
||
| 1181 | * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions |
||
| 1182 | * (and the equivalent e_date, e_time, e_datetime). |
||
| 1183 | * |
||
| 1184 | * @access protected |
||
| 1185 | * @param string $field_name Field on the instantiated EE_Base_Class child object |
||
| 1186 | * @param string $dt_frmt valid datetime format used for date |
||
| 1187 | * (if '' then we just use the default on the field, |
||
| 1188 | * if NULL we use the last-used format) |
||
| 1189 | * @param string $tm_frmt Same as above except this is for time format |
||
| 1190 | * @param string $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time. |
||
| 1191 | * @param boolean $echo Whether the dtt is echoing using pretty echoing or just returned using vanilla get |
||
| 1192 | * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown |
||
| 1193 | * if field is not a valid dtt field, or void if echoing |
||
| 1194 | * @throws \EE_Error |
||
| 1195 | */ |
||
| 1196 | protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false) |
||
| 1209 | |||
| 1210 | |||
| 1211 | |||
| 1212 | /** |
||
| 1213 | * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date |
||
| 1214 | * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
||
| 1215 | * other echoes the pretty value for dtt) |
||
| 1216 | * |
||
| 1217 | * @param string $field_name name of model object datetime field holding the value |
||
| 1218 | * @param string $format format for the date returned (if NULL we use default in dt_frmt property) |
||
| 1219 | * @return string datetime value formatted |
||
| 1220 | * @throws \EE_Error |
||
| 1221 | */ |
||
| 1222 | public function get_date($field_name, $format = '') |
||
| 1226 | |||
| 1227 | |||
| 1228 | |||
| 1229 | /** |
||
| 1230 | * @param $field_name |
||
| 1231 | * @param string $format |
||
| 1232 | * @throws \EE_Error |
||
| 1233 | */ |
||
| 1234 | public function e_date($field_name, $format = '') |
||
| 1238 | |||
| 1239 | |||
| 1240 | |||
| 1241 | /** |
||
| 1242 | * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time |
||
| 1243 | * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
||
| 1244 | * other echoes the pretty value for dtt) |
||
| 1245 | * |
||
| 1246 | * @param string $field_name name of model object datetime field holding the value |
||
| 1247 | * @param string $format format for the time returned ( if NULL we use default in tm_frmt property) |
||
| 1248 | * @return string datetime value formatted |
||
| 1249 | * @throws \EE_Error |
||
| 1250 | */ |
||
| 1251 | public function get_time($field_name, $format = '') |
||
| 1255 | |||
| 1256 | |||
| 1257 | |||
| 1258 | /** |
||
| 1259 | * @param $field_name |
||
| 1260 | * @param string $format |
||
| 1261 | * @throws \EE_Error |
||
| 1262 | */ |
||
| 1263 | public function e_time($field_name, $format = '') |
||
| 1267 | |||
| 1268 | |||
| 1269 | |||
| 1270 | /** |
||
| 1271 | * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND |
||
| 1272 | * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the |
||
| 1273 | * other echoes the pretty value for dtt) |
||
| 1274 | * |
||
| 1275 | * @param string $field_name name of model object datetime field holding the value |
||
| 1276 | * @param string $dt_frmt format for the date returned (if NULL we use default in dt_frmt property) |
||
| 1277 | * @param string $tm_frmt format for the time returned (if NULL we use default in tm_frmt property) |
||
| 1278 | * @return string datetime value formatted |
||
| 1279 | * @throws \EE_Error |
||
| 1280 | */ |
||
| 1281 | public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
||
| 1285 | |||
| 1286 | |||
| 1287 | |||
| 1288 | /** |
||
| 1289 | * @param string $field_name |
||
| 1290 | * @param string $dt_frmt |
||
| 1291 | * @param string $tm_frmt |
||
| 1292 | * @throws \EE_Error |
||
| 1293 | */ |
||
| 1294 | public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '') |
||
| 1298 | |||
| 1299 | |||
| 1300 | |||
| 1301 | /** |
||
| 1302 | * Get the i8ln value for a date using the WordPress @see date_i18n function. |
||
| 1303 | * |
||
| 1304 | * @param string $field_name The EE_Datetime_Field reference for the date being retrieved. |
||
| 1305 | * @param string $format PHP valid date/time string format. If none is provided then the internal set format |
||
| 1306 | * on the object will be used. |
||
| 1307 | * @return string Date and time string in set locale or false if no field exists for the given |
||
| 1308 | * @throws \EE_Error |
||
| 1309 | * field name. |
||
| 1310 | */ |
||
| 1311 | public function get_i18n_datetime($field_name, $format = '') |
||
| 1319 | |||
| 1320 | |||
| 1321 | |||
| 1322 | /** |
||
| 1323 | * This method validates whether the given field name is a valid field on the model object as well as it is of a |
||
| 1324 | * type EE_Datetime_Field. On success there will be returned the field settings. On fail an EE_Error exception is |
||
| 1325 | * thrown. |
||
| 1326 | * |
||
| 1327 | * @param string $field_name The field name being checked |
||
| 1328 | * @throws EE_Error |
||
| 1329 | * @return EE_Datetime_Field |
||
| 1330 | */ |
||
| 1331 | protected function _get_dtt_field_settings($field_name) |
||
| 1342 | |||
| 1343 | |||
| 1344 | |||
| 1345 | |||
| 1346 | /** |
||
| 1347 | * NOTE ABOUT BELOW: |
||
| 1348 | * These convenience date and time setters are for setting date and time independently. In other words you might |
||
| 1349 | * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand |
||
| 1350 | * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value) |
||
| 1351 | * method and make sure you send the entire datetime value for setting. |
||
| 1352 | */ |
||
| 1353 | /** |
||
| 1354 | * sets the time on a datetime property |
||
| 1355 | * |
||
| 1356 | * @access protected |
||
| 1357 | * @param string|Datetime $time a valid time string for php datetime functions (or DateTime object) |
||
| 1358 | * @param string $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field) |
||
| 1359 | * @throws \EE_Error |
||
| 1360 | */ |
||
| 1361 | protected function _set_time_for($time, $fieldname) |
||
| 1365 | |||
| 1366 | |||
| 1367 | |||
| 1368 | /** |
||
| 1369 | * sets the date on a datetime property |
||
| 1370 | * |
||
| 1371 | * @access protected |
||
| 1372 | * @param string|DateTime $date a valid date string for php datetime functions ( or DateTime object) |
||
| 1373 | * @param string $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field) |
||
| 1374 | * @throws \EE_Error |
||
| 1375 | */ |
||
| 1376 | protected function _set_date_for($date, $fieldname) |
||
| 1380 | |||
| 1381 | |||
| 1382 | |||
| 1383 | /** |
||
| 1384 | * This takes care of setting a date or time independently on a given model object property. This method also |
||
| 1385 | * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field |
||
| 1386 | * |
||
| 1387 | * @access protected |
||
| 1388 | * @param string $what "T" for time, 'B' for both, 'D' for Date. |
||
| 1389 | * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object) |
||
| 1390 | * @param string $fieldname the name of the field the date OR time is being set on (must match a |
||
| 1391 | * EE_Datetime_Field property) |
||
| 1392 | * @throws \EE_Error |
||
| 1393 | */ |
||
| 1394 | protected function _set_date_time($what = 'T', $datetime_value, $fieldname) |
||
| 1419 | |||
| 1420 | |||
| 1421 | |||
| 1422 | /** |
||
| 1423 | * This will return a timestamp for the website timezone but ONLY when the current website timezone is different |
||
| 1424 | * than the timezone set for the website. NOTE, this currently only works well with methods that return values. If |
||
| 1425 | * you use it with methods that echo values the $_timestamp property may not get reset to its original value and |
||
| 1426 | * that could lead to some unexpected results! |
||
| 1427 | * |
||
| 1428 | * @access public |
||
| 1429 | * @param string $field_name This is the name of the field on the object that contains the date/time |
||
| 1430 | * value being returned. |
||
| 1431 | * @param string $callback must match a valid method in this class (defaults to get_datetime) |
||
| 1432 | * @param mixed (array|string) $args This is the arguments that will be passed to the callback. |
||
| 1433 | * @param string $prepend You can include something to prepend on the timestamp |
||
| 1434 | * @param string $append You can include something to append on the timestamp |
||
| 1435 | * @throws EE_Error |
||
| 1436 | * @return string timestamp |
||
| 1437 | */ |
||
| 1438 | public function display_in_my_timezone( |
||
| 1469 | |||
| 1470 | |||
| 1471 | |||
| 1472 | /** |
||
| 1473 | * Deletes this model object. |
||
| 1474 | * This calls the `EE_Base_Class::_delete` method. Child classes wishing to change default behaviour should |
||
| 1475 | * override |
||
| 1476 | * `EE_Base_Class::_delete` NOT this class. |
||
| 1477 | * |
||
| 1478 | * @return boolean | int |
||
| 1479 | * @throws \EE_Error |
||
| 1480 | */ |
||
| 1481 | public function delete() |
||
| 1505 | |||
| 1506 | |||
| 1507 | |||
| 1508 | /** |
||
| 1509 | * Calls the specific delete method for the instantiated class. |
||
| 1510 | * This method is called by the public `EE_Base_Class::delete` method. Any child classes desiring to override |
||
| 1511 | * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT |
||
| 1512 | * `EE_Base_Class::delete` |
||
| 1513 | * |
||
| 1514 | * @return bool|int |
||
| 1515 | * @throws \EE_Error |
||
| 1516 | */ |
||
| 1517 | protected function _delete() |
||
| 1521 | |||
| 1522 | |||
| 1523 | |||
| 1524 | /** |
||
| 1525 | * Deletes this model object permanently from db (but keep in mind related models my block the delete and return an |
||
| 1526 | * error) |
||
| 1527 | * |
||
| 1528 | * @return bool | int |
||
| 1529 | * @throws \EE_Error |
||
| 1530 | */ |
||
| 1531 | public function delete_permanently() |
||
| 1551 | |||
| 1552 | |||
| 1553 | |||
| 1554 | /** |
||
| 1555 | * When this model object is deleted, it may still be cached on related model objects. This clears the cache of |
||
| 1556 | * related model objects |
||
| 1557 | * |
||
| 1558 | * @throws \EE_Error |
||
| 1559 | */ |
||
| 1560 | public function refresh_cache_of_related_objects() |
||
| 1580 | |||
| 1581 | |||
| 1582 | |||
| 1583 | /** |
||
| 1584 | * Saves this object to the database. An array may be supplied to set some values on this |
||
| 1585 | * object just before saving. |
||
| 1586 | * |
||
| 1587 | * @access public |
||
| 1588 | * @param array $set_cols_n_values keys are field names, values are their new values, |
||
| 1589 | * if provided during the save() method (often client code will change the fields' |
||
| 1590 | * values before calling save) |
||
| 1591 | * @throws \EE_Error |
||
| 1592 | * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object |
||
| 1593 | * isn't allowed to persist (as determined by EE_Base_Class::allow_persist()) |
||
| 1594 | */ |
||
| 1595 | public function save($set_cols_n_values = array()) |
||
| 1705 | |||
| 1706 | |||
| 1707 | |||
| 1708 | /** |
||
| 1709 | * Updates the foreign key on related models objects pointing to this to have this model object's ID |
||
| 1710 | * as their foreign key. If the cached related model objects already exist in the db, saves them (so that the DB |
||
| 1711 | * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its |
||
| 1712 | * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't |
||
| 1713 | * 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 |
||
| 1714 | * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether |
||
| 1715 | * or not they exist in the DB (if they do, their DB records will be automatically updated) |
||
| 1716 | * |
||
| 1717 | * @return void |
||
| 1718 | * @throws \EE_Error |
||
| 1719 | */ |
||
| 1720 | protected function _update_cached_related_model_objs_fks() |
||
| 1737 | |||
| 1738 | |||
| 1739 | |||
| 1740 | /** |
||
| 1741 | * Saves this model object and its NEW cached relations to the database. |
||
| 1742 | * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB. |
||
| 1743 | * In order for that to work, we would need to mark model objects as dirty/clean... |
||
| 1744 | * because otherwise, there's a potential for infinite looping of saving |
||
| 1745 | * Saves the cached related model objects, and ensures the relation between them |
||
| 1746 | * and this object and properly setup |
||
| 1747 | * |
||
| 1748 | * @return int ID of new model object on save; 0 on failure+ |
||
| 1749 | * @throws \EE_Error |
||
| 1750 | */ |
||
| 1751 | public function save_new_cached_related_model_objs() |
||
| 1787 | |||
| 1788 | |||
| 1789 | |||
| 1790 | /** |
||
| 1791 | * for getting a model while instantiated. |
||
| 1792 | * |
||
| 1793 | * @return \EEM_Base | \EEM_CPT_Base |
||
| 1794 | */ |
||
| 1795 | public function get_model() |
||
| 1806 | |||
| 1807 | |||
| 1808 | |||
| 1809 | /** |
||
| 1810 | * @param $props_n_values |
||
| 1811 | * @param $classname |
||
| 1812 | * @return mixed bool|EE_Base_Class|EEM_CPT_Base |
||
| 1813 | * @throws \EE_Error |
||
| 1814 | */ |
||
| 1815 | protected static function _get_object_from_entity_mapper($props_n_values, $classname) |
||
| 1825 | |||
| 1826 | |||
| 1827 | |||
| 1828 | /** |
||
| 1829 | * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for |
||
| 1830 | * the primary key (if present in incoming values). If there is a key in the incoming array that matches the |
||
| 1831 | * 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 |
||
| 1832 | * we return false. |
||
| 1833 | * |
||
| 1834 | * @param array $props_n_values incoming array of properties and their values |
||
| 1835 | * @param string $classname the classname of the child class |
||
| 1836 | * @param null $timezone |
||
| 1837 | * @param array $date_formats incoming date_formats in an array where the first value is the |
||
| 1838 | * date_format and the second value is the time format |
||
| 1839 | * @return mixed (EE_Base_Class|bool) |
||
| 1840 | * @throws \EE_Error |
||
| 1841 | */ |
||
| 1842 | protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array()) |
||
| 1879 | |||
| 1880 | |||
| 1881 | |||
| 1882 | /** |
||
| 1883 | * Gets the EEM_*_Model for this class |
||
| 1884 | * |
||
| 1885 | * @access public now, as this is more convenient |
||
| 1886 | * @param $classname |
||
| 1887 | * @param null $timezone |
||
| 1888 | * @throws EE_Error |
||
| 1889 | * @return EEM_Base |
||
| 1890 | */ |
||
| 1891 | protected static function _get_model($classname, $timezone = null) |
||
| 1908 | |||
| 1909 | |||
| 1910 | |||
| 1911 | /** |
||
| 1912 | * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee) |
||
| 1913 | * |
||
| 1914 | * @param string $model_classname |
||
| 1915 | * @param null $timezone |
||
| 1916 | * @return EEM_Base |
||
| 1917 | */ |
||
| 1918 | protected static function _get_model_instance_with_name($model_classname, $timezone = null) |
||
| 1925 | |||
| 1926 | |||
| 1927 | |||
| 1928 | /** |
||
| 1929 | * If a model name is provided (eg Registration), gets the model classname for that model. |
||
| 1930 | * Also works if a model class's classname is provided (eg EE_Registration). |
||
| 1931 | * |
||
| 1932 | * @param null $model_name |
||
| 1933 | * @return string like EEM_Attendee |
||
| 1934 | */ |
||
| 1935 | private static function _get_model_classname($model_name = null) |
||
| 1944 | |||
| 1945 | |||
| 1946 | |||
| 1947 | /** |
||
| 1948 | * returns the name of the primary key attribute |
||
| 1949 | * |
||
| 1950 | * @param null $classname |
||
| 1951 | * @throws EE_Error |
||
| 1952 | * @return string |
||
| 1953 | */ |
||
| 1954 | protected static function _get_primary_key_name($classname = null) |
||
| 1966 | |||
| 1967 | |||
| 1968 | |||
| 1969 | /** |
||
| 1970 | * Gets the value of the primary key. |
||
| 1971 | * If the object hasn't yet been saved, it should be whatever the model field's default was |
||
| 1972 | * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value |
||
| 1973 | * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL). |
||
| 1974 | * |
||
| 1975 | * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string |
||
| 1976 | * @throws \EE_Error |
||
| 1977 | */ |
||
| 1978 | public function ID() |
||
| 1988 | |||
| 1989 | |||
| 1990 | |||
| 1991 | /** |
||
| 1992 | * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current |
||
| 1993 | * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE |
||
| 1994 | * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing |
||
| 1995 | * |
||
| 1996 | * @param mixed $otherObjectModelObjectOrID EE_Base_Class or the ID of the other object |
||
| 1997 | * @param string $relationName eg 'Events','Question',etc. |
||
| 1998 | * an attendee to a group, you also want to specify which role they |
||
| 1999 | * will have in that group. So you would use this parameter to |
||
| 2000 | * specify array('role-column-name'=>'role-id') |
||
| 2001 | * @param array $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that |
||
| 2002 | * allow you to further constrict the relation to being added. |
||
| 2003 | * However, keep in mind that the columns (keys) given must match a |
||
| 2004 | * column on the JOIN table and currently only the HABTM models |
||
| 2005 | * accept these additional conditions. Also remember that if an |
||
| 2006 | * exact match isn't found for these extra cols/val pairs, then a |
||
| 2007 | * NEW row is created in the join table. |
||
| 2008 | * @param null $cache_id |
||
| 2009 | * @throws EE_Error |
||
| 2010 | * @return EE_Base_Class the object the relation was added to |
||
| 2011 | */ |
||
| 2012 | public function _add_relation_to( |
||
| 2056 | |||
| 2057 | |||
| 2058 | |||
| 2059 | /** |
||
| 2060 | * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current |
||
| 2061 | * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE |
||
| 2062 | * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing |
||
| 2063 | * from the cache |
||
| 2064 | * |
||
| 2065 | * @param mixed $otherObjectModelObjectOrID |
||
| 2066 | * EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved |
||
| 2067 | * to the DB yet |
||
| 2068 | * @param string $relationName |
||
| 2069 | * @param array $where_query |
||
| 2070 | * You can optionally include an array of key=>value pairs that allow you to further constrict the |
||
| 2071 | * relation to being added. However, keep in mind that the columns (keys) given must match a column |
||
| 2072 | * on the JOIN table and currently only the HABTM models accept these additional conditions. Also |
||
| 2073 | * remember that if an exact match isn't found for these extra cols/val pairs, then a NEW row is |
||
| 2074 | * created in the join table. |
||
| 2075 | * @return EE_Base_Class the relation was removed from |
||
| 2076 | * @throws \EE_Error |
||
| 2077 | */ |
||
| 2078 | public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array()) |
||
| 2095 | |||
| 2096 | |||
| 2097 | |||
| 2098 | /** |
||
| 2099 | * Removes ALL the related things for the $relationName. |
||
| 2100 | * |
||
| 2101 | * @param string $relationName |
||
| 2102 | * @param array $where_query_params like EEM_Base::get_all's $query_params[0] (where conditions) |
||
| 2103 | * @return EE_Base_Class |
||
| 2104 | * @throws \EE_Error |
||
| 2105 | */ |
||
| 2106 | public function _remove_relations($relationName, $where_query_params = array()) |
||
| 2123 | |||
| 2124 | |||
| 2125 | |||
| 2126 | /** |
||
| 2127 | * Gets all the related model objects of the specified type. Eg, if the current class if |
||
| 2128 | * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the |
||
| 2129 | * EE_Registration objects which related to this event. Note: by default, we remove the "default query params" |
||
| 2130 | * because we want to get even deleted items etc. |
||
| 2131 | * |
||
| 2132 | * @param string $relationName key in the model's _model_relations array |
||
| 2133 | * @param array $query_params like EEM_Base::get_all |
||
| 2134 | * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary keys |
||
| 2135 | * @throws \EE_Error |
||
| 2136 | * or might not be saved yet. Consider using EEM_Base::get_IDs() on these results if |
||
| 2137 | * you want IDs |
||
| 2138 | */ |
||
| 2139 | public function get_many_related($relationName, $query_params = array()) |
||
| 2166 | |||
| 2167 | |||
| 2168 | |||
| 2169 | /** |
||
| 2170 | * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default, |
||
| 2171 | * unless otherwise specified in the $query_params |
||
| 2172 | * |
||
| 2173 | * @param string $relation_name model_name like 'Event', or 'Registration' |
||
| 2174 | * @param array $query_params like EEM_Base::get_all's |
||
| 2175 | * @param string $field_to_count name of field to count by. By default, uses primary key |
||
| 2176 | * @param bool $distinct if we want to only count the distinct values for the column then you can trigger |
||
| 2177 | * that by the setting $distinct to TRUE; |
||
| 2178 | * @return int |
||
| 2179 | */ |
||
| 2180 | public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false) |
||
| 2184 | |||
| 2185 | |||
| 2186 | |||
| 2187 | /** |
||
| 2188 | * Instead of getting the related model objects, simply sums up the values of the specified field. |
||
| 2189 | * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params |
||
| 2190 | * |
||
| 2191 | * @param string $relation_name model_name like 'Event', or 'Registration' |
||
| 2192 | * @param array $query_params like EEM_Base::get_all's |
||
| 2193 | * @param string $field_to_sum name of field to count by. |
||
| 2194 | * By default, uses primary key (which doesn't make much sense, so you should probably |
||
| 2195 | * change it) |
||
| 2196 | * @return int |
||
| 2197 | */ |
||
| 2198 | public function sum_related($relation_name, $query_params = array(), $field_to_sum = null) |
||
| 2202 | |||
| 2203 | |||
| 2204 | |||
| 2205 | /** |
||
| 2206 | * Gets the first (ie, one) related model object of the specified type. |
||
| 2207 | * |
||
| 2208 | * @param string $relationName key in the model's _model_relations array |
||
| 2209 | * @param array $query_params like EEM_Base::get_all |
||
| 2210 | * @return EE_Base_Class (not an array, a single object) |
||
| 2211 | * @throws \EE_Error |
||
| 2212 | */ |
||
| 2213 | public function get_first_related($relationName, $query_params = array()) |
||
| 2249 | |||
| 2250 | |||
| 2251 | |||
| 2252 | /** |
||
| 2253 | * Does a delete on all related objects of type $relationName and removes |
||
| 2254 | * the current model object's relation to them. If they can't be deleted (because |
||
| 2255 | * of blocking related model objects) does nothing. If the related model objects are |
||
| 2256 | * soft-deletable, they will be soft-deleted regardless of related blocking model objects. |
||
| 2257 | * If this model object doesn't exist yet in the DB, just removes its related things |
||
| 2258 | * |
||
| 2259 | * @param string $relationName |
||
| 2260 | * @param array $query_params like EEM_Base::get_all's |
||
| 2261 | * @return int how many deleted |
||
| 2262 | * @throws \EE_Error |
||
| 2263 | */ |
||
| 2264 | View Code Duplication | public function delete_related($relationName, $query_params = array()) |
|
| 2274 | |||
| 2275 | |||
| 2276 | |||
| 2277 | /** |
||
| 2278 | * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes |
||
| 2279 | * the current model object's relation to them. If they can't be deleted (because |
||
| 2280 | * of blocking related model objects) just does a soft delete on it instead, if possible. |
||
| 2281 | * If the related thing isn't a soft-deletable model object, this function is identical |
||
| 2282 | * to delete_related(). If this model object doesn't exist in the DB, just remove its related things |
||
| 2283 | * |
||
| 2284 | * @param string $relationName |
||
| 2285 | * @param array $query_params like EEM_Base::get_all's |
||
| 2286 | * @return int how many deleted (including those soft deleted) |
||
| 2287 | * @throws \EE_Error |
||
| 2288 | */ |
||
| 2289 | View Code Duplication | public function delete_related_permanently($relationName, $query_params = array()) |
|
| 2299 | |||
| 2300 | |||
| 2301 | |||
| 2302 | /** |
||
| 2303 | * is_set |
||
| 2304 | * Just a simple utility function children can use for checking if property exists |
||
| 2305 | * |
||
| 2306 | * @access public |
||
| 2307 | * @param string $field_name property to check |
||
| 2308 | * @return bool TRUE if existing,FALSE if not. |
||
| 2309 | */ |
||
| 2310 | public function is_set($field_name) |
||
| 2314 | |||
| 2315 | |||
| 2316 | |||
| 2317 | /** |
||
| 2318 | * Just a simple utility function children can use for checking if property (or properties) exists and throwing an |
||
| 2319 | * EE_Error exception if they don't |
||
| 2320 | * |
||
| 2321 | * @param mixed (string|array) $properties properties to check |
||
| 2322 | * @throws EE_Error |
||
| 2323 | * @return bool TRUE if existing, throw EE_Error if not. |
||
| 2324 | */ |
||
| 2325 | protected function _property_exists($properties) |
||
| 2343 | |||
| 2344 | |||
| 2345 | |||
| 2346 | /** |
||
| 2347 | * This simply returns an array of model fields for this object |
||
| 2348 | * |
||
| 2349 | * @return array |
||
| 2350 | * @throws \EE_Error |
||
| 2351 | */ |
||
| 2352 | public function model_field_array() |
||
| 2362 | |||
| 2363 | |||
| 2364 | |||
| 2365 | /** |
||
| 2366 | * Very handy general function to allow for plugins to extend any child of EE_Base_Class. |
||
| 2367 | * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called |
||
| 2368 | * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. Instead of |
||
| 2369 | * requiring a plugin to extend the EE_Base_Class (which works fine is there's only 1 plugin, but when will that |
||
| 2370 | * happen?) they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' (eg, |
||
| 2371 | * filters_hook_espresso__EE_Answer__my_great_function) and accepts 2 arguments: the object on which the function |
||
| 2372 | * was called, and an array of the original arguments passed to the function. Whatever their callback function |
||
| 2373 | * returns will be returned by this function. Example: in functions.php (or in a plugin): |
||
| 2374 | * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); function |
||
| 2375 | * my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){ |
||
| 2376 | * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray); |
||
| 2377 | * return $previousReturnValue.$returnString; |
||
| 2378 | * } |
||
| 2379 | * require('EE_Answer.class.php'); |
||
| 2380 | * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42')); |
||
| 2381 | * echo $answer->my_callback('monkeys',100); |
||
| 2382 | * //will output "you called my_callback! and passed args:monkeys,100" |
||
| 2383 | * |
||
| 2384 | * @param string $methodName name of method which was called on a child of EE_Base_Class, but which |
||
| 2385 | * @param array $args array of original arguments passed to the function |
||
| 2386 | * @throws EE_Error |
||
| 2387 | * @return mixed whatever the plugin which calls add_filter decides |
||
| 2388 | */ |
||
| 2389 | public function __call($methodName, $args) |
||
| 2408 | |||
| 2409 | |||
| 2410 | |||
| 2411 | /** |
||
| 2412 | * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value. |
||
| 2413 | * A $previous_value can be specified in case there are many meta rows with the same key |
||
| 2414 | * |
||
| 2415 | * @param string $meta_key |
||
| 2416 | * @param mixed $meta_value |
||
| 2417 | * @param mixed $previous_value |
||
| 2418 | * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row) |
||
| 2419 | * @throws \EE_Error |
||
| 2420 | * NOTE: if the values haven't changed, returns 0 |
||
| 2421 | */ |
||
| 2422 | public function update_extra_meta($meta_key, $meta_value, $previous_value = null) |
||
| 2443 | |||
| 2444 | |||
| 2445 | |||
| 2446 | /** |
||
| 2447 | * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check |
||
| 2448 | * no other extra meta for this model object have the same key. Returns TRUE if the |
||
| 2449 | * extra meta row was entered, false if not |
||
| 2450 | * |
||
| 2451 | * @param string $meta_key |
||
| 2452 | * @param mixed $meta_value |
||
| 2453 | * @param boolean $unique |
||
| 2454 | * @return boolean |
||
| 2455 | * @throws \EE_Error |
||
| 2456 | */ |
||
| 2457 | public function add_extra_meta($meta_key, $meta_value, $unique = false) |
||
| 2484 | |||
| 2485 | |||
| 2486 | |||
| 2487 | /** |
||
| 2488 | * Deletes all the extra meta rows for this record as specified by key. If $meta_value |
||
| 2489 | * is specified, only deletes extra meta records with that value. |
||
| 2490 | * |
||
| 2491 | * @param string $meta_key |
||
| 2492 | * @param mixed $meta_value |
||
| 2493 | * @return int number of extra meta rows deleted |
||
| 2494 | * @throws \EE_Error |
||
| 2495 | */ |
||
| 2496 | public function delete_extra_meta($meta_key, $meta_value = null) |
||
| 2510 | |||
| 2511 | |||
| 2512 | |||
| 2513 | /** |
||
| 2514 | * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise |
||
| 2515 | * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation. |
||
| 2516 | * You can specify $default is case you haven't found the extra meta |
||
| 2517 | * |
||
| 2518 | * @param string $meta_key |
||
| 2519 | * @param boolean $single |
||
| 2520 | * @param mixed $default if we don't find anything, what should we return? |
||
| 2521 | * @return mixed single value if $single; array if ! $single |
||
| 2522 | * @throws \EE_Error |
||
| 2523 | */ |
||
| 2524 | public function get_extra_meta($meta_key, $single = false, $default = null) |
||
| 2552 | |||
| 2553 | |||
| 2554 | |||
| 2555 | /** |
||
| 2556 | * Returns a simple array of all the extra meta associated with this model object. |
||
| 2557 | * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the |
||
| 2558 | * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with |
||
| 2559 | * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123)) |
||
| 2560 | * If $one_of_each_key is false, it will return an array with the top-level keys being |
||
| 2561 | * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and |
||
| 2562 | * finally the extra meta's value as each sub-value. (eg |
||
| 2563 | * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123))) |
||
| 2564 | * |
||
| 2565 | * @param boolean $one_of_each_key |
||
| 2566 | * @return array |
||
| 2567 | * @throws \EE_Error |
||
| 2568 | */ |
||
| 2569 | public function all_extra_meta_array($one_of_each_key = true) |
||
| 2592 | |||
| 2593 | |||
| 2594 | |||
| 2595 | /** |
||
| 2596 | * Gets a pretty nice displayable nice for this model object. Often overridden |
||
| 2597 | * |
||
| 2598 | * @return string |
||
| 2599 | * @throws \EE_Error |
||
| 2600 | */ |
||
| 2601 | public function name() |
||
| 2617 | |||
| 2618 | |||
| 2619 | |||
| 2620 | /** |
||
| 2621 | * in_entity_map |
||
| 2622 | * Checks if this model object has been proven to already be in the entity map |
||
| 2623 | * |
||
| 2624 | * @return boolean |
||
| 2625 | * @throws \EE_Error |
||
| 2626 | */ |
||
| 2627 | public function in_entity_map() |
||
| 2636 | |||
| 2637 | |||
| 2638 | |||
| 2639 | /** |
||
| 2640 | * refresh_from_db |
||
| 2641 | * Makes sure the fields and values on this model object are in-sync with what's in the database. |
||
| 2642 | * |
||
| 2643 | * @throws EE_Error if this model object isn't in the entity mapper (because then you should |
||
| 2644 | * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE |
||
| 2645 | */ |
||
| 2646 | public function refresh_from_db() |
||
| 2668 | |||
| 2669 | |||
| 2670 | |||
| 2671 | /** |
||
| 2672 | * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method |
||
| 2673 | * (probably a bad assumption they have made, oh well) |
||
| 2674 | * |
||
| 2675 | * @return string |
||
| 2676 | */ |
||
| 2677 | public function __toString() |
||
| 2686 | |||
| 2687 | |||
| 2688 | |||
| 2689 | /** |
||
| 2690 | * Clear related model objects if they're already in the DB, because otherwise when we |
||
| 2691 | * UN-serialize this model object we'll need to be careful to add them to the entity map. |
||
| 2692 | * This means if we have made changes to those related model objects, and want to unserialize |
||
| 2693 | * the this model object on a subsequent request, changes to those related model objects will be lost. |
||
| 2694 | * Instead, those related model objects should be directly serialized and stored. |
||
| 2695 | * Eg, the following won't work: |
||
| 2696 | * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
||
| 2697 | * $att = $reg->attendee(); |
||
| 2698 | * $att->set( 'ATT_fname', 'Dirk' ); |
||
| 2699 | * update_option( 'my_option', serialize( $reg ) ); |
||
| 2700 | * //END REQUEST |
||
| 2701 | * //START NEXT REQUEST |
||
| 2702 | * $reg = get_option( 'my_option' ); |
||
| 2703 | * $reg->attendee()->save(); |
||
| 2704 | * And would need to be replace with: |
||
| 2705 | * $reg = EEM_Registration::instance()->get_one_by_ID( 123 ); |
||
| 2706 | * $att = $reg->attendee(); |
||
| 2707 | * $att->set( 'ATT_fname', 'Dirk' ); |
||
| 2708 | * update_option( 'my_option', serialize( $reg ) ); |
||
| 2709 | * //END REQUEST |
||
| 2710 | * //START NEXT REQUEST |
||
| 2711 | * $att = get_option( 'my_option' ); |
||
| 2712 | * $att->save(); |
||
| 2713 | * |
||
| 2714 | * @return array |
||
| 2715 | * @throws \EE_Error |
||
| 2716 | */ |
||
| 2717 | public function __sleep() |
||
| 2737 | |||
| 2738 | |||
| 2739 | |||
| 2740 | /** |
||
| 2741 | * restore _props_n_values_provided_in_constructor |
||
| 2742 | * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization, |
||
| 2743 | * and therefore should NOT be used to determine if state change has occurred since initial construction. |
||
| 2744 | * At best, you would only be able to detect if state change has occurred during THIS request. |
||
| 2745 | */ |
||
| 2746 | public function __wakeup() |
||
| 2750 | |||
| 2751 | |||
| 2752 | |||
| 2753 | } |
||
| 2754 | |||
| 2756 |
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.