diff --git a/src/Propel/Generator/Builder/Om/ObjectBuilder.php b/src/Propel/Generator/Builder/Om/ObjectBuilder.php index ec0fe5365..38578ae8e 100644 --- a/src/Propel/Generator/Builder/Om/ObjectBuilder.php +++ b/src/Propel/Generator/Builder/Om/ObjectBuilder.php @@ -1723,7 +1723,7 @@ protected function addLazyLoaderBody(string &$script, Column $column): void if (is_resource(\$firstColumn)) { \$firstColumn = stream_get_contents(\$firstColumn); } - \$this->$clo = (\$firstColumn) ? UuidConverter::binToUuid(\$firstColumn, $uuidSwapFlag) : null;"; + \$this->$clo = UuidConverter::binToUuid(\$firstColumn, $uuidSwapFlag);"; } else { $script .= " \$this->$clo = \$firstColumn;"; @@ -2737,7 +2737,7 @@ protected function addHydrateBody(string &$script): void if (is_resource(\$col)) { \$col = stream_get_contents(\$col); } - \$this->$clo = (\$col) ? UuidConverter::binToUuid(\$col, $uuidSwapFlag) : null;"; + \$this->$clo = UuidConverter::binToUuid(\$col, $uuidSwapFlag);"; } elseif ($col->isPhpPrimitiveType()) { $script .= " \$this->$clo = (null !== \$col) ? (" . $col->getPhpType() . ') $col : null;'; @@ -2882,10 +2882,15 @@ protected function addBuildPkeyCriteriaBody(string &$script): void $script .= " \$criteria = " . $this->getQueryClassName() . '::create();'; - foreach ($this->getTable()->getPrimaryKey() as $col) { - $clo = $col->getLowercasedName(); + foreach ($this->getTable()->getPrimaryKey() as $column) { + $dataAccessExpression = '$this->' . $column->getLowercasedName(); + if ($column->getType() === PropelTypes::UUID_BINARY) { + $uuidSwapFlag = $this->getUuidSwapFlagLiteral(); + $dataAccessExpression = "UuidConverter::uuidToBin($dataAccessExpression, $uuidSwapFlag)"; + } + $columnConstant = $this->getColumnConstant($column); $script .= " - \$criteria->add(" . $this->getColumnConstant($col) . ", \$this->$clo);"; + \$criteria->add($columnConstant, $dataAccessExpression);"; } } @@ -6689,7 +6694,7 @@ protected function getAccessValueStatement(Column $column): string if ($column->isUuidBinaryType()) { $uuidSwapFlag = $this->getUuidSwapFlagLiteral(); - return "(\$this->$columnName) ? UuidConverter::uuidToBin(\$this->$columnName, $uuidSwapFlag) : null"; + return "UuidConverter::uuidToBin(\$this->$columnName, $uuidSwapFlag)"; } return "\$this->$columnName"; diff --git a/src/Propel/Runtime/Util/UuidConverter.php b/src/Propel/Runtime/Util/UuidConverter.php index cb46d0bc2..cb059ac62 100644 --- a/src/Propel/Runtime/Util/UuidConverter.php +++ b/src/Propel/Runtime/Util/UuidConverter.php @@ -16,13 +16,16 @@ class UuidConverter /** * Transforms a UUID string to a binary string. * - * @param string $uuid + * @param string|null $uuid * @param bool $swapFlag Swap first four bytes for better indexing of version-1 UUIDs (@link https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_uuid-to-bin) * - * @return string + * @return string|null */ - public static function uuidToBin(string $uuid, bool $swapFlag = true): string + public static function uuidToBin(?string $uuid, bool $swapFlag = true): ?string { + if (!$uuid) { + return null; + } $rawHex = (!$swapFlag) ? str_replace('-', '', $uuid) : preg_replace( @@ -37,13 +40,16 @@ public static function uuidToBin(string $uuid, bool $swapFlag = true): string /** * Transforms a binary string to a UUID string. * - * @param string $bin + * @param string|null $bin * @param bool $swapFlag Assume bytes were swapped (@link https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_bin-to-uuid) * - * @return string + * @return string|null */ - public static function binToUuid(string $bin, bool $swapFlag = true): string + public static function binToUuid(?string $bin, bool $swapFlag = true): ?string { + if (!$bin) { + return null; + } $rawHex = bin2hex($bin); $recombineFormat = $swapFlag ? '$3$4-$2-$1-$5-$6' : '$1$2-$3-$4-$5-$6'; diff --git a/tests/Fixtures/bookstore/schema.xml b/tests/Fixtures/bookstore/schema.xml index e9798e939..411eec8d3 100644 --- a/tests/Fixtures/bookstore/schema.xml +++ b/tests/Fixtures/bookstore/schema.xml @@ -354,6 +354,15 @@ + +