Exemple #1
0
 /**
  * use this function to create link between 2 objects
  * 1:1 will be treated like 1 to many.
  *
  * the function also allows for setting of values for additional field in the table being
  * updated to save the relationship, in case of many-to-many relationships this would be the join table.
  *
  * @param array $rel_keys          array of ids or SugarBean objects. If you have the bean in memory, pass it in.
  * @param array $additional_values the values should be passed as key value pairs with column name as the key name
  *                                 and column value as key value.
  *
  * @return boolean|array          Return true if all relationships were added.  Return an array with the failed
  *                                keys if any of them failed.
  */
 function add($rel_keys, $additional_values = [])
 {
     if (!is_array($rel_keys)) {
         $rel_keys = [$rel_keys];
     }
     $failures = [];
     foreach ($rel_keys as $key) {
         //We must use beans for LogicHooks and other business logic to fire correctly
         if (!$key instanceof SugarBean) {
             $key = $this->getRelatedBean($key);
             if (!$key instanceof SugarBean) {
                 Log::error("Unable to load related bean by id");
                 return false;
             }
         }
         if (empty($key->id) || empty($this->focus->id)) {
             return false;
         }
         if ($this->getSide() == REL_LHS) {
             $success = $this->relationship->add($this->focus, $key, $additional_values);
         } else {
             $success = $this->relationship->add($key, $this->focus, $additional_values);
         }
         if ($success == false) {
             $failures[] = $key->id;
         }
     }
     if (!empty($failures)) {
         return $failures;
     }
     return true;
 }