From 5c091cd4af1dc3b2b5a6222df693e384ed0cf8e8 Mon Sep 17 00:00:00 2001 From: dsavina Date: Tue, 20 Apr 2021 18:26:15 +0200 Subject: [PATCH] [hotfix] Use column name provided by FK instead of defaulting to first (#254) Signed-off-by: Dorian Savina --- src/DbRow.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/DbRow.php b/src/DbRow.php index 84d2780e..e92887f0 100644 --- a/src/DbRow.php +++ b/src/DbRow.php @@ -405,15 +405,16 @@ private function buildDbRow(array $dbRow, array $references): array if ($reference !== null) { $refDbRows = $reference->_getDbRows(); - $firstRefDbRow = reset($refDbRows); - if ($firstRefDbRow === false) { + $refDbRow = $refDbRows[$fk->getForeignTableName()] ?? false; + + if ($refDbRow === false) { throw new \RuntimeException('Unexpected error: empty refDbRows'); // @codeCoverageIgnore } - if ($firstRefDbRow->_getStatus() === TDBMObjectStateEnum::STATE_DELETED) { + if ($refDbRow->_getStatus() === TDBMObjectStateEnum::STATE_DELETED) { throw TDBMMissingReferenceException::referenceDeleted($this->dbTableName, $reference); } $foreignColumns = $fk->getUnquotedForeignColumns(); - $refBeanValues = $firstRefDbRow->dbRow; + $refBeanValues = $refDbRow->dbRow; for ($i = 0, $count = \count($localColumns); $i < $count; ++$i) { $dbRow[$localColumns[$i]] = $refBeanValues[$foreignColumns[$i]]; }