Skip to content

Instantly share code, notes, and snippets.

@peshi
Forked from xphere/CSVType.php
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peshi/8761720 to your computer and use it in GitHub Desktop.
Save peshi/8761720 to your computer and use it in GitHub Desktop.
<?php
namespace Acme\DemoBundle\DBAL\Type;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* Class SemicolonArrayType represents an array of values that will be persisted as a string of semicolon-separated strings.
*
* @see http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_find-in-set
* @package Acme\DemoBundle\Type
*/
class SemicolonArrayType extends Type
{
/**
* Name of this type
*/
const TYPE = 'semicolon_array';
const DELIMITER = ';';
/**
* @inheritDoc
*/
public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->getClobTypeDeclarationSQL($fieldDeclaration);
}
/**
* @inheritDoc
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === null) {
return null;
}
return explode(self::DELIMITER, $value);
}
/**
* @inheritDoc
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if (!is_array($value)) {
return $value;
}
return implode(self::DELIMITER, $value);
}
/**
* {@inheritdoc}
*/
public function requiresSQLCommentHint(AbstractPlatform $platform)
{
return true;
}
/**
* @inheritDoc
*/
public function getName()
{
return self::TYPE;
}
}
@peshi
Copy link
Author

peshi commented Feb 3, 2014

Symfony 2.4.x

doctrine:
    dbal:
        .....
        types:
            semicolon_array: Acme\DemoBundle\DBAL\Type\SemicolonArrayType
        mapping_types:
            semicolon_array: semicolon_array

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment