From bcbf8bb72a084f59c8285d5bb38737cba8ffed7b Mon Sep 17 00:00:00 2001 From: Florian HENRY Date: Tue, 9 Mar 2021 10:21:32 +0100 Subject: [PATCH 1/2] allow table alias into commonObject getFieldList --- htdocs/core/class/commonobject.class.php | 30 ++++++++++++------- .../template/class/myobject.class.php | 2 +- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index c7dbab57b31..353dbd7da33 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -7467,12 +7467,22 @@ abstract class CommonObject /** * Function to concat keys of fields * + * @param array $alias content informations of field + * * @return string */ - protected function getFieldList() + protected function getFieldList($alias = 't') { $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); + } } /** @@ -7646,13 +7656,13 @@ abstract class CommonObject { if (empty($id) && empty($ref) && empty($morewhere)) return -1; - $sql = 'SELECT '.$this->getFieldList(); - $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element; + $sql = 'SELECT '.$this->getFieldList('t'); + $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t'; - if (!empty($id)) $sql .= ' WHERE rowid = '.$id; - elseif (!empty($ref)) $sql .= " WHERE ref = ".$this->quote($ref, $this->fields['ref']); + if (!empty($id)) $sql .= ' WHERE t.rowid = '.$id; + elseif (!empty($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).')'; + if (empty($id) && isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) $sql .= ' AND t.entity IN ('.getEntity($this->table_element).')'; if ($morewhere) $sql .= $morewhere; $sql .= ' LIMIT 1'; // This is a fetch, to be sure to get only one record @@ -7695,9 +7705,9 @@ abstract class CommonObject $objectline = new $objectlineclassname($this->db); - $sql = 'SELECT '.$objectline->getFieldList(); - $sql .= ' FROM '.MAIN_DB_PREFIX.$objectline->table_element; - $sql .= ' WHERE fk_'.$this->element.' = '.$this->id; + $sql = 'SELECT '.$objectline->getFieldList('l'); + $sql .= ' FROM '.MAIN_DB_PREFIX.$objectline->table_element.' as l'; + $sql .= ' WHERE l.fk_'.$this->element.' = '.$this->id; if ($morewhere) $sql .= $morewhere; $resql = $this->db->query($sql); diff --git a/htdocs/modulebuilder/template/class/myobject.class.php b/htdocs/modulebuilder/template/class/myobject.class.php index fe4cc0931fd..6151a50db58 100644 --- a/htdocs/modulebuilder/template/class/myobject.class.php +++ b/htdocs/modulebuilder/template/class/myobject.class.php @@ -403,7 +403,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).')'; else $sql .= ' WHERE 1 = 1'; From 3cb072f67af69b980f8ef7650462c0039695fbb9 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 9 Mar 2021 15:13:20 +0100 Subject: [PATCH 2/2] Update commonobject.class.php --- htdocs/core/class/commonobject.class.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 42dac9b7d99..c324b2502c9 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -8255,11 +8255,10 @@ abstract class CommonObject /** * Function to concat keys of fields * - * @param array $alias content informations of field - * - * @return string + * @param string $alias String of alias of table for fields. For example 't'. + * @return string list of alias fields */ - protected function getFieldList($alias = 't') + protected function getFieldList($alias = '') { $keys = array_keys($this->fields); if (!empty($alias)) {