Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Commit

Permalink
[draft] fix crecord creation workflow when table PK not A.I. field (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
apiraino authored Jul 23, 2020
1 parent ff3398f commit b6e06ea
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/core/Directus/Database/TableGateway/BaseTableGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,26 @@ public function addRecordByArray(array $recordData)
if ($columnObject->hasAutoIncrement()) {
$recordData[$primaryKey] = $TableGateway->getLastInsertValue();
}
else if ($columnObject->hasPrimaryKey())
{
// identify the autoincrement field+value to retrieve the new record
$fieldAutoIncrement = null;
foreach($this->getTableSchema()->getFields() as $f) {
if ($f->hasAutoIncrement()) {
$fieldAutoIncrement = $f->getName();
$lastInsertValue = $TableGateway->getLastInsertValue();
break;
}
}

$columns = SchemaService::getAllNonAliasCollectionFieldNames($this->table);
// retrieve the full record
if (isset($fieldAutoIncrement)) {
$record = $this->findOneBy($fieldAutoIncrement, $lastInsertValue);
$recordData[$primaryKey] = $record[$primaryKey];
}
}

$columns = SchemaService::getAllNonAliasCollectionFieldNames($this->table);
return $TableGateway->fetchAll(function (Select $select) use ($recordData, $columns, $primaryKey) {
$select
->columns($columns)
Expand Down Expand Up @@ -815,6 +832,22 @@ protected function executeInsert(Insert $insert)

$resultData = $insertTableGateway->find($generatedValue);

// try to retrieve the record using another field
if (empty($resultData)) {
$columnObject = $this->getTableSchema()->getField($this->primaryKeyFieldName);
if ($columnObject->hasPrimaryKey()) {
$fieldAutoIncrement = null;
foreach($this->getTableSchema()->getFields() as $f) {
$fieldAutoIncrement = $f->getName();
$lastInsertValue = $this->getLastInsertValue();
break;
}

if (isset($fieldAutoIncrement))
$resultData = $this->findOneBy($fieldAutoIncrement, $lastInsertValue);
}
}

if ($useFilter) {
$this->runHook('item.create', [$insertTable, $resultData]);
$this->runHook('item.create.' . $insertTable, [$resultData]);
Expand Down

0 comments on commit b6e06ea

Please sign in to comment.