简体   繁体   中英

Symfony2 add new doctrine field type

i lost 3 hours on this thing, and still cant figure out whats wrong, iam trying to add a VARBINARY field type, and doctrine shows me that field as a option when generating new entity, but the problem is when i do php app/console doctrine:schema:update --force then it gives me an error,

    [DoctrineDBAL\DBALException]
An exception occured while executing 'CREATE TABLE Test (id INT AUTO_INCREMENT NOT NULL, name VARBINARY NOT NULL, PRIMARY KEY(id))


SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;check the manual that corresponds to your MySQL server version for the right sytax to use near 'NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci EN' at line 1

this is my VARBINARY type class

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

class VarBinaryType extends Type {

    const VARBINARY = 'VARBINARY';

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

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

    public function convertToDatabaseValue($value, AbstractPlatform $platform) {
        return ($value === null)? null : base64_encode($value);
    }

    public function convertToPHPValue($value, AbstractPlatform $platform) {
        return ($value === null)? null : base64_decode($value);
    }

}

and i also did register it within boot function

$connection = $this->container->get('doctrine.orm.entity_manager')->getConnection();
        if(!Type::hasType('VARBINARY'))
        {

            Type::addType('VARBINARY', 'Uapi\\CoreBundle\\System\\DBALType\\VarBinaryType');
            $connection->getDatabasePlatform()->registerDoctrineTypeMapping('VARBINARY', 'VARBINARY');
        }

You have to provide field length , eg:

 CREATE TABLE t (c VARBINARY(8));

Look into Doctrine docs about how to provide such value [out of hardcoding it].

In conjunction with moonwave99's advice, here's a solution.

In your VarBinaryType class, use this code for the getSQLDeclaration function:

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) {
    if(isset($fieldDeclaration['length'])){
        return "VARBINARY (".$fieldDeclaration['length'].")";
    }else{
        return "VARBINARY (1024)";
    }
}

I found out this answer by going through the source code, which is was more helpful than the documentation. Using the above code works for MySQL. Depending on your implementation, you may need to adjust it for the database platform you are using.

I'm posting this here for those who are looking for this solution in regards to older builds of Doctrine. The newest version currently supports both BINARY and VARBINARY types.

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