Esempio n. 1
0
 /**
  * @param string    $_rel_name   use this relationship key.
  * @param SugarBean $_bean       reference of the bean that instantiated this class.
  * @param array     $fieldDef    vardef entry for the field.
  * @param string    $_table_name optional, fetch from the bean's table name property.
  * @param string    $_key_name   optional, name of the primary key column for _table_name
  */
 public function Link($_rel_name, SugarBean &$_bean, $fieldDef, $_table_name = '', $_key_name = '')
 {
     global $dictionary;
     require_once DOCROOT . "modules/TableDictionary.php";
     Log::debug("Link Constructor, relationship name: [{$_rel_name}], Table name [{$_table_name}], Key name [{$_key_name}]");
     $this->_relationship_name = $_rel_name;
     $this->relationship_fields = !empty($fieldDef['rel_fields']) ? $fieldDef['rel_fields'] : [];
     $this->_bean =& $_bean;
     $this->_relationship = new Relationship();
     //$this->_relationship->retrieve_by_string_fields(array('relationship_name'=>$this->_relationship_name));
     $this->_relationship->retrieve_by_name($this->_relationship_name);
     $this->_db = DBManagerFactory::getInstance();
     // Following behavior is tied to a property(ignore_role) value in the vardef.
     // It alters the values of 2 properties, ignore_role_filter and add_distinct.
     // the property values can be altered again before any requests are made.
     if (!empty($fieldDef) && is_array($fieldDef)) {
         if (array_key_exists('ignore_role', $fieldDef)) {
             if ($fieldDef['ignore_role'] == true) {
                 $this->ignore_role_filter = true;
                 $this->add_distinct = true;
             }
         }
     }
     $this->_bean_table_name = !empty($_table_name) ? $_table_name : $_bean->getTableName();
     if (!empty($key_name)) {
         $this->_bean_key_name = $_key_name;
     } else {
         if ($this->_relationship->lhs_table != $this->_relationship->rhs_table) {
             if ($_bean->table_name == $this->_relationship->lhs_table) {
                 $this->_bean_key_name = $this->_relationship->lhs_key;
             }
             if ($_bean->table_name == $this->_relationship->rhs_table) {
                 $this->_bean_key_name = $this->_relationship->rhs_key;
             }
         }
     }
     if ($this->_relationship->lhs_table == $this->_relationship->rhs_table && isset($fieldDef['side']) && $fieldDef['side'] == 'right') {
         $this->_swap_sides = true;
     }
     if (!empty($fieldDef['rhs_key_override'])) {
         $this->_rhs_key_override = true;
     }
     if (!empty($fieldDef['bean_filter_field'])) {
         $this->_bean_filter_field = $fieldDef['bean_filter_field'];
     }
     //default to id if not set.
     if (empty($this->_bean_key_name)) {
         $this->_bean_key_name = 'id';
     }
     Log::debug("Link Constructor, _bean_table_name: [{$this->_bean_table_name}], _bean_key_name: [{$this->_bean_key_name}]");
     if (!empty($this->_relationship->id)) {
         Log::debug("Link Constructor, relationship record found.");
     } else {
         Log::debug("Link Constructor, No relationship record.");
     }
 }
 public function testretrieve_by_name()
 {
     //unset and reconnect Db to resolve mysqli fetch exeception
     global $db;
     unset($db->database);
     $db->checkConnection();
     $relationship = new Relationship();
     //test with invalid relationship
     $result = $relationship->retrieve_by_name('test_test');
     $this->assertEquals(false, $result);
     //test with invalid relationship
     unset($result);
     $result = $relationship->retrieve_by_name('roles_users');
     $this->assertEquals(null, $result);
     $this->assertEquals('Users', $relationship->rhs_module);
     $this->assertEquals('Roles', $relationship->lhs_module);
     $this->assertEquals('id', $relationship->rhs_key);
     $this->assertEquals('id', $relationship->lhs_key);
     $this->assertEquals('many-to-many', $relationship->relationship_type);
 }
 /**
  * Method returns part of where in style table_alias.id IN (...) because we can't join of relation
  *
  * @param array $layout_def definition of a field
  * @param bool $rename_columns unused
  * @return string SQL where part
  */
 public function queryFilterone_of($layout_def, $rename_columns = true)
 {
     $ids = array();
     if (isset($layout_def['link'])) {
         $relation = new Relationship();
         $relation->retrieve_by_name($layout_def['link']);
     }
     $module = isset($layout_def['custom_module']) ? $layout_def['custom_module'] : $layout_def['module'];
     $seed = BeanFactory::getBean($module);
     foreach ($layout_def['input_name0'] as $beanId) {
         if (!empty($relation->lhs_module) && !empty($relation->rhs_module) && $relation->lhs_module == $relation->rhs_module) {
             $filter = array('id');
         } else {
             $filter = array('id', $layout_def['name']);
         }
         $where = $layout_def['id_name'] . "='{$beanId}' ";
         $sql = $seed->create_new_list_query('', $where, $filter, array(), 0, '', false, $seed, true);
         $result = $this->reporter->db->query($sql);
         while ($row = $this->reporter->db->fetchByAssoc($result)) {
             $ids[] = $row['id'];
         }
     }
     $ids = array_unique($ids);
     $layout_def['name'] = 'id';
     return $this->_get_column_select($layout_def) . " IN ('" . implode("', '", $ids) . "')";
 }
 function setup()
 {
     if (!class_exists('Relationship')) {
     }
     $rel = new Relationship();
     if (!empty($this->vardef['relationship'])) {
         $rel->retrieve_by_name($this->vardef['relationship']);
     }
     if ($rel->relationship_type == 'many-to-many') {
         if ($rel->lhs_module == $this->module_dir) {
             $this->related_module = $rel->rhs_module;
             $module_dir = $rel->lhs_module;
         } else {
             if ($rel->rhs_module == $this->module_dir) {
                 $this->related_module = $rel->lhs_module;
                 $module_dir = $rel->rhs_module;
             } else {
                 die("this field has no relationships mapped with this module");
             }
         }
         if ($module_dir != $this->module_dir) {
             die('These modules do not match : ' . $this->module_dir . ' and ' . $module_dir);
         }
         if (isset($GLOBALS['beanList'][$this->module_dir])) {
             $class = $GLOBALS['beanList'][$this->module_dir];
             if (file_exists($GLOBALS['beanFiles'][$class])) {
                 $this->bean = loadBean($this->module_dir);
                 $this->bean->retrieve($_REQUEST['bean_id']);
                 if ($this->bean->load_relationship($this->vardef['name'])) {
                     $this->retrieve_values();
                 } else {
                     die('failed to load the relationship');
                 }
             } else {
                 die('class file do not exist');
             }
         } else {
             die($this->module_dir . ' is not in the beanList.');
         }
     } else {
         die("the relationship is not a many-to-many");
     }
 }
Esempio n. 5
0
 /**
  * Method returns part of where in style table_alias.id IN (...) because we can't join of relation
  *
  * @param array $layout_def definition of a field
  * @param bool $rename_columns unused
  * @return string SQL where part
  */
 public function queryFilterone_of($layout_def, $rename_columns = true)
 {
     $ids = array();
     $relation = new Relationship();
     $relation->retrieve_by_name($layout_def['link']);
     global $beanList;
     $beanClass = $beanList[$relation->lhs_module];
     $seed = new $beanClass();
     foreach ($layout_def['input_name0'] as $beanId) {
         $seed->retrieve($beanId);
         $link = new Link2($layout_def['link'], $seed);
         $sql = $link->getQuery();
         $result = $this->reporter->db->query($sql);
         while ($row = $this->reporter->db->fetchByAssoc($result)) {
             $ids[] = $row['id'];
         }
     }
     $ids = array_unique($ids);
     $layout_def['name'] = 'id';
     return $this->_get_column_select($layout_def) . " IN ('" . implode("', '", $ids) . "')";
 }
Esempio n. 6
0
 /**
  * Get relationships for a specific module
  *
  * @param string $module SugarCRM Module's name
  * @param string $type   Could be either 'all' or 'one'. One will give only the rels as "fields"
  *
  * @throws \InvalidArgumentException
  *
  * @return array List of relationships
  */
 public function getModuleRelationships($module, $type = 'all')
 {
     // Check in cache
     if (isset($this->moduleRels[$module][$type])) {
         $this->getLogger()->debug($this->logPrefix . 'Got rels for this module in cache');
         return $this->moduleRels[$module][$type];
     }
     $sugarBean = $this->getBean($module);
     $data = array();
     $rels = $sugarBean->get_linked_fields();
     // removing the "right side" of a relationship
     foreach ($rels as $props) {
         $relName = $props['relationship'];
         $relationship = new \Relationship();
         $relationship->retrieve_by_name($relName);
         // Id is empty: fake relationship, especially for users
         if (empty($relationship->id)) {
             continue;
         }
         // Just rename the relationship if I ask only the beans directly related
         if ($type == 'one' && (array_key_exists('side', $props) && $props['side'] == 'right' || array_key_exists('link_type', $props) && $props['link_type'] == 'one')) {
             $relName = $props['name'];
         }
         // I am in a many to One, ignore other
         if ($type == 'one' && $relName == $relationship->relationship_name) {
             continue;
         }
         // Write data
         $data[$relName] = array('relationship_name' => $relName, 'lhs_key' => $relationship->lhs_key, 'lhs_module' => $relationship->lhs_module, 'relationship_type' => $relationship->relationship_type, 'rhs_module' => $relationship->rhs_module, 'rhs_key' => $relationship->rhs_key, 'join_table' => $relationship->join_table, 'join_key_lhs' => $relationship->join_key_lhs, 'join_key_rhs' => $relationship->join_key_rhs);
     }
     // sort it
     ksort($data);
     // cache it
     $this->moduleRels[$module][$type] = $data;
     return $data;
 }