Skip to content
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

Fix/deeper composite fk #230

Merged
merged 4 commits into from
Sep 30, 2020
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
2 changes: 1 addition & 1 deletion src/Utils/AbstractBeanPropertyDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions src/Utils/ObjectBeanPropertyDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +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) {
$rows[] = trim($descriptor->getLazySerializeCode($propertyAccess), '[]');
if ($shouldFlatten) {
$rows[] = trim($descriptor->getLazySerializeCode($propertyAccess), '[]');
} else {
$lazySerializeCode = $descriptor->getLazySerializeCode("$propertyAccess->$columnGetterName()");
$rows[] = "'$indexName' => $lazySerializeCode";
}
} 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.');
Expand All @@ -280,6 +288,9 @@ private function getBeanPropertyDescriptor(string $column): AbstractBeanProperty
if ($descriptor instanceof ScalarBeanPropertyDescriptor && $descriptor->getColumnName() === $column) {
return $descriptor;
}
if ($descriptor instanceof ObjectBeanPropertyDescriptor && in_array($column, $descriptor->getForeignKey()->getLocalColumns(), true)) {
return $descriptor;
}
}
throw new TDBMException('PropertyDescriptor for `'.$this->table->getName().'`.`' . $column . '` not found in `' . $this->foreignBeanDescriptor->getTable()->getName() . '`');
}
Expand Down
10 changes: 9 additions & 1 deletion tests/TDBMAbstractServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/TDBMDaoGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

Expand Down