Doctrine 2 Custom Types

I am trying to implement a Doctrine Custom Datatype for saving currency decimals as SQL integer. I can't change the Database Design, so i'll have to this:

<?php

namespace Doctrine\DBAL\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;

class Currency extends Type {

    const CURRENCY = 'currency';
    const DECIMALS = 2;

    public function getName() {
        return self::CURRENCY;
    }

    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) {
        return $platform->getDoctrineTypeMapping('CURRENCY');
    }

    public function convertToPHPValue($value, AbstractPlatform $platform) {
        return ($value / pow(10, self::DECIMALS));
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform) {
        return (int) ($value * pow(10, self::DECIMALS));
    }

}

?>

That works fine if i load my entity with EntityRepository's "find"-method. But it doesn't work if use "UPDATE" DQL statements. What can i do to make this work in DQL statements, too?

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .



# More Articles