Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom type ignored by identity generator #5684

Closed
bharley opened this issue Feb 27, 2016 · 3 comments
Closed

Custom type ignored by identity generator #5684

bharley opened this issue Feb 27, 2016 · 3 comments
Assignees
Labels
Milestone

Comments

@bharley
Copy link

bharley commented Feb 27, 2016

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.

@OJezu
Copy link

OJezu commented 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.

@mantiz
Copy link
Contributor

mantiz commented 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:

$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. 😉

@Ocramius
Copy link
Member

This was handled in #6152

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants