简体   繁体   中英

Doctrine custom mapping type schema update

I am trying to create a custom Doctrine mapping type, as per: http://doctrine-orm.readthedocs.org/projects/doctrine-dbal/en/latest/reference/types.html#custom-mapping-types

My class is as follows:

<?php
namespace AppBundle\Doctrine\Type;

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;

/**
 * My custom datatype.
 */
class BinaryStringType extends Type
{

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

    public function getDefaultLength(AbstractPlatform $platform)
    {
        return $platform->getVarcharDefaultLength();
    }

    public function getName()
    {
        return 'binarystring';
    }
}

?>

And in config.yml doctrine section:

dbal:
    types:
        binarystring: AppBundle\Doctrine\Type\BinaryStringType

It (kind of) works, but: running php bin/console doctrine:schema:update ALWAYS generates an ALTER TABLE statement, no matter if database is up to date or not.

ALTER TABLE xxxx CHANGE column column VARBINARY(24) DEFAULT NULL;

Any ideas how to fix this?

thanks!

Your problem is that doctrine is recignising your field as a regular string type rather than your binary string so it is trying to convert it each time.

If you add..

/**
 * {@inheritdoc}
 */
public function requiresSQLCommentHint(AbstractPlatform $platform)
{
    return true;
}

.. to your class then it will add something like COMMENT \\'(DC2Type:binarystring)\\' to the alter table call in your migration (and a comment hint to your DB) so it is recognised as your field type in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM