Doctrine 2 Custom Types

2019-02-26 08:52发布

问题:

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?