/** * @param $lhs SugarBean left side bean to add to the relationship. * @param $rhs SugarBean right side bean to add to the relationship. * @param $additionalFields key=>value pairs of fields to save on the relationship * @return boolean true if successful */ public function add($lhs, $rhs, $additionalFields = array()) { $lhsLinkName = $this->lhsLink; $rhsLinkName = $this->rhsLink; //Since this is bean based, we know updating the RHS's field will overwrite any old value, //But we need to use delete to make sure custom logic is called correctly if ($rhs->load_relationship($rhsLinkName)) { $oldLink = $rhs->{$rhsLinkName}; $prevRelated = $oldLink->getBeans(null); foreach ($prevRelated as $oldLHS) { $this->remove($oldLHS, $rhs, false); } } //Make sure we load the current relationship state to the LHS link if (isset($lhs->{$lhsLinkName}) && is_a($lhs->{$lhsLinkName}, "Link2") || $lhs->load_relationship($lhsLinkName)) { $lhs->{$lhsLinkName}->load(); } $this->updateFields($lhs, $rhs, $additionalFields); if (empty($_SESSION['disable_workflow']) || $_SESSION['disable_workflow'] != "Yes") { //Need to call save to update the bean as the relationship is saved on the main table //We don't want to create a save loop though, so make sure we aren't already in the middle of saving this bean SugarRelationship::addToResaveList($rhs); $this->updateLinks($lhs, $lhsLinkName, $rhs, $rhsLinkName); $this->callAfterAdd($lhs, $rhs); $this->callAfterAdd($rhs, $lhs); } }
/** * @param $lhs SugarBean left side bean to add to the relationship. * @param $rhs SugarBean right side bean to add to the relationship. * @param $additionalFields key=>value pairs of fields to save on the relationship * * @return boolean true if successful */ public function add($lhs, $rhs, $additionalFields = array()) { // test to see if the relationship exist if the relationship between the two beans // exist then we just fail out with false as we don't want to re-trigger this // the save and such as it causes problems with the related() in sugarlogic if ($this->relationship_exists($lhs, $rhs) && $lhs->inOperation('saving_related')) { return false; } $lhsLinkName = $this->lhsLink; $rhsLinkName = $this->rhsLink; //Since this is bean based, we know updating the RHS's field will overwrite any old value, //But we need to use delete to make sure custom logic is called correctly if ($rhs->load_relationship($rhsLinkName)) { $oldLink = $rhs->{$rhsLinkName}; $prevRelated = $oldLink->getBeans(null); foreach ($prevRelated as $oldLHS) { if ($oldLHS->id != $lhs->id) { $this->remove($oldLHS, $rhs, false); } } } //Make sure we load the current relationship state to the LHS link if (isset($lhs->{$lhsLinkName}) && is_a($lhs->{$lhsLinkName}, "Link2") || $lhs->load_relationship($lhsLinkName)) { $lhs->{$lhsLinkName}->load(); } if (empty($_SESSION['disable_workflow']) || $_SESSION['disable_workflow'] != "Yes") { $this->callBeforeAdd($lhs, $rhs, $lhsLinkName); $this->callBeforeAdd($rhs, $lhs, $rhsLinkName); } $this->updateFields($lhs, $rhs, $additionalFields); if (empty($_SESSION['disable_workflow']) || $_SESSION['disable_workflow'] != "Yes") { //Need to call save to update the bean as the relationship is saved on the main table //We don't want to create a save loop though, so make sure we aren't already in the middle of saving this bean SugarRelationship::addToResaveList($rhs); $this->updateLinks($lhs, $lhsLinkName, $rhs, $rhsLinkName); $this->callAfterAdd($lhs, $rhs, $lhsLinkName); $this->callAfterAdd($rhs, $lhs, $rhsLinkName); } //One2MBean relationships require that the RHS bean be saved or else the relationship will not be saved. //If we aren't already in a relationship save, intitiate a save now. if (!$lhs->inOperation('saving_related')) { SugarRelationship::resaveRelatedBeans(false); } return true; }
/** * Updates relationships based on changes to fields of type 'parent' which * may or may not have links associated with them * * @param array $exclude */ protected function update_parent_relationships($exclude = array()) { foreach ($this->field_defs as $def) { if (!empty($def['type']) && $def['type'] == "parent") { if (empty($def['type_name']) || empty($def['id_name'])) { continue; } $typeField = $def['type_name']; $idField = $def['id_name']; if (in_array($idField, $exclude)) { continue; } //Determine if the parent field has changed. if (!empty($this->fetched_row[$typeField]) && !empty($this->fetched_row[$idField]) && (empty($this->{$typeField}) || empty($this->{$idField})) || !empty($this->{$typeField}) && !empty($this->{$idField}) && (empty($this->fetched_row[$typeField]) || empty($this->fetched_row[$idField]) || $this->fetched_row[$idField] != $this->{$idField}) || $this->deleted == 1) { $parentLinks = array(); //Correlate links to parent field module types foreach ($this->field_defs as $ldef) { if (!empty($ldef['type']) && $ldef['type'] == "link" && !empty($ldef['relationship'])) { $relDef = SugarRelationshipFactory::getInstance()->getRelationshipDef($ldef['relationship']); if (!empty($relDef['relationship_role_column']) && $relDef['relationship_role_column'] == $typeField) { $parentLinks[$relDef['lhs_module']] = $ldef; } } } // Save $this->$idField, because it can be resetted in case of link->delete() call $idFieldVal = $this->{$idField}; //If we used to have a parent, call remove on that relationship if (!empty($this->fetched_row[$typeField]) && !empty($this->fetched_row[$idField]) && !empty($parentLinks[$this->fetched_row[$typeField]]) && $this->fetched_row[$idField] != $this->{$idField}) { $oldParentLink = $parentLinks[$this->fetched_row[$typeField]]['name']; //Load the relationship if ($this->load_relationship($oldParentLink)) { $this->{$oldParentLink}->delete($this->fetched_row[$idField]); // Should resave the old parent SugarRelationship::addToResaveList(BeanFactory::getBean($this->fetched_row[$typeField], $this->fetched_row[$idField])); } } // If both parent type and parent id are set, save it unless the bean is being deleted if (!empty($this->{$typeField}) && !empty($idFieldVal) && !empty($parentLinks[$this->{$typeField}]['name']) && $this->deleted != 1) { //Now add the new parent $parentLink = $parentLinks[$this->{$typeField}]['name']; if ($this->load_relationship($parentLink)) { $this->{$parentLink}->add($idFieldVal); } } } } } }
/** * Updates relationships based on changes to fields of type 'parent' which * may or may not have links associated with them * * @param array $exclude */ protected function update_parent_relationships($exclude = array()) { foreach ($this->field_defs as $def) { if (!empty($def['type']) && $def['type'] == "parent") { if (empty($def['type_name']) || empty($def['id_name'])) { continue; } $typeField = $def['type_name']; $idField = $def['id_name']; // save the new id $newIdValue = $this->{$idField}; if (in_array($idField, $exclude)) { continue; } //Determine if the parent field has changed. if (!empty($this->fetched_row[$typeField]) && !empty($this->fetched_row[$idField]) && (empty($this->{$typeField}) || empty($this->{$idField})) || !empty($this->{$typeField}) && !empty($this->{$idField}) && (empty($this->fetched_row[$typeField]) || empty($this->fetched_row[$idField]) || $this->fetched_row[$idField] != $this->{$idField}) || $this->deleted == 1) { $parentLinks = array(); //Correlate links to parent field module types foreach ($this->field_defs as $ldef) { if (!empty($ldef['type']) && $ldef['type'] == "link" && !empty($ldef['relationship'])) { $rel = SugarRelationshipFactory::getInstance()->getRelationship($ldef['relationship']); $relColumns = $rel->getRelationshipRoleColumns(); if (isset($relColumns[$typeField])) { $parentLinks[$rel->getLHSModule()] = $ldef; } } } //If we used to have a parent, call remove on that relationship if (!empty($this->fetched_row[$typeField]) && !empty($this->fetched_row[$idField]) && !empty($parentLinks[$this->fetched_row[$typeField]]) && $this->fetched_row[$idField] != $this->{$idField}) { $oldParentLink = $parentLinks[$this->fetched_row[$typeField]]['name']; //Load the relationship if ($this->load_relationship($oldParentLink)) { $this->{$oldParentLink}->delete($this->id, $this->fetched_row[$idField]); // Should resave the old parent, if the current user has access to it and can save it $beanToSave = BeanFactory::getBean($this->fetched_row[$typeField], $this->fetched_row[$idField]); if (!empty($beanToSave->id)) { SugarRelationship::addToResaveList($beanToSave); } } } // If both parent type and parent id are set, save it unless the bean is being deleted if (!empty($this->{$typeField}) && !empty($newIdValue) && !empty($parentLinks[$this->{$typeField}]['name']) && $this->deleted != 1) { //Now add the new parent $parentLink = $parentLinks[$this->{$typeField}]['name']; if ($this->load_relationship($parentLink)) { $this->{$parentLink}->add($newIdValue); } } } } } }