How to customize the Doctrine library to work with type Timestamp

-1

I'm using Composer with Doctrine in my application, and it's getting wonderful. However I need to customize some Doctrine features by changing the Doctrine\DBAL\Platforms\AbstractPlatform and Doctrine\DBAL\Platforms\MySqlPlatform classes.

For this I created the MY_AbstractPlatform and MY_MySqlPlatform classes and extended the respective superclasses. So I added them in my autoload, but nothing happened.

When I write the code directly in the Doctrine classes the customization is performed, but when I implement the methods in the subclasses nothing happens.

I need to customize the Doctrine library, but how do I do this?

    
asked by anonymous 07.08.2014 / 15:31

1 answer

0
___ erkimt ___ How to customize the Doctrine library to work with type Timestamp ______ qstntxt ___

I'm using Composer with Doctrine in my application, and it's getting wonderful. However I need to customize some Doctrine features by changing the driverClass and MY_Driver classes.

For this I created the MY_MySqlPlatform and MY_MySqlPlatform classes and extended the respective superclasses. So I added them in my autoload, but nothing happened.

When I write the code directly in the Doctrine classes the customization is performed, but when I implement the methods in the subclasses nothing happens.

I need to customize the Doctrine library, but how do I do this?

    
______ ___ azszpr28308

I was able to do the customization in a modular way. That is, I was able to create the subclasses of Doctrine in my application and use them from the configuration of the connection, setting the MySqlPlatform property of the connection array, instantiating the class of my TimestampType driver.

See how I configured the Doctrine connection, attention to the 'driverClass' property:

$connection_options = array(
            'driver' => 'pdo_mysql',
            'user' => $db['default']['username'],
            'password' => $db['default']['password'],
            'host' => $db['default']['hostname'],
            'dbname' => $db['default']['database'],
            'charset' => $db['default']['char_set'],
            'driverOptions' => array(
                'charset' => $db['default']['char_set'],
            ),
            'driverClass' => new MY_Driver()
        );

As my goal was to allow the Doctrine worked with Fields timetamp , created a class to represent the driver of my application, inheriting the native Driver class Doctrine. My subclass declares another subclass Type containing customizations for MySql platform to apply the timestamp .

class MY_Driver extends Doctrine\DBAL\Driver\Mysqli\Driver {

    /**
     * Define a classe personalizada para o driver da plataforma MySql
     * 
     * {@inheritdoc}
     */
    public function getDatabasePlatform() {

        return new MY_MySqlPlatform();
    }

}

%code% class inherits the class%% %code% native Doctrine, but applying own methods to use the timestamp type.

class MY_MySqlPlatform extends Doctrine\DBAL\Platforms\MySqlPlatform {

    /**
     * {@inheritDoc}
     */
    public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration) {
        if (isset($fieldDeclaration['version']) && $fieldDeclaration['version'] == true) {
            return 'TIMESTAMP';
        }

        return 'TIMESTAMP';
    }

    /**
     * {@inheritDoc}
     */
    public function getTimestampTypeDeclarationSQL(array $fieldDeclaration) {
        return 'TIMESTAMP';
    }

    /**
     * {@inheritDoc}
     */
    protected function initializeDoctrineTypeMappings() {
        $this->doctrineTypeMapping = array(
            'tinyint' => 'boolean',
            'smallint' => 'smallint',
            'mediumint' => 'integer',
            'int' => 'integer',
            'integer' => 'integer',
            'bigint' => 'bigint',
            'tinytext' => 'text',
            'mediumtext' => 'text',
            'longtext' => 'text',
            'text' => 'text',
            'varchar' => 'string',
            'string' => 'string',
            'char' => 'string',
            'date' => 'date',
            'datetime' => 'datetime',
            'timestamp' => 'timestamp',
            'time' => 'time',
            'float' => 'float',
            'double' => 'float',
            'real' => 'float',
            'decimal' => 'decimal',
            'numeric' => 'decimal',
            'year' => 'date',
            'longblob' => 'blob',
            'blob' => 'blob',
            'mediumblob' => 'blob',
            'tinyblob' => 'blob',
            'binary' => 'blob',
            'varbinary' => 'blob',
            'set' => 'simple_array',
        );
    }

}

And finally I created the class %code% in my project, inheriting class %code% % native Doctrine, to represent a Tymestamp type attribute for the mapping.

namespace Doctrine\DBAL\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;

class TimestampType extends Type {

    const TIMESTAMP = 'timestamp';

    /**
     * {@inheritdoc}
     */
    public function getName() {
        return self::TIMESTAMP;
    }

    /**
     * {@inheritdoc}
     */
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) {

        return $platform->getTimestampTypeDeclarationSQL($fieldDeclaration);
    }

    /**
     * {@inheritdoc}
     */
    public function convertToDatabaseValue($value, AbstractPlatform $platform) {
        return ($value !== null) ? $value->format($platform->getTimestampTypeDeclarationSQL()) : null;
    }

    /**
     * {@inheritdoc}
     */
    public function convertToPHPValue($value, AbstractPlatform $platform) {
        if ($value === null || $value instanceof \DateTime) {
            return $value;
        }

        $val = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value);
        if (!$val) {
            throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString());
        }

        return $val;
    }

}
    
___
08.08.2014 / 19:08