Skip to content

Commit

Permalink
Merge pull request #87 from WebFiori/dev
Browse files Browse the repository at this point in the history
Fix to Bug in Adding FKs in MySQL
  • Loading branch information
usernane authored Jan 3, 2024
2 parents c32d8c0 + 01d523b commit f32a87a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
12 changes: 6 additions & 6 deletions webfiori/database/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@ public function addColumns(array $cols) : Table {
* @throws DatabaseException
*/
public function addReferenceFromArray(string $colName, array $keyProps) : Table {
if (!isset($keyProps['table'])) {
if (!isset($keyProps[ColOption::FK_TABLE])) {
return $this;
}
$table = $this->getRefTable($keyProps['table']);
$keyName = $keyProps['name'] ?? '';
$col = $keyProps['col'] ?? '';
$onUpdate = $keyProps['on-update'] ?? FK::SET_NULL;
$onDelete = $keyProps['on-delete'] ?? FK::SET_NULL;
$table = $this->getRefTable($keyProps[ColOption::FK_TABLE]);
$keyName = $keyProps[ColOption::FK_NAME] ?? '';
$col = $keyProps[ColOption::FK_COL] ?? '';
$onUpdate = $keyProps[ColOption::FK_ON_UPDATE] ?? FK::SET_NULL;
$onDelete = $keyProps[ColOption::FK_ON_DELETE] ?? FK::SET_NULL;

return $this->addReference($table, [$colName => $col], $keyName, $onUpdate, $onDelete);
}
Expand Down
9 changes: 5 additions & 4 deletions webfiori/database/mssql/MSSQLTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
namespace webfiori\database\mssql;

use webfiori\database\ColOption;
use webfiori\database\Column;
use webfiori\database\Table;
/**
Expand Down Expand Up @@ -103,16 +104,16 @@ public function addColumns(array $colsArr) : Table {
$arrToAdd[$key] = $arrOrObj;
} else {
if (gettype($arrOrObj) == 'array') {
if (!isset($arrOrObj['name'])) {
$arrOrObj['name'] = str_replace('-', '_', $key);
if (!isset($arrOrObj[ColOption::NAME])) {
$arrOrObj[ColOption::NAME] = str_replace('-', '_', $key);
}
$colObj = MSSQLColumn::createColObj($arrOrObj);

if ($colObj instanceof MSSQLColumn) {
$arrToAdd[$key] = $colObj;

if (isset($arrOrObj['fk'])) {
$fksArr[$key] = $arrOrObj['fk'];
if (isset($arrOrObj[ColOption::FK])) {
$fksArr[$key] = $arrOrObj[ColOption::FK];

}
}
Expand Down
9 changes: 5 additions & 4 deletions webfiori/database/mysql/MySQLTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
namespace webfiori\database\mysql;

use webfiori\database\ColOption;
use webfiori\database\Column;
use webfiori\database\Table;
/**
Expand Down Expand Up @@ -115,15 +116,15 @@ public function addColumns(array $colsArr) : Table {
$arrToAdd[$key] = $arrOrObj;
} else {
if (gettype($arrOrObj) == 'array') {
if (!isset($arrOrObj['name'])) {
$arrOrObj['name'] = str_replace('-', '_', $key);
if (!isset($arrOrObj[ColOption::NAME])) {
$arrOrObj[ColOption::NAME] = str_replace('-', '_', $key);
}
$colObj = MySQLColumn::createColObj($arrOrObj);

if ($colObj instanceof MySQLColumn) {
$arrToAdd[$key] = $colObj;
if (isset($colsArr['fk'])) {
$fksArr[$key] = $arrOrObj['fk'];
if (isset($arrOrObj[ColOption::FK])) {
$fksArr[$key] = $arrOrObj[ColOption::FK];
}
}
}
Expand Down

0 comments on commit f32a87a

Please sign in to comment.