Skip to content
27 changes: 26 additions & 1 deletion src/Propel/Generator/Builder/Om/ObjectBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,13 @@ protected function addHasOnlyDefaultValuesBody(string &$script): void
}
$notEquals = '!==';
$defaultValueString = ColumnCodeProducerFactory::create($col, $this)->getDefaultValueString();
if ($col->isPhpObjectType()) {
if ($col->isPhpBackedEnumType()) {
$assumedClassName = $this->declareClass($col->getPhpType());
$defaultValueString = "$assumedClassName::from($defaultValueString)";
} elseif ($col->isPhpUnitEnumType()) {
$assumedClassName = $this->declareClass($col->getPhpType());
$defaultValueString = "constant($assumedClassName::class . '::' . $defaultValueString)";
} elseif ($col->isPhpObjectType()) {
$assumedClassName = $this->declareClass($col->getPhpType());
$defaultValueString = "new $assumedClassName($defaultValueString)";
}
Expand Down Expand Up @@ -1018,6 +1024,17 @@ protected function addHydrateBody(string &$script): void
$script .= "
\$this->$clo = \$columnValue;
\$this->$cloConverted = null;";
} elseif ($col->isPhpBackedEnumType()) {
$assumedClassName = $this->declareClass($col->getPhpType());
$script .= "
\$this->$clo = (\$columnValue === null) ? null : $assumedClassName::from(\$columnValue);";
} elseif ($col->isPhpUnitEnumType()) {
$assumedClassName = $this->declareClass($col->getPhpType());
$script .= "
if (\$columnValue !== null && !defined($assumedClassName::class . '::' . \$columnValue)) {
throw new \\Propel\\Runtime\\Exception\\PropelException(sprintf('Unknown enum case \"%s\" for %s', \$columnValue, $assumedClassName::class));
}
\$this->$clo = (\$columnValue === null) ? null : constant($assumedClassName::class . '::' . \$columnValue);";
} elseif ($col->isPhpObjectType()) {
$assumedClassName = $this->declareClass($col->getPhpType());
$script .= "
Expand Down Expand Up @@ -2619,6 +2636,14 @@ protected function getAccessValueStatement(Column $column): string
return "UuidConverter::uuidToBin(\$this->$columnName, $uuidSwapFlag)";
}

if ($column->isPhpBackedEnumType()) {
return '$this->' . $columnName . '?->value';
}

if ($column->isPhpUnitEnumType()) {
return '$this->' . $columnName . '?->name';
}

return "\$this->$columnName";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,13 @@ public function getApplyDefaultValueStatement(): string
{
$clo = $this->column->getLowercasedName();
$defaultValue = $this->getDefaultValueString();
if ($this->column->isPhpObjectType()) {
if ($this->column->isPhpBackedEnumType()) {
$assumedClassName = $this->declareClass($this->column->getPhpType());
$defaultValue = "$assumedClassName::from($defaultValue)";
} elseif ($this->column->isPhpUnitEnumType()) {
$assumedClassName = $this->declareClass($this->column->getPhpType());
$defaultValue = "constant($assumedClassName::class . '::' . $defaultValue)";
} elseif ($this->column->isPhpObjectType()) {
$assumedClassName = $this->declareClass($this->column->getPhpType());
$defaultValue = "new $assumedClassName($defaultValue )";
}
Expand Down
24 changes: 24 additions & 0 deletions src/Propel/Generator/Model/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,30 @@ public function isPhpObjectType(): bool
return PropelTypes::isPhpObjectType($this->getPhpType());
}

/**
* Returns whether the column PHP type is a backed enum.
*
* @see PropelTypes::isPhpBackedEnumType()
*
* @return bool
*/
public function isPhpBackedEnumType(): bool
{
return PropelTypes::isPhpBackedEnumType($this->getPhpType());
}

/**
* Returns whether this column's phpType is a UnitEnum (non-backed).
*
* @see PropelTypes::isPhpUnitEnumType()
*
* @return bool
*/
public function isPhpUnitEnumType(): bool
{
return PropelTypes::isPhpUnitEnumType($this->getPhpType());
}

/**
* Returns an instance of PlatformInterface interface.
*
Expand Down
27 changes: 27 additions & 0 deletions src/Propel/Generator/Model/PropelTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

namespace Propel\Generator\Model;

use BackedEnum;
use PDO;
use UnitEnum;
use function in_array;
use function is_subclass_of;
use function strtoupper;

/**
Expand Down Expand Up @@ -718,6 +721,30 @@ public static function isPhpObjectType(string $phpType): bool
return !self::isPhpPrimitiveType($phpType) && !in_array($phpType, ['resource', 'array'], true);
}

/**
* Returns whether a passed-in PHP type is a backed enum.
*
* @param string $phpType
*
* @return bool
*/
public static function isPhpBackedEnumType(string $phpType): bool
{
return is_subclass_of($phpType, BackedEnum::class);
}

/**
* Convenience method to indicate whether a passed-in PHP type is a UnitEnum (non-backed).
*
* @param string $phpType
*
* @return bool
*/
public static function isPhpUnitEnumType(string $phpType): bool
{
return is_subclass_of($phpType, UnitEnum::class) && !is_subclass_of($phpType, BackedEnum::class);
}

/**
* Convenience method to indicate whether a passed-in PHP type is an array.
*
Expand Down
Loading
Loading