Skip to content

Commit

Permalink
Merge pull request #5 from mringler/bugfix/fix_inheritance_classkey_type
Browse files Browse the repository at this point in the history
Fix single table inheritance classkey type
  • Loading branch information
mringler authored Nov 28, 2023
2 parents 32161c1 + 6e73cf7 commit d4ad8c4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
12 changes: 9 additions & 3 deletions src/Propel/Generator/Builder/Om/TableMapBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Propel\Generator\Model\ForeignKey;
use Propel\Generator\Model\IdMethod;
use Propel\Generator\Model\PropelTypes;
use Propel\Generator\Platform\PlatformInterface;

/**
Expand Down Expand Up @@ -348,26 +349,31 @@ public function addInheritanceColumnConstants(string &$script): void
return;
}

$isNumericKey = $col->isNumericType() && $col->getType() !== PropelTypes::DECIMAL;

foreach ($col->getChildren() as $child) {
$childBuilder = $this->getMultiExtendObjectBuilder();
$childBuilder->setChild($child);
$fqcn = addslashes($childBuilder->getFullyQualifiedClassName());

$suffix = $child->getConstantSuffix();
$key = $isNumericKey ? $child->getKey() : "'" . $child->getKey() . "'";
$script .= "
/** A key representing a particular subclass */
public const CLASSKEY_" . $child->getConstantSuffix() . " = '" . $child->getKey() . "';
public const CLASSKEY_{$suffix} = $key;
";

if (strtoupper($child->getClassName()) != $child->getConstantSuffix()) {
$childClassLiteral = strtoupper($child->getClassname());
$script .= "
/** A key representing a particular subclass */
public const CLASSKEY_" . strtoupper($child->getClassname()) . " = '" . $fqcn . "';
public const CLASSKEY_{$childClassLiteral} = '$fqcn';
";
}

$script .= "
/** A class that can be returned by this tableMap. */
public const CLASSNAME_" . $child->getConstantSuffix() . " = '" . $fqcn . "';
public const CLASSNAME_{$suffix} = '$fqcn';
";
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Propel/Generator/Model/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class Column extends MappingModel
private $isEnumeratedClasses = false;

/**
* @var array|null
* @var array<\Propel\Generator\Model\Inheritance>|null
*/
private $inheritanceList;

Expand Down Expand Up @@ -845,7 +845,7 @@ public function getInheritanceType(): ?string
/**
* Returns the inheritance list.
*
* @return array|null
* @return array<\Propel\Generator\Model\Inheritance>|null
*/
public function getInheritanceList(): ?array
{
Expand All @@ -855,11 +855,11 @@ public function getInheritanceList(): ?array
/**
* Returns the inheritance definitions.
*
* @return array|null
* @return array<\Propel\Generator\Model\Inheritance>|null
*/
public function getChildren(): ?array
{
return $this->inheritanceList;
return $this->getInheritanceList();
}

/**
Expand Down
50 changes: 50 additions & 0 deletions tests/Propel/Tests/Generator/Model/InheritanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

namespace Propel\Tests\Generator\Model;

use Propel\Generator\Builder\Om\TableMapBuilder;
use Propel\Generator\Config\QuickGeneratorConfig;
use Propel\Generator\Model\Inheritance;
use Propel\Generator\Util\QuickBuilder;
use Propel\Tests\TestCase;

/**
Expand Down Expand Up @@ -60,4 +63,51 @@ public function testSetupObject()
$this->assertSame('baz', $inheritance->getKey());
$this->assertSame('Foo\Bar', $inheritance->getClassName());
}

public function singleInheritanceTestDataProvider(){
return [
// string $type, $key, string $expectedClasskey
['varchar', 'le_key', "LE_KEY = 'le_key'"],
['enum', 'default', "DEFAULT = 'default'"],
['integer', 4, '4 = 4'],
['smallint', 4, '4 = 4'],
['float', 0.5, "0_5 = 0.5"],
['decimal', '0.33', "0_33 = '0.33'"],
];
}

/**
* @dataProvider singleInheritanceTestDataProvider
* @return void
*/
public function testSingleInheritanceKeyType(string $type, $key, string $expectedClasskey)
{

$databaseXml = <<<XML
<database namespace="SingleTableInheritanceTest">
<table name="Inheriter">
<column
name="type_indicator"
type="$type"
inheritance="single"
>
<inheritance key="$key" class="Inheriter"/>
</column>
</table>
</database>
XML;
$schemaBuilder = new QuickBuilder();
$schemaBuilder->setSchema($databaseXml);
$database = $schemaBuilder->getDatabase();
$table = $database->getTable('Inheriter');
$builder = new TableMapBuilder($table);
$builder->setGeneratorConfig(new QuickGeneratorConfig());
$script = '';
$builder->addInheritanceColumnConstants($script);

$expectedClasskeyDeclaration = "public const CLASSKEY_$expectedClasskey;";
$description = "Inheritance column of type $type should generate $type classkey values";

$this->assertStringContainsString($expectedClasskeyDeclaration, $script, $description);
}
}

0 comments on commit d4ad8c4

Please sign in to comment.