Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Nov 14, 2023
1 parent 4bbafdd commit 26b63c1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
20 changes: 19 additions & 1 deletion webfiori/database/FK.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @author Ibrahim
*/
class FK extends webfiori\database\ForeignKey {
class FK extends ForeignKey {
/**
* An action that can be performed on update or delete.
*
Expand Down Expand Up @@ -50,6 +50,24 @@ class FK extends webfiori\database\ForeignKey {
* @var string
*/
const RESTRICT = 'restrict';
/**
* Creates new foreign key.
*
* @param string $name The name of the key. It must be a string, and it's not empty.
* Also, it must not contain any spaces or any characters other than A-Z, a-z and
* underscore. The default value is 'key_name'.
*
* @param Table $ownerTable The table that will contain the key.
*
* @param Table $sourceTable The name of the table that contains the
* original values.
*
* @param array $cols An associative array that contains the names of key
* columns. The indices must be columns in the owner table and the values are
* columns in the source table.
*
* @throws DatabaseException If one of the tables of the foreign key is not set.
*/
public function __construct(string $name = 'key_name', Table $ownerTable = null, Table $sourceTable = null, array $cols = []) {
parent::__construct($name, $ownerTable, $sourceTable, $cols);
}
Expand Down
9 changes: 5 additions & 4 deletions webfiori/database/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ public function addReferenceFromArray(string $colName, array $keyProps) : Table
return $this;

Check warning on line 172 in webfiori/database/Table.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/Table.php#L172

Added line #L172 was not covered by tests
}
$table = $this->getRefTable($keyProps['table']);
$keyName = isset($keyProps['name']) ?? '';
$col = isset($keyProps['col']);
$onUpdate = isset($keyProps['on-update']) ?? FK::SET_NULL;
$onDelete = isset($keyProps['on-delete']) ?? FK::SET_NULL;
$keyName = $keyProps['name'] ?? '';
$col = $keyProps['col'] ?? '';
$onUpdate = $keyProps['on-update'] ?? FK::SET_NULL;
$onDelete = $keyProps['on-delete'] ?? FK::SET_NULL;

return $this->addReference($table, [$colName => $col], $keyName, $onUpdate, $onDelete);
}
Expand Down Expand Up @@ -249,6 +249,7 @@ private function getRefTable($refTable) {
}
}
}
return $refTable;
}
/**
* Returns a column given its index.
Expand Down
14 changes: 11 additions & 3 deletions webfiori/database/mssql/MSSQLTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public function addColumn(string $key, Column $colObj) : bool {
*/
public function addColumns(array $colsArr) : Table {
$arrToAdd = [];

$fksArr = [];

foreach ($colsArr as $key => $arrOrObj) {
if ($arrOrObj instanceof MSSQLColumn) {
$arrToAdd[$key] = $arrOrObj;
Expand All @@ -111,14 +112,21 @@ public function addColumns(array $colsArr) : Table {
$arrToAdd[$key] = $colObj;

if (isset($arrOrObj['fk'])) {
$this->addReferenceFromArray($key, $arrOrObj['fk']);
$fksArr[$key] = $arrOrObj['fk'];

}
}
}
}
}

return parent::addColumns($arrToAdd);
parent::addColumns($arrToAdd);

foreach ($fksArr as $col => $fkArr) {
$this->addReferenceFromArray($col, $fkArr);
}

return $this;
}
/**
* Returns a string which can be used to add table comment as extended
Expand Down
13 changes: 10 additions & 3 deletions webfiori/database/mysql/MySQLTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public function addColumn(string $key, Column $colObj) : bool {
*/
public function addColumns(array $colsArr) : Table {
$arrToAdd = [];

$fksArr = [];

foreach ($colsArr as $key => $arrOrObj) {
if ($arrOrObj instanceof MySQLColumn) {
$arrToAdd[$key] = $arrOrObj;
Expand All @@ -122,14 +123,20 @@ public function addColumns(array $colsArr) : Table {
if ($colObj instanceof MySQLColumn) {
$arrToAdd[$key] = $colObj;
if (isset($colsArr['fk'])) {
$this->addReferenceFromArray($key, $arrOrObj['fk']);
$fksArr[$key] = $arrOrObj['fk'];
}
}
}
}
}

return parent::addColumns($arrToAdd);
parent::addColumns($arrToAdd);

foreach ($fksArr as $col => $fkArr) {
$this->addReferenceFromArray($col, $fkArr);

Check warning on line 136 in webfiori/database/mysql/MySQLTable.php

View check run for this annotation

Codecov / codecov/patch

webfiori/database/mysql/MySQLTable.php#L136

Added line #L136 was not covered by tests
}

return $this;
}
/**
* Returns the character set that is used by the table.
Expand Down

0 comments on commit 26b63c1

Please sign in to comment.