Example #1
0
 /**
  * @outputBuffering disabled
  */
 public function testM2MRelationships()
 {
     require_once 'data/Relationships/M2MRelationship.php';
     $def = array('table' => 'accounts_contacts', 'join_table' => 'accounts_contacts', 'name' => 'accounts_contacts', 'lhs_module' => 'accounts', 'rhs_module' => 'contacts');
     $m2mRelationship = new M2MRelationship($def);
     $m2mRelationship->join_key_lhs = 'account_id';
     $m2mRelationship->join_key_rhs = 'contact_id';
     $result = $m2mRelationship->relationship_exists($this->account, $this->contact);
     $entry_id = $GLOBALS['db']->getOne("SELECT id FROM accounts_contacts WHERE account_id='{$this->account->id}' AND contact_id = '{$this->contact->id}'");
     $this->assertEquals($entry_id, $result);
     $result = $m2mRelationship->relationship_exists($this->account2, $this->contact);
     $this->assertEmpty($result);
 }
 /**
  * {@inheritDoc}
  */
 public function remove($lhs, $rhs, $save = true)
 {
     $result = parent::remove($lhs, $rhs, $save);
     if ($result) {
         $this->registerUserAclRoles($rhs);
     }
     return $result;
 }
 /**
  * @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())
 {
     $dataToInsert = $this->getRowToInsert($lhs, $rhs, $additionalFields);
     //If the current data matches the existing data, don't do anything
     if (!$this->checkExisting($dataToInsert)) {
         $lhsLinkName = $this->lhsLink;
         $rhsLinkName = $this->rhsLink;
         //In a one to one, any existing links from boths sides must be removed first.
         //one2Many will take care of the right side, so we'll do the left.
         $lhs->load_relationship($lhsLinkName);
         $this->removeAll($lhs->{$lhsLinkName});
         $rhs->load_relationship($rhsLinkName);
         $this->removeAll($rhs->{$rhsLinkName});
         parent::add($lhs, $rhs, $additionalFields);
     }
 }
 /**
  * @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())
 {
     $dataToInsert = $this->getRowToInsert($lhs, $rhs, $additionalFields);
     //If the current data matches the existing data, don't do anything
     if (!$this->checkExisting($dataToInsert)) {
         $rhsLinkName = $this->rhsLink;
         //In a one to many, any existing links from the many (right) side must be removed first
         $rhs->load_relationship($rhsLinkName);
         $this->removeAll($rhs->{$rhsLinkName});
         parent::add($lhs, $rhs, $additionalFields);
     }
 }
 /**
  * @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())
 {
     $dataToInsert = $this->getRowToInsert($lhs, $rhs, $additionalFields);
     //If the current data matches the existing data, don't do anything
     if (!$this->checkExisting($dataToInsert)) {
         // If it's a One2Many self-referencing relationship
         // the positions of the default One (LHS) and Many (RHS) are swaped
         // so we should clear the links from the many (left) side
         if ($this->selfReferencing) {
             // Load right hand side relationship name
             $linkName = $this->rhsLink;
             // Load the relationship into the left hand side bean
             $lhs->load_relationship($linkName);
             // Pick the loaded link
             $link = $lhs->{$linkName};
             // Get many (LHS) side bean
             $focus = $link->getFocus();
             // Get relations
             $related = $link->getBeans();
             // Clear the relations from many side bean
             foreach ($related as $relBean) {
                 $this->remove($focus, $relBean);
             }
         } else {
             // For non self-referencing, just load the many (RHS) side, and remove all the relationships from it
             $rhsLinkName = $this->rhsLink;
             //In a one to many, any existing links from the many (right) side must be removed first
             $rhs->load_relationship($rhsLinkName);
             $this->removeAll($rhs->{$rhsLinkName});
         }
         // Add relationship
         parent::add($lhs, $rhs, $additionalFields);
     }
 }
 /**
  * Gets the relationship role column check for the where clause
  * This overload adds additional bean check for the primary_address variable.
  *
  * @param string $table
  * @param bool   $ignore_role_filter
  *
  * @return string
  */
 protected function getRoleWhere($table = "", $ignore_role_filter = false)
 {
     $roleCheck = parent::getRoleWhere($table, $ignore_role_filter);
     if ($this->def['relationship_role_column'] == 'primary_address' && $this->def["relationship_role_column_value"] == '1') {
         if (empty($table)) {
             $roleCheck .= " AND bean_module";
         } else {
             $roleCheck .= " AND {$table}.bean_module";
         }
         $roleCheck .= " = '" . $this->getLHSModule() . "'";
     }
     return $roleCheck;
 }
 /**
  * @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())
 {
     $dataToInsert = $this->getRowToInsert($lhs, $rhs, $additionalFields);
     //If the current data matches the existing data, don't do anything
     if (!$this->checkExisting($dataToInsert)) {
         // Pre-load the RHS relationship, which is used later in the add() function and expects a Bean
         // and we also use it for clearing relationships in case of non self-referencing O2M relations
         // (should be preloaded because when using the relate_to field for updating/saving relationships,
         // only the bean id is loaded into $rhs->$rhsLinkName)
         $rhsLinkName = $this->rhsLink;
         $rhs->load_relationship($rhsLinkName);
         // If it's a One2Many self-referencing relationship
         // the positions of the default One (LHS) and Many (RHS) are swaped
         // so we should clear the links from the many (left) side
         if ($this->selfReferencing) {
             // Load right hand side relationship name
             $linkName = $this->rhsLink;
             // Load the relationship into the left hand side bean
             $lhs->load_relationship($linkName);
             // Pick the loaded link
             $link = $lhs->{$linkName};
             // Get many (LHS) side bean
             $focus = $link->getFocus();
             // Get relations
             $related = $link->getBeans();
             // Clear the relations from many side bean
             foreach ($related as $relBean) {
                 $this->remove($focus, $relBean);
             }
         } else {
             // For non self-referencing, remove all the relationships from the many (RHS) side
             $this->removeAll($rhs->{$rhsLinkName});
         }
         // Add relationship
         parent::add($lhs, $rhs, $additionalFields);
     }
 }
 /**
  * @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())
 {
     $dataToInsert = $this->getRowToInsert($lhs, $rhs, $additionalFields);
     //If the current data matches the existing data, don't do anything
     if (!$this->checkExisting($dataToInsert)) {
         // Pre-load the RHS relationship, which is used later in the add() function and expects a Bean
         // and we also use it for clearing relationships in case of non self-referencing O2M relations
         // (should be preloaded because when using the relate_to field for updating/saving relationships,
         // only the bean id is loaded into $rhs->$rhsLinkName)
         $rhsLinkName = $this->rhsLink;
         $rhs->load_relationship($rhsLinkName);
         // For self-referencing from 6.5.x
         // The left side is one, and right side is many
         if ($this->isRHSMany()) {
             $lhsLinkName = $this->lhsLink;
             $lhs->load_relationship($lhsLinkName);
             $this->removeAll($lhs->{$lhsLinkName});
         } else {
             // For non self-referencing, remove all the relationships from the many (RHS) side
             $this->removeAll($rhs->{$rhsLinkName});
         }
         // Add relationship
         parent::add($lhs, $rhs, $additionalFields);
     }
 }
 protected function buildSugarQueryRoleWhere($sugar_query, $table = "", $ignore_role_filter = false)
 {
     $sugar_query = parent::buildSugarQueryRoleWhere($sugar_query, $table, $ignore_role_filter);
     $sugar_query->join[$table]->on()->equals($table . '.' . $this->userField, $GLOBALS['current_user']->id);
     return $sugar_query;
 }