Custom type ignored by identity generator #5026

Closed
opened 2026-01-22 14:56:52 +01:00 by admin · 3 comments
Owner

Originally created by @bharley on GitHub (Feb 27, 2016).

Originally assigned to: @Ocramius on GitHub.

I have a custom type which extends the IntegerType. It encodes the integer on the way out of the database, and decodes it on the way in with the use of convertToPHPValue/convertToDatabaseValue. I tried using it like so:

<?php

namespace App\Entities;

use Doctrine\ORM\Mapping AS ORM;

class Location
{
    /**
     * @var string
     * @ORM\Id
     * @ORM\Column(type="hash", nullable=false)
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    // ...
}

The problem with this implementation is that the generated value returned from the database after a persist/flush operation is the integer stored in the database; not the value as it would be had it passed through the convertToPHPValue method of my custom type.

Originally created by @bharley on GitHub (Feb 27, 2016). Originally assigned to: @Ocramius on GitHub. I have a custom type which extends the `IntegerType`. It encodes the integer on the way out of the database, and decodes it on the way in with the use of `convertToPHPValue`/`convertToDatabaseValue`. I tried using it like so: ``` php <?php namespace App\Entities; use Doctrine\ORM\Mapping AS ORM; class Location { /** * @var string * @ORM\Id * @ORM\Column(type="hash", nullable=false) * @ORM\GeneratedValue(strategy="IDENTITY") */ protected $id; // ... } ``` The problem with this implementation is that the generated value returned from the database after a `persist`/`flush` operation is the integer stored in the database; not the value as it would be had it passed through the `convertToPHPValue` method of my custom type.
admin added the Bug label 2026-01-22 14:56:52 +01:00
admin closed this issue 2026-01-22 14:56:53 +01:00
Author
Owner

@OJezu commented on GitHub (Apr 26, 2016):

I would like to add, that even if you try to create your own generator that will convert the value, well too bad, because doctrine is hardcoded to add autoincrement only when generated value strategy is identity. If you try to roll your own, its impossible to have autoincrement on your columns.

@OJezu commented on GitHub (Apr 26, 2016): I would like to add, that even if you try to create your own generator that will convert the value, well too bad, because doctrine is hardcoded to add autoincrement only when generated value strategy is identity. If you try to roll your own, its impossible to have autoincrement on your columns.
Author
Owner

@mantiz commented on GitHub (Jul 13, 2016):

I encountered the same problem today. A small debugging session pointed me to 2b47670831/lib/Doctrine/ORM/UnitOfWork.php (L1016) where the value of the id generator gets directly assigned.

I made a few quick changes directly below the linked line and it seems to work well. The changes are:

$platform = $this->em->getConnection()->getDatabasePlatform();
$idType = $this->em->getClassMetadata(get_class($entity))->fieldMappings[$idField]['type'];
$mappedId = Types\Type::getType($idType)->convertToPHPValue($id, $platform);

$class->reflFields[$idField]->setValue($entity, $mappedId);

The Types namespace, of course, was used by use Doctrine\DBAL\Types.

I'm not sure if this has any relevant performance (or other) issues.

If someone could confirm that these changes are reasonable, I would be happy to submit a pull request within the next days. Just let me know. 😉

@mantiz commented on GitHub (Jul 13, 2016): I encountered the same problem today. A small debugging session pointed me to https://github.com/doctrine/doctrine2/blob/2b476708311b6003881a0cb944ca8c1bf996d58a/lib/Doctrine/ORM/UnitOfWork.php#L1016 where the value of the id generator gets directly assigned. I made a few quick changes directly below the linked line and it seems to work well. The changes are: ``` php $platform = $this->em->getConnection()->getDatabasePlatform(); $idType = $this->em->getClassMetadata(get_class($entity))->fieldMappings[$idField]['type']; $mappedId = Types\Type::getType($idType)->convertToPHPValue($id, $platform); $class->reflFields[$idField]->setValue($entity, $mappedId); ``` The `Types` namespace, of course, was used by `use Doctrine\DBAL\Types`. I'm not sure if this has any relevant performance (or other) issues. If someone could confirm that these changes are reasonable, I would be happy to submit a pull request within the next days. Just let me know. :wink:
Author
Owner

@Ocramius commented on GitHub (Nov 27, 2016):

This was handled in #6152

@Ocramius commented on GitHub (Nov 27, 2016): This was handled in #6152
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5026