diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index f80fcbce957..c324b2502c9 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -8255,12 +8255,21 @@ abstract class CommonObject /** * Function to concat keys of fields * - * @return string + * @param string $alias String of alias of table for fields. For example 't'. + * @return string list of alias fields */ - protected function getFieldList() + protected function getFieldList($alias = '') { $keys = array_keys($this->fields); - return implode(',', $keys); + if (!empty($alias)) { + $keys_with_alias = array(); + foreach ($keys as $fieldname) { + $keys_with_alias[] = $alias . '.' . $fieldname; + } + return implode(',', $keys_with_alias); + } else { + return implode(',', $keys); + } } /** @@ -8462,23 +8471,23 @@ abstract class CommonObject return -1; } - $fieldlist = $this->getFieldList(); + $fieldlist = $this->getFieldList('t'); if (empty($fieldlist)) { return 0; } $sql = 'SELECT '.$fieldlist; - $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element; + $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t'; if (!empty($id)) { - $sql .= ' WHERE rowid = '.$id; + $sql .= ' WHERE t.rowid = '.$id; } elseif (!empty($ref)) { - $sql .= " WHERE ref = ".$this->quote($ref, $this->fields['ref']); + $sql .= " WHERE t.ref = ".$this->quote($ref, $this->fields['ref']); } else { $sql .= ' WHERE 1 = 1'; // usage with empty id and empty ref is very rare } if (empty($id) && isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) { - $sql .= ' AND entity IN ('.getEntity($this->table_element).')'; + $sql .= ' AND t.entity IN ('.getEntity($this->table_element).')'; } if ($morewhere) { $sql .= $morewhere; @@ -8522,9 +8531,9 @@ abstract class CommonObject $objectline = new $objectlineclassname($this->db); - $sql = 'SELECT '.$objectline->getFieldList(); + $sql = 'SELECT '.$objectline->getFieldList('l'); $sql .= ' FROM '.MAIN_DB_PREFIX.$objectline->table_element; - $sql .= ' WHERE fk_'.$this->element.' = '.$this->id; + $sql .= ' WHERE l.fk_'.$this->element.' = '.$this->id; if ($morewhere) { $sql .= $morewhere; } diff --git a/htdocs/modulebuilder/template/class/myobject.class.php b/htdocs/modulebuilder/template/class/myobject.class.php index b4428312018..69d69ba92c0 100644 --- a/htdocs/modulebuilder/template/class/myobject.class.php +++ b/htdocs/modulebuilder/template/class/myobject.class.php @@ -425,7 +425,7 @@ class MyObject extends CommonObject $records = array(); $sql = 'SELECT '; - $sql .= $this->getFieldList(); + $sql .= $this->getFieldList('t'); $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t'; if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) { $sql .= ' WHERE t.entity IN ('.getEntity($this->table_element).')';