diff --git a/webfiori/database/JoinTable.php b/webfiori/database/JoinTable.php index 06d770b..9798254 100644 --- a/webfiori/database/JoinTable.php +++ b/webfiori/database/JoinTable.php @@ -10,8 +10,10 @@ */ namespace webfiori\database; +use webfiori\database\mssql\MSSQLColumn; use webfiori\database\mssql\MSSQLQuery; use webfiori\database\mssql\MSSQLTable; +use webfiori\database\mysql\MySQLColumn; use webfiori\database\mysql\MySQLQuery; use webfiori\database\mysql\MySQLTable; /** @@ -99,6 +101,67 @@ public function addJoinCondition(Condition $cond, string $joinOp = 'and') { $this->joinConditions = $newCond; } } + /** + * Adds multiple columns at once. + * + * @param array $colsArr An associative array. The keys will act as column + * key in the table. The value of the key can be an object of type 'MSSQLColumn' or 'MySQLColumn' + * or be an associative array of column options. The available options + * are: + * + * + * @return Table The method will return the instance at which the method + * is called on. + * + */ + public function addColumns(array $colsArr) : Table { + $arrToAdd = []; + + foreach ($colsArr as $key => $arrOrObj) { + if ($arrOrObj instanceof Column) { + $arrToAdd[$key] = $arrOrObj; + } else { + if (gettype($arrOrObj) == 'array') { + if (!isset($arrOrObj['name'])) { + $arrOrObj['name'] = str_replace('-', '_', $key); + } + if ($this->getLeft() instanceof MSSQLTable) { + $colObj = MSSQLColumn::createColObj($arrOrObj); + } else { + $colObj = MySQLColumn::createColObj($arrOrObj); + } + + if ($colObj instanceof Column) { + $arrToAdd[$key] = $colObj; + } + } + } + } + + return parent::addColumns($arrToAdd); + } /** * Returns a string which represents the join condition of the two tables. * @@ -214,10 +277,10 @@ private function addColsHelper(bool $left = true) { } } private function copyCol(Column $column) { - if ($column instanceof mysql\MySQLColumn) { - $copyCol = new mysql\MySQLColumn($column->getName(), $column->getDatatype(), $column->getSize()); + if ($column instanceof MySQLColumn) { + $copyCol = new MySQLColumn($column->getName(), $column->getDatatype(), $column->getSize()); } else { - $copyCol = new mssql\MSSQLColumn($column->getName(), $column->getDatatype(), $column->getSize()); + $copyCol = new MSSQLColumn($column->getName(), $column->getDatatype(), $column->getSize()); } $copyCol->setOwner($column->getOwner()); $copyCol->setCustomFilter($column->getCustomCleaner());