Skip to content

Commit badd068

Browse files
committed
One To One inverse index test, Prefer unique index over non unique one
1 parent 3633fe0 commit badd068

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/Utils/BeanDescriptor.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,11 @@ private function removeDuplicateIndexes(array $indexes): array
11191119
{
11201120
$indexesByKey = [];
11211121
foreach ($indexes as $index) {
1122-
$indexesByKey[implode('__`__', $index->getUnquotedColumns())] = $index;
1122+
$key = implode('__`__', $index->getUnquotedColumns());
1123+
// Unique Index have precedence over non unique one
1124+
if (!isset($indexesByKey[$key]) || $index->isUnique()) {
1125+
$indexesByKey[$key] = $index;
1126+
}
11231127
}
11241128

11251129
return array_values($indexesByKey);

tests/TDBMAbstractServiceTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,12 @@ private static function initSchema(Connection $connection): void
437437
->column('track_id')->references('tracks')
438438
->column('artist_id')->references('artists')->comment('@JsonKey("feat") @JsonInclude');
439439

440+
$db->table('object_base')
441+
->column('id')->integer()->primaryKey()->autoIncrement();
442+
$db->table('object_inherited')
443+
->column('id')->integer()->primaryKey()->autoIncrement()
444+
->column('object_base_id')->references('object_base')->unique();
445+
440446
$sqlStmts = $toSchema->getMigrateFromSql($fromSchema, $connection->getDatabasePlatform());
441447

442448
foreach ($sqlStmts as $sqlStmt) {

tests/TDBMDaoGeneratorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
use TheCodingMachine\TDBM\Test\Dao\Bean\Generated\FileBaseBean;
6060
use TheCodingMachine\TDBM\Test\Dao\Bean\Generated\UserBaseBean;
6161
use TheCodingMachine\TDBM\Test\Dao\Bean\NodeBean;
62+
use TheCodingMachine\TDBM\Test\Dao\Bean\ObjectBaseBean;
63+
use TheCodingMachine\TDBM\Test\Dao\Bean\ObjectInheritedBean;
6264
use TheCodingMachine\TDBM\Test\Dao\Bean\PersonBean;
6365
use TheCodingMachine\TDBM\Test\Dao\Bean\RefNoPrimKeyBean;
6466
use TheCodingMachine\TDBM\Test\Dao\Bean\RoleBean;
@@ -74,6 +76,8 @@
7476
use TheCodingMachine\TDBM\Test\Dao\Generated\ContactBaseDao;
7577
use TheCodingMachine\TDBM\Test\Dao\Generated\UserBaseDao;
7678
use TheCodingMachine\TDBM\Test\Dao\NodeDao;
79+
use TheCodingMachine\TDBM\Test\Dao\ObjectBaseDao;
80+
use TheCodingMachine\TDBM\Test\Dao\ObjectInheritedDao;
7781
use TheCodingMachine\TDBM\Test\Dao\RefNoPrimKeyDao;
7882
use TheCodingMachine\TDBM\Test\Dao\RoleDao;
7983
use TheCodingMachine\TDBM\Test\Dao\StateDao;
@@ -2074,4 +2078,16 @@ public function testLazyLoad(): void
20742078
$roleBean->getId();
20752079
$this->assertSame(TDBMObjectStateEnum::STATE_NOT_LOADED, $roleBean->_getDbRows()['roles']->_getStatus());
20762080
}
2081+
2082+
public function testOneToOneInverseRelationGetter(): void
2083+
{
2084+
$objectBaseDao = new ObjectBaseDao($this->tdbmService);
2085+
$objectInheritedDao = new ObjectInheritedDao($this->tdbmService);
2086+
$objectBase = new ObjectBaseBean();
2087+
$objectBaseDao->save($objectBase);
2088+
$this->assertNull($objectBase->getObjectInherited());
2089+
$objectInherited = new ObjectInheritedBean($objectBase);
2090+
$objectInheritedDao->save($objectInherited);
2091+
$this->assertInstanceOf(ObjectInheritedBean::class, $objectBase->getObjectInherited());
2092+
}
20772093
}

0 commit comments

Comments
 (0)