Skip to content

Commit d5625f3

Browse files
committed
Inheritance: handle different pk column name
1 parent e8bafbf commit d5625f3

File tree

4 files changed

+57
-10
lines changed

4 files changed

+57
-10
lines changed

src/Utils/BeanDescriptor.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public function getPropertiesWithDefault(): array
230230
public function getExposedProperties(): array
231231
{
232232
$exposedProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) {
233-
return $property->getTable()->getName() == $this->table->getName();
233+
return !$property instanceof ScalarReferencePropertyDescriptor && $property->getTable()->getName() === $this->table->getName();
234234
});
235235

236236
return $exposedProperties;
@@ -256,7 +256,7 @@ private function getProperties(Table $table): array
256256
$localProperties = $this->getPropertiesForTable($table);
257257
foreach ($localProperties as $name => $property) {
258258
// We do not override properties if this is a primary key!
259-
if ($property->isPrimaryKey()) {
259+
if (!$property instanceof ScalarReferencePropertyDescriptor && $property->isPrimaryKey()) {
260260
continue;
261261
}
262262
$properties[$name] = $property;
@@ -279,14 +279,14 @@ private function getPropertiesForTable(Table $table): array
279279
{
280280
$parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName());
281281
if ($parentRelationship) {
282-
$ignoreColumns = $parentRelationship->getUnquotedLocalColumns();
282+
$ignoreColumns = $parentRelationship->getUnquotedForeignColumns();
283283
} else {
284284
$ignoreColumns = [];
285285
}
286286

287287
$beanPropertyDescriptors = [];
288288
foreach ($table->getColumns() as $column) {
289-
if (array_search($column->getName(), $ignoreColumns) !== false) {
289+
if (in_array($column->getName(), $ignoreColumns, true)) {
290290
continue;
291291
}
292292

@@ -300,11 +300,18 @@ private function getPropertiesForTable(Table $table): array
300300
}
301301
// Check that this property is not an inheritance relationship
302302
$parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName());
303-
if ($parentRelationship === $fk) {
304-
continue;
303+
if ($parentRelationship !== null && $parentRelationship->getName() === $fk->getName()) {
304+
$beanPropertyDescriptors[] = new ScalarReferencePropertyDescriptor(
305+
$table,
306+
$column,
307+
$this->namingStrategy,
308+
$this->annotationParser,
309+
new ScalarBeanPropertyDescriptor($table, $column, $this->namingStrategy, $this->annotationParser)
310+
);
311+
} else {
312+
$beanPropertyDescriptors[] = new ObjectBeanPropertyDescriptor($table, $fk, $this->namingStrategy, $this->beanNamespace, $this->annotationParser, $this->registry->getBeanForTableName($fk->getForeignTableName()));;
305313
}
306314

307-
$beanPropertyDescriptors[] = new ObjectBeanPropertyDescriptor($table, $fk, $this->namingStrategy, $this->beanNamespace, $this->annotationParser, $this->registry->getBeanForTableName($fk->getForeignTableName()));
308315
} else {
309316
$beanPropertyDescriptors[] = new ScalarBeanPropertyDescriptor($table, $column, $this->namingStrategy, $this->annotationParser);
310317
}

src/Utils/DefaultNamingStrategy.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,11 @@ protected function getUpperCamelCaseName(AbstractBeanPropertyDescriptor $propert
312312
{
313313
if ($property instanceof ObjectBeanPropertyDescriptor) {
314314
return $this->getForeignKeyUpperCamelCaseName($property->getForeignKey(), $property->isAlternativeName());
315-
} elseif ($property instanceof ScalarBeanPropertyDescriptor) {
315+
}
316+
if ($property instanceof ScalarBeanPropertyDescriptor) {
316317
return $this->getScalarColumnUpperCamelCaseName($property->getColumnName(), $property->isAlternativeName());
317-
} else {
318-
throw new TDBMException('Unexpected property type. Should be either ObjectBeanPropertyDescriptor or ScalarBeanPropertyDescriptor'); // @codeCoverageIgnore
319318
}
319+
throw new TDBMException('Unexpected property type. Should be either ObjectBeanPropertyDescriptor or ScalarBeanPropertyDescriptor'); // @codeCoverageIgnore
320320
}
321321

322322
private function getSchema(): Schema

src/Utils/ObjectBeanPropertyDescriptor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ private function getLazySerializeCode(string $propertyAccess): string
258258
$rows = [];
259259
foreach ($this->getForeignKey()->getUnquotedForeignColumns() as $column) {
260260
$descriptor = $this->getBeanPropertyDescriptor($column);
261+
if ($descriptor instanceof ScalarReferencePropertyDescriptor) {
262+
$descriptor = $descriptor->getReferencedPropertyDescriptor();
263+
}
261264
$indexName = ltrim($descriptor->getVariableName(), '$');
262265
$columnGetterName = $descriptor->getGetterName();
263266
$rows[] = "'$indexName' => $propertyAccess->$columnGetterName()";
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\TDBM\Utils;
5+
6+
use Doctrine\DBAL\Schema\Column;
7+
use Doctrine\DBAL\Schema\Table;
8+
use TheCodingMachine\TDBM\Utils\Annotation\AnnotationParser;
9+
10+
/**
11+
* This class represents a reference to a AbstractBeanPropertyDescriptor
12+
*/
13+
class ScalarReferencePropertyDescriptor extends ScalarBeanPropertyDescriptor
14+
{
15+
/** @var AbstractBeanPropertyDescriptor */
16+
private $referencedPropertyDescriptor;
17+
18+
public function __construct(
19+
Table $table,
20+
Column $column,
21+
NamingStrategyInterface $namingStrategy,
22+
AnnotationParser $annotationParser,
23+
AbstractBeanPropertyDescriptor $referencedPropertyDescriptor
24+
)
25+
{
26+
parent::__construct($table, $column, $namingStrategy, $annotationParser);
27+
$this->referencedPropertyDescriptor = $referencedPropertyDescriptor;
28+
}
29+
30+
/**
31+
* @return AbstractBeanPropertyDescriptor
32+
*/
33+
public function getReferencedPropertyDescriptor(): AbstractBeanPropertyDescriptor
34+
{
35+
return $this->referencedPropertyDescriptor;
36+
}
37+
}

0 commit comments

Comments
 (0)