/** * This method operates on all related record, takes action based on cardinality of the relationship. * one-to-one, one-to-many: update the rhs table's parent id with null * many-to-one: update the lhs table's parent-id with null. * many-to-many: delete rows from the link table. related table must have deleted and date_modified column. * If related_id is null, the methods assumes that the parent bean (whose id is passed) is being deleted. * If both id and related_id are passed, the method unlinks a single relationship. * parameters: id of the bean being deleted. * * @param string $id * @param string $related_id * * @return null */ public function delete($id, $related_id = '') { Log::debug(sprintf("delete called with these parameter values. id=%s, related_id=%s", $id, $related_id)); $_relationship =& $this->_relationship; $_bean =& $this->_bean; $bean_is_lhs = $this->_get_bean_position(); if (!isset($bean_is_lhs)) { Log::debug("Invalid relationship parameters. Exiting.."); return null; } if ($_relationship->relationship_type == 'one-to-many' or $_relationship->relationship_type == 'one-to-one') { if ($bean_is_lhs) { // update rhs_table set rhs_key = null, relation_column_name = null where rhs_key= this_bean_id $query = 'UPDATE ' . $_relationship->rhs_table . ' SET ' . $_relationship->rhs_key . "=NULL, date_modified='" . $GLOBALS['timedate']->nowDb() . "'"; if (!empty($_relationship->relationship_role_column) && !empty($_relationship->relationship_role_column_value)) { $query .= ',' . $_relationship->relationship_role_column . "= NULL "; $query .= ' WHERE ' . $_relationship->relationship_role_column . "= '" . $_relationship->relationship_role_column_value . "' AND "; } else { $query .= ' WHERE '; } $query .= $_relationship->rhs_key . "= '" . $id . "' "; // restrict to one row if related_id is passed. if (!empty($related_id)) { $query .= " AND " . $_relationship->rhs_table . ".id='" . $related_id . "'"; } } else { // do nothing because the row that stores the relationship keys is being deleted. // todo log an error message here. // if this is the case and related_id is passed then log a message asking the user // to clear the relationship using the bean. } } if ($_relationship->relationship_type == 'many-to-one') { // do nothing because the row that stores the relationship keys is being deleted. // todo log an error message here. // if this is the case and related_id is passed then log a message asking the user // to clear the relationship using the bean. } if ($_relationship->relationship_type == 'many-to-many') { $use_bean_is_lhs = isset($_REQUEST['ajaxSubpanel']) || $this->_swap_sides !== true; $query = 'UPDATE ' . $_relationship->join_table . " SET deleted=1, date_modified='" . $GLOBALS['timedate']->nowDb() . "'"; if ($bean_is_lhs && $use_bean_is_lhs) { if (!empty($this->_relationship->reverse) && ($this->_relationship->reverse == true or $this->_relationship->reverse == 1)) { if (empty($related_id)) { $query .= " WHERE (" . $_relationship->join_key_lhs . "= '" . $id . "' or " . $_relationship->join_key_rhs . "='" . $id . "')"; } else { $query .= " WHERE (" . $_relationship->join_key_lhs . "= '" . $id . "' AND " . $_relationship->join_key_rhs . "='" . $related_id . "') OR (" . $_relationship->join_key_rhs . "='" . $id . "' AND " . $_relationship->join_key_lhs . "='" . $related_id . "')"; } } else { if (empty($related_id)) { $query .= " WHERE " . $_relationship->join_key_lhs . "= '" . $id . "'"; } else { $query .= " WHERE " . $_relationship->join_key_lhs . "= '" . $id . "' AND " . $_relationship->join_key_rhs . "= '" . $related_id . "'"; } } } else { if (!empty($this->_relationship->reverse) && ($this->_relationship->reverse == true or $this->_relationship->reverse == 1)) { if (empty($related_id)) { $query .= " WHERE (" . $_relationship->join_key_rhs . "= '" . $id . "' or " . $_relationship->join_key_lhs . "='" . $id . "')"; } else { $query .= " WHERE (" . $_relationship->join_key_rhs . "= '" . $id . "' AND " . $_relationship->join_key_lhs . "='" . $related_id . "') OR (" . $_relationship->join_key_lhs . "='" . $id . "' AND " . $_relationship->join_key_rhs . "='" . $related_id . "')"; } } else { if (empty($related_id)) { $query .= " WHERE " . $_relationship->join_key_rhs . "= '" . $id . "'"; } else { $query .= " WHERE " . $_relationship->join_key_rhs . "= '" . $id . "' AND " . $_relationship->join_key_lhs . "= '" . $related_id . "'"; } } if (!empty($_relationship->relationship_role_column) && !empty($_relationship->relationship_role_column_value)) { $query .= ' AND ' . $_relationship->relationship_role_column . "= '" . $_relationship->relationship_role_column_value . "'"; } } } // if query string is not empty execute it. if (isset($query)) { Log::fatal('Link.Delete:Delete Query: ' . $query); $this->_db->query($query, true); } $custom_logic_arguments = []; $custom_logic_arguments['id'] = $id; $custom_logic_arguments['related_id'] = $related_id; $custom_reverse_arguments = []; $custom_reverse_arguments['related_id'] = $id; $custom_reverse_arguments['id'] = $related_id; if ($bean_is_lhs) { $custom_logic_arguments['module'] = $this->_relationship->lhs_module; $custom_logic_arguments['related_module'] = $this->_relationship->rhs_module; $custom_reverse_arguments['module'] = $this->_relationship->lhs_module; $custom_reverse_arguments['related_module'] = $this->_relationship->rhs_module; } else { $custom_logic_arguments['module'] = $this->_relationship->rhs_module; $custom_logic_arguments['related_module'] = $this->_relationship->lhs_module; $custom_reverse_arguments['module'] = $this->_relationship->lhs_module; $custom_reverse_arguments['related_module'] = $this->_relationship->rhs_module; } if (empty($this->_bean->id)) { $this->_bean->retrieve($id); } $this->_bean->call_custom_logic('after_relationship_delete', $custom_logic_arguments); // NOW THE REVERSE WAY SINCE A RELATIONSHIP TAKES TWO global $beanList; if (isset($beanList[$custom_logic_arguments['related_module']])) { $class = $beanList[$custom_logic_arguments['related_module']]; if (!empty($class)) { /** @var SugarBean $rbean */ $rbean = new $class(); $rbean->retrieve(empty($related_id) ? $id : $related_id); $rbean->call_custom_logic('after_relationship_delete', $custom_reverse_arguments); } } }
protected function runQuery(ServiceBase $api, array $args, SugarQuery $q, array $options, SugarBean $seed) { $seed->call_custom_logic("before_filter", array($q, $options)); if (empty($args['fields'])) { $fields = array(); } else { $fields = $options['select']; } $queryOptions = array('returnRawRows' => true, 'compensateDistinct' => true); $fetched = $seed->fetchFromQuery($q, $fields, $queryOptions); list($beans, $rows, $distinctCompensation) = $this->parseQueryResults($fetched); $data = array(); $data['next_offset'] = -1; $i = $distinctCompensation; foreach ($beans as $bean_id => $bean) { if ($i == $options['limit']) { if (count($beans) > $options['limit']) { unset($beans[$bean_id]); } $data['next_offset'] = (int) ($options['limit'] + $options['offset']); continue; } $i++; $this->populateRelatedFields($bean, $rows[$bean_id]); } $data['records'] = $this->formatBeans($api, $args, $beans); return $data; }