/** * Mostly the same as populate_defaults , except takes a model object as input, not an array, * and also sets the form's _model_object * @param EE_Base_Class $model_obj * @return void */ public function populate_model_obj($model_obj) { $model_obj = $this->_model->ensure_is_obj($model_obj); $this->_model_object = $model_obj; $defaults = $model_obj->model_field_array(); foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { $form_inputs = $this->inputs(); if (isset($form_inputs[$relation_name])) { if ($relation_obj instanceof EE_Belongs_To_Relation) { //then we only expect there to be one $related_item = $this->_model_object->get_first_related($relation_name); $defaults[$relation_name] = $related_item->ID(); } else { $related_items = $this->_model_object->get_many_related($relation_name); $ids = array(); foreach ($related_items as $related_item) { $ids[] = $related_item->ID(); } $defaults[$relation_name] = $ids; } } } $this->populate_defaults($defaults); }
/** * Overrides parent ot also check by the slug * @see EEM_Base::ensure_is_obj() * @param string|int|EE_Payment_Method $base_class_obj_or_id * @param boolean $ensure_is_in_db * @return EE_Payment_Method * @throws EE_Error */ public function ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db = FALSE) { //first: check if it's a slug if (is_string($base_class_obj_or_id)) { $obj = $this->get_one_by_slug($base_class_obj_or_id); if ($obj) { return $obj; } } //ok so it wasn't a slug we were passed. try the usual then (ie, it's an object or an ID) try { return parent::ensure_is_obj($base_class_obj_or_id, $ensure_is_in_db); } catch (EE_Error $e) { //handle it outside the catch } throw new EE_Error(sprintf(__("'%s' is neither a Payment Method ID, slug, nor object.", "event_espresso"), $base_class_obj_or_id)); }