/**
  * Object representing the relationship between two models. HasAndBelongsToMany relations always use a join-table
  * (and an ee joining-model.) This knows how to join the models,
  * get related models across the relation, and add-and-remove the relationships.
  * @param boolean $block_deletes for this type of relation, we block by default for now. if there are related models across this relation, block (prevent and add an error) the deletion of this model
  * @param type $blocking_delete_error_message a customized error message on blocking deletes instead of the default
  */
 function __construct($joining_model_name, $block_deletes = true, $blocking_delete_error_message = '')
 {
     $this->_joining_model_name = $joining_model_name;
     parent::__construct($block_deletes, $blocking_delete_error_message);
 }
 /**
  * Object representing the relationship between two models. Belongs_To means that THIS model has the foreign key
  * to the other model. This knows how to join the models,
  * get related models across the relation, and add-and-remove the relationships.
  * @param boolean $block_deletes For Belongs_To relations, this is set to FALSE by default. if there are related models across this relation, block (prevent and add an error) the deletion of this model
  * @param type $blocking_delete_error_message a customized error message on blocking deletes instead of the default
  */
 function __construct($block_deletes = false, $related_model_objects_deletion_error_message = null)
 {
     parent::__construct($block_deletes, $related_model_objects_deletion_error_message);
 }
 /**
  * Gets the collection for given relation object
  *
  * The same as Read::get_entities_from_model(), except if the relation
  * is a HABTM relation, in which case it merges any non-foreign-key fields from
  * the join-model-object into the results
  *
  * @param string $id the ID of the thing we are fetching related stuff from
  * @param \EE_Model_Relation_Base $relation
  * @param \WP_REST_Request $request
  * @return array
  */
 public function get_entities_from_relation($id, $relation, $request)
 {
     $context = $this->validate_context($request->get_param('caps'));
     $model = $relation->get_this_model();
     $related_model = $relation->get_other_model();
     //check if they can access the 1st model object
     $query_params = array(array($model->primary_key_name() => $id), 'limit' => 1);
     if ($model instanceof \EEM_Soft_Delete_Base) {
         $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params);
     }
     $restricted_query_params = $query_params;
     $restricted_query_params['caps'] = $context;
     $this->_set_debug_info('main model query params', $restricted_query_params);
     $this->_set_debug_info('missing caps', Capabilities::get_missing_permissions_string($related_model, $context));
     if (!(Capabilities::current_user_has_partial_access_to($related_model, $context) && $model->exists($restricted_query_params))) {
         if ($relation instanceof \EE_Belongs_To_Relation) {
             $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name());
         } else {
             $related_model_name_maybe_plural = \EEH_Inflector::pluralize_and_lower($related_model->get_this_model_name());
         }
         return new \WP_Error(sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), sprintf(__('Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', 'event_espresso'), $related_model_name_maybe_plural, $relation->get_this_model()->get_this_model_name(), implode(',', array_keys(Capabilities::get_missing_permissions($related_model, $context)))), array('status' => 403));
     }
     $query_params = $this->create_model_query_params($relation->get_other_model(), $request->get_params());
     $query_params[0][$relation->get_this_model()->get_this_model_name() . '.' . $relation->get_this_model()->primary_key_name()] = $id;
     $query_params['default_where_conditions'] = 'none';
     $query_params['caps'] = $context;
     $this->_set_debug_info('model query params', $query_params);
     /** @type array $results */
     $results = $relation->get_other_model()->get_all_wpdb_results($query_params);
     $nice_results = array();
     foreach ($results as $result) {
         $nice_result = $this->create_entity_from_wpdb_result($relation->get_other_model(), $result, $request->get_param('include'), $query_params['caps']);
         if ($relation instanceof \EE_HABTM_Relation) {
             //put the unusual stuff (properties from the HABTM relation) first, and make sure
             //if there are conflicts we prefer the properties from the main model
             $join_model_result = $this->create_entity_from_wpdb_result($relation->get_join_model(), $result, $request->get_param('include'), $query_params['caps']);
             $joined_result = array_merge($nice_result, $join_model_result);
             //but keep the meta stuff from the main model
             if (isset($nice_result['meta'])) {
                 $joined_result['meta'] = $nice_result['meta'];
             }
             $nice_result = $joined_result;
         }
         $nice_results[] = $nice_result;
     }
     if ($relation instanceof \EE_Belongs_To_Relation) {
         return array_shift($nice_results);
     } else {
         return $nice_results;
     }
 }
 /**
  * Object representing the relationship between two models. Has_Many_Relations are where the OTHER model has the foreign key
  * this model. IE, there can be many other model objects related to one of this model's objects (but NOT through a JOIN table,
  * which is the case for EE_HABTM_Relations). This knows how to join the models,
  * get related models across the relation, and add-and-remove the relationships.
  * @param boolean $block_deletes For this type of relation, we block by default. If there are related models across this relation, block (prevent and add an error) the deletion of this model
  * @param type $blocking_delete_error_message a customized error message on blocking deletes instead of the default
  */
 function __construct($block_deletes = true, $blocking_delete_error_message = null)
 {
     parent::__construct($block_deletes, $blocking_delete_error_message);
 }