From 1475df5fdf47c4bb23973914448dfb588ca7abc3 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Thu, 24 Sep 2020 15:15:47 +0200 Subject: [PATCH 1/4] Deeper Composite FK: Test Case --- tests/TDBMAbstractServiceTest.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/TDBMAbstractServiceTest.php b/tests/TDBMAbstractServiceTest.php index 97608fda..60cdcdfb 100644 --- a/tests/TDBMAbstractServiceTest.php +++ b/tests/TDBMAbstractServiceTest.php @@ -458,8 +458,12 @@ private static function initSchema(Connection $connection): void ->column('id')->integer()->primaryKey()->autoIncrement() ->column('base_object_id')->references('base_objects')->unique()->comment('@JsonCollection'); + $db->table('composite_fk_target_reference') + ->column('id')->integer()->primaryKey()->autoIncrement() + ->column('label')->string(); + $targetTable = $db->table('composite_fk_target') - ->column('id_1')->integer() + ->column('id_1')->references('composite_fk_target_reference') ->column('id_2')->integer() ->then()->primaryKey(['id_1', 'id_2']); $db->table('composite_fk_source') @@ -800,6 +804,10 @@ private static function initSchema(Connection $connection): void 'person_id' => 1, 'boat_id' => 1, ]); + self::insert($connection, 'composite_fk_target_reference', [ + 'id' => 1, + 'label' => 'test' + ]); self::insert($connection, 'composite_fk_target', [ 'id_1' => 1, 'id_2' => 1 From 94e51b0fbe4eaccf98ed605e81abf5803765462b Mon Sep 17 00:00:00 2001 From: Guillaume Date: Thu, 24 Sep 2020 15:16:19 +0200 Subject: [PATCH 2/4] Deeper Composite FK: Implementation --- src/Utils/ObjectBeanPropertyDescriptor.php | 11 ++++++++--- tests/TDBMDaoGeneratorTest.php | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Utils/ObjectBeanPropertyDescriptor.php b/src/Utils/ObjectBeanPropertyDescriptor.php index 8a14be67..55bb0690 100644 --- a/src/Utils/ObjectBeanPropertyDescriptor.php +++ b/src/Utils/ObjectBeanPropertyDescriptor.php @@ -261,11 +261,13 @@ private function getLazySerializeCode(string $propertyAccess): string if ($descriptor instanceof InheritanceReferencePropertyDescriptor) { $descriptor = $descriptor->getNonScalarReferencedPropertyDescriptor(); } + $indexName = ltrim($descriptor->getVariableName(), '$'); + $columnGetterName = $descriptor->getGetterName(); if ($descriptor instanceof ObjectBeanPropertyDescriptor) { - $rows[] = trim($descriptor->getLazySerializeCode($propertyAccess), '[]'); + $varName = '$o' . lcfirst($indexName); + $lazySerializeCode = $descriptor->getLazySerializeCode($varName); + $rows[] = "'$indexName' => ($varName = $propertyAccess->$columnGetterName()) ? $lazySerializeCode : null"; } elseif ($descriptor instanceof ScalarBeanPropertyDescriptor) { - $indexName = ltrim($descriptor->getVariableName(), '$'); - $columnGetterName = $descriptor->getGetterName(); $rows[] = "'$indexName' => $propertyAccess->$columnGetterName()"; } else { throw new TDBMException('PropertyDescriptor of class `' . get_class($descriptor) . '` cannot be serialized.'); @@ -277,6 +279,9 @@ private function getLazySerializeCode(string $propertyAccess): string private function getBeanPropertyDescriptor(string $column): AbstractBeanPropertyDescriptor { foreach ($this->foreignBeanDescriptor->getBeanPropertyDescriptors() as $descriptor) { + if ($descriptor instanceof ObjectBeanPropertyDescriptor && in_array($column, $descriptor->getForeignKey()->getLocalColumns(), true)) { + return $descriptor; + } if ($descriptor instanceof ScalarBeanPropertyDescriptor && $descriptor->getColumnName() === $column) { return $descriptor; } diff --git a/tests/TDBMDaoGeneratorTest.php b/tests/TDBMDaoGeneratorTest.php index c4bc19f2..1d18f56c 100644 --- a/tests/TDBMDaoGeneratorTest.php +++ b/tests/TDBMDaoGeneratorTest.php @@ -2165,7 +2165,7 @@ public function testLazyStopRecursionOnCompositeForeignKey(): void $compositeFkSourceDao = new CompositeFkSourceDao($this->tdbmService); $compositeFkSourceBean = $compositeFkSourceDao->getById(1); $json = $compositeFkSourceBean->jsonSerialize(true); - $this->assertEquals(1, $json['compositeFkTarget']['id1']); + $this->assertEquals(1, $json['compositeFkTarget']['1']['id']); $this->assertEquals(1, $json['compositeFkTarget']['id2']); } From bd3f65349e086fcc7e3b4ebf7c8d5c817a8cda9d Mon Sep 17 00:00:00 2001 From: Guillaume Date: Thu, 24 Sep 2020 17:53:58 +0200 Subject: [PATCH 3/4] Deeper Composite FK: Fix flatten in case of inheritance --- src/Utils/AbstractBeanPropertyDescriptor.php | 2 +- src/Utils/ObjectBeanPropertyDescriptor.php | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Utils/AbstractBeanPropertyDescriptor.php b/src/Utils/AbstractBeanPropertyDescriptor.php index c8e559c3..3c949905 100644 --- a/src/Utils/AbstractBeanPropertyDescriptor.php +++ b/src/Utils/AbstractBeanPropertyDescriptor.php @@ -67,7 +67,7 @@ abstract public function getPhpType(): string; */ public function getParamAnnotation(): ParamTag { - return new ParamTag($this->getVariableName(), [ $this->getPhpType() ]); + return new ParamTag($this->getSafeVariableName(), [ $this->getPhpType() ]); } public function getVariableName(): string diff --git a/src/Utils/ObjectBeanPropertyDescriptor.php b/src/Utils/ObjectBeanPropertyDescriptor.php index 55bb0690..f2009564 100644 --- a/src/Utils/ObjectBeanPropertyDescriptor.php +++ b/src/Utils/ObjectBeanPropertyDescriptor.php @@ -258,15 +258,22 @@ private function getLazySerializeCode(string $propertyAccess): string $rows = []; foreach ($this->getForeignKey()->getUnquotedForeignColumns() as $column) { $descriptor = $this->getBeanPropertyDescriptor($column); + $shouldFlatten = false; if ($descriptor instanceof InheritanceReferencePropertyDescriptor) { $descriptor = $descriptor->getNonScalarReferencedPropertyDescriptor(); + $shouldFlatten = true; } + $indexName = ltrim($descriptor->getVariableName(), '$'); $columnGetterName = $descriptor->getGetterName(); if ($descriptor instanceof ObjectBeanPropertyDescriptor) { - $varName = '$o' . lcfirst($indexName); - $lazySerializeCode = $descriptor->getLazySerializeCode($varName); - $rows[] = "'$indexName' => ($varName = $propertyAccess->$columnGetterName()) ? $lazySerializeCode : null"; + if ($shouldFlatten) { + $rows[] = trim($descriptor->getLazySerializeCode($propertyAccess), '[]'); + } else { + $varName = $descriptor->getSafeVariableName(); + $lazySerializeCode = $descriptor->getLazySerializeCode($varName); + $rows[] = "'$indexName' => ($varName = $propertyAccess->$columnGetterName()) ? $lazySerializeCode : null"; + } } elseif ($descriptor instanceof ScalarBeanPropertyDescriptor) { $rows[] = "'$indexName' => $propertyAccess->$columnGetterName()"; } else { @@ -279,10 +286,10 @@ private function getLazySerializeCode(string $propertyAccess): string private function getBeanPropertyDescriptor(string $column): AbstractBeanPropertyDescriptor { foreach ($this->foreignBeanDescriptor->getBeanPropertyDescriptors() as $descriptor) { - if ($descriptor instanceof ObjectBeanPropertyDescriptor && in_array($column, $descriptor->getForeignKey()->getLocalColumns(), true)) { + if ($descriptor instanceof ScalarBeanPropertyDescriptor && $descriptor->getColumnName() === $column) { return $descriptor; } - if ($descriptor instanceof ScalarBeanPropertyDescriptor && $descriptor->getColumnName() === $column) { + if ($descriptor instanceof ObjectBeanPropertyDescriptor && in_array($column, $descriptor->getForeignKey()->getLocalColumns(), true)) { return $descriptor; } } From 9d060fc5f70705b5d604a60c42d72444a79334e0 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Thu, 24 Sep 2020 18:02:23 +0200 Subject: [PATCH 4/4] Deeper Composite FK: Fix ternary --- src/Utils/ObjectBeanPropertyDescriptor.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Utils/ObjectBeanPropertyDescriptor.php b/src/Utils/ObjectBeanPropertyDescriptor.php index f2009564..c5ac455b 100644 --- a/src/Utils/ObjectBeanPropertyDescriptor.php +++ b/src/Utils/ObjectBeanPropertyDescriptor.php @@ -270,9 +270,8 @@ private function getLazySerializeCode(string $propertyAccess): string if ($shouldFlatten) { $rows[] = trim($descriptor->getLazySerializeCode($propertyAccess), '[]'); } else { - $varName = $descriptor->getSafeVariableName(); - $lazySerializeCode = $descriptor->getLazySerializeCode($varName); - $rows[] = "'$indexName' => ($varName = $propertyAccess->$columnGetterName()) ? $lazySerializeCode : null"; + $lazySerializeCode = $descriptor->getLazySerializeCode("$propertyAccess->$columnGetterName()"); + $rows[] = "'$indexName' => $lazySerializeCode"; } } elseif ($descriptor instanceof ScalarBeanPropertyDescriptor) { $rows[] = "'$indexName' => $propertyAccess->$columnGetterName()";