Skip to content

Commit

Permalink
Merge pull request #581 from nextras/mysql-foregin-keys-case-insenstive
Browse files Browse the repository at this point in the history
conventions: fix ManyHasMany keys lookup on MySql with different key casing
  • Loading branch information
hrach committed Jun 19, 2022
2 parents 948436e + a923231 commit d39fb16
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/Mapper/Dbal/Conventions/Conventions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Nextras\Dbal\Bridges\NetteCaching\CachedPlatform;
use Nextras\Dbal\IConnection;
use Nextras\Dbal\Platforms\Data\Table;
use Nextras\Dbal\Platforms\MySqlPlatform;
use Nextras\Orm\Entity\Embeddable\EmbeddableContainer;
use Nextras\Orm\Entity\Reflection\EntityMetadata;
use Nextras\Orm\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -324,14 +325,24 @@ protected function findManyHasManyPrimaryColumns(string $joinTable, Table $targe
$targetTable = $targetTableReflection->getNameFqn();
$sourceId = $targetId = null;

$isCaseSensitive = $this->platform->getName() !== MySqlPlatform::NAME;

$keys = $this->platform->getForeignKeys($joinTable);
foreach ($keys as $column => $meta) {
$refTable = $meta->getRefTableFqn();

if ($refTable === $sourceTable && $sourceId === null) {
$sourceId = $column;
} elseif ($refTable === $targetTable) {
$targetId = $column;
if ($isCaseSensitive) {
if ($refTable === $sourceTable && $sourceId === null) {
$sourceId = $column;
} elseif ($refTable === $targetTable) {
$targetId = $column;
}
} else {
if (strcasecmp($refTable, $sourceTable) === 0 && $sourceId === null) {
$sourceId = $column;
} elseif (strcasecmp($refTable, $targetTable) === 0) {
$targetId = $column;
}
}
}

Expand Down

0 comments on commit d39fb16

Please sign in to comment.