8000 only replace '_id' at end of columnName by rharink · Pull Request #7684 · doctrine/orm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

only replace '_id' at end of columnName #7684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Mapping\MappingException;
use function preg_replace;

/**
* The DatabaseDriver reverse engineers the mapping metadata from a database.
Expand Down Expand Up @@ -548,7 +549,7 @@ private function getFieldNameForColumn($tableName, $columnName, $fk = false)

// Replace _id if it is a foreignkey column
if ($fk) {
$columnName = str_replace('_id', '', $columnName);
$columnName = preg_replace('/_id$/', '', $columnName);
}

return Inflector::camelize($columnName);
Expand Down
38 changes: 38 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/GH7684Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\DBAL\Schema\Table;
use Doctrine\Tests\ORM\Functional\DatabaseDriverTestCase;

/**
* Verifies that associations/columns with an inline '_id' get named properly
*
* Github issue: 7684
*/
class GH7684 extends DatabaseDriverTestCase
{
public function testIssue() : void
{
if (! $this->_em->getConnection()->getDatabasePlatform()->supportsForeignKeyConstraints()) {
$this->markTestSkipped('Platform does not support foreign keys.');
}

$table1 = new Table('GH7684_identity_test_table');
$table1->addColumn('id', 'integer');
$table1->setPrimaryKey(['id']);

$table2 = new Table('GH7684_identity_test_assoc_table');
$table2->addColumn('id', 'integer');
$table2->addColumn('gh7684_identity_test_id', 'integer');
$table2->setPrimaryKey(['id']);
$table2->addForeignKeyConstraint('GH7684_identity_test', ['gh7684_identity_test_id'], ['id']);

$metadatas = $this->convertToClassMetadata([$table1, $table2]);
$metadata = $metadatas['Gh7684IdentityTestAssocTable'];

$this->assertArrayHasKey('gh7684IdentityTest', $metadata->associationMappings);
}
}
0