Skip to content

Commit d0b598e

Browse files
committed
JSON Serialize Stop Recursion: Composite Fk Implementation
1 parent 58b5be8 commit d0b598e

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/Utils/ObjectBeanPropertyDescriptor.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,10 @@ private function getLazySerializeCode(string $propertyAccess): string
245245
{
246246
$rows = [];
247247
foreach ($this->getForeignKey()->getUnquotedForeignColumns() as $column) {
248-
$columnGetterName = 'get' . TDBMDaoGenerator::toCamelCase($column);
249-
$rows[] = "'$column' => $propertyAccess->$columnGetterName()";
248+
$camelColumn = TDBMDaoGenerator::toCamelCase($column);
249+
$indexName = lcfirst($camelColumn);
250+
$columnGetterName = 'get' . $camelColumn;
251+
$rows[] = "'$indexName' => $propertyAccess->$columnGetterName()";
250252
}
251253
return '[' . implode(', ', $rows) . ']';
252254
}

tests/TDBMAbstractServiceTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,17 @@ private static function initSchema(Connection $connection): void
451451
->column('id')->integer()->primaryKey()->autoIncrement()
452452
->column('base_object_id')->references('base_objects')->unique()->comment('@JsonCollection');
453453

454+
$targetTable = $db->table('composite_fk_target')
455+
->column('id_1')->integer()
456+
->column('id_2')->integer()
457+
->then()->primaryKey(['id_1', 'id_2']);
458+
$db->table('composite_fk_source')
459+
->column('id')->integer()->primaryKey()->autoIncrement()
460+
->column('fk_1')->integer()
461+
->column('fk_2')->integer()
462+
->then()->addForeignKeyConstraint($targetTable, ['fk_1', 'fk_2'], ['id_1', 'id_2']);
463+
464+
454465
$sqlStmts = $toSchema->getMigrateFromSql($fromSchema, $connection->getDatabasePlatform());
455466

456467
foreach ($sqlStmts as $sqlStmt) {
@@ -771,6 +782,15 @@ private static function initSchema(Connection $connection): void
771782
'person_id' => 1,
772783
'boat_id' => 1,
773784
]);
785+
self::insert($connection, 'composite_fk_target', [
786+
'id_1' => 1,
787+
'id_2' => 1
788+
]);
789+
self::insert($connection, 'composite_fk_source', [
790+
'id' => 1,
791+
'fk_1' => 1,
792+
'fk_2' => 1
793+
]);
774794
}
775795

776796
protected static function insert(Connection $connection, string $tableName, array $data): void

tests/TDBMDaoGeneratorTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
use TheCodingMachine\TDBM\Test\Dao\BoatDao;
6969
use TheCodingMachine\TDBM\Test\Dao\CatDao;
7070
use TheCodingMachine\TDBM\Test\Dao\CategoryDao;
71+
use TheCodingMachine\TDBM\Test\Dao\CompositeFkSourceDao;
7172
use TheCodingMachine\TDBM\Test\Dao\ContactDao;
7273
use TheCodingMachine\TDBM\Test\Dao\CountryDao;
7374
use TheCodingMachine\TDBM\Test\Dao\DogDao;
@@ -2149,4 +2150,13 @@ public function testLazyStopRecursion(): void
21492150
$this->assertArrayHasKey('id', $json['artist']);
21502151
$this->assertArrayNotHasKey('name', $json['artist']);
21512152
}
2153+
2154+
public function testLazyStopRecursionOnCompositeForeignKey(): void
2155+
{
2156+
$compositeFkSourceDao = new CompositeFkSourceDao($this->tdbmService);
2157+
$compositeFkSourceBean = $compositeFkSourceDao->getById(1);
2158+
$json = $compositeFkSourceBean->jsonSerialize(true);
2159+
$this->assertEquals(1, $json['compositeFkTarget']['id1']);
2160+
$this->assertEquals(1, $json['compositeFkTarget']['id2']);
2161+
}
21522162
}

0 commit comments

Comments
 (0)