From 44cd77f0228784b61d1109e98512bfe61df07924 Mon Sep 17 00:00:00 2001 From: "Johannes M. Schmitt" Date: Mon, 7 May 2012 12:16:37 -0500 Subject: [PATCH 1/3] added simple_array type --- lib/Doctrine/DBAL/Types/SimpleArrayType.php | 63 +++++++++++++++++++++ lib/Doctrine/DBAL/Types/Type.php | 4 +- 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100755 lib/Doctrine/DBAL/Types/SimpleArrayType.php diff --git a/lib/Doctrine/DBAL/Types/SimpleArrayType.php b/lib/Doctrine/DBAL/Types/SimpleArrayType.php new file mode 100755 index 00000000000..1986f6cf241 --- /dev/null +++ b/lib/Doctrine/DBAL/Types/SimpleArrayType.php @@ -0,0 +1,63 @@ +. + */ + +namespace Doctrine\DBAL\Types; + +/** + * Array Type which can be used for simple values. + * + * Only use this type if you are sure that your values cannot contain a ",". + * + * @since 2.3 + * @author Johannes M. Schmitt + */ +class SimpleArrayType extends Type +{ + const NAME = 'simple_array'; + + public function getSQLDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) + { + return $platform->getClobTypeDeclarationSQL($fieldDeclaration); + } + + public function convertToDatabaseValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) + { + if (!$value) { + return null; + } + + return implode(',', $value); + } + + public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) + { + if ($value === null) { + return array(); + } + + $value = (is_resource($value)) ? stream_get_contents($value) : $value; + + return explode(',', $value); + } + + public function getName() + { + return Type::SIMPLE_ARRAY; + } +} \ No newline at end of file diff --git a/lib/Doctrine/DBAL/Types/Type.php b/lib/Doctrine/DBAL/Types/Type.php index d59ed4ab815..593a62f55f3 100644 --- a/lib/Doctrine/DBAL/Types/Type.php +++ b/lib/Doctrine/DBAL/Types/Type.php @@ -34,6 +34,7 @@ abstract class Type { const TARRAY = 'array'; + const SIMPLE_ARRAY = 'simple_array'; const BIGINT = 'bigint'; const BOOLEAN = 'boolean'; const DATETIME = 'datetime'; @@ -56,6 +57,7 @@ abstract class Type /** The map of supported doctrine mapping types. */ private static $_typesMap = array( self::TARRAY => 'Doctrine\DBAL\Types\ArrayType', + self::SIMPLE_ARRAY => 'Doctrine\DBAL\Types\SimpleArrayType', self::OBJECT => 'Doctrine\DBAL\Types\ObjectType', self::BOOLEAN => 'Doctrine\DBAL\Types\BooleanType', self::INTEGER => 'Doctrine\DBAL\Types\IntegerType', @@ -191,7 +193,7 @@ public static function overrideType($name, $className) if ( ! isset(self::$_typesMap[$name])) { throw DBALException::typeNotFound($name); } - + if (isset(self::$_typeObjects[$name])) { unset(self::$_typeObjects[$name]); } From 2d183acdf06596ade895d2de9a39e756bce3a5af Mon Sep 17 00:00:00 2001 From: "Johannes M. Schmitt" Date: Sun, 27 May 2012 08:17:18 -0500 Subject: [PATCH 2/3] added JsonArrayType, fixed commented types --- .../DBAL/Platforms/AbstractPlatform.php | 2 +- lib/Doctrine/DBAL/Types/JsonArrayType.php | 59 +++++++++++++++++++ lib/Doctrine/DBAL/Types/SimpleArrayType.php | 2 - lib/Doctrine/DBAL/Types/Type.php | 2 + 4 files changed, 62 insertions(+), 3 deletions(-) create mode 100755 lib/Doctrine/DBAL/Types/JsonArrayType.php diff --git a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php index 9687febb491..f861bbb5b09 100644 --- a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php @@ -301,7 +301,7 @@ public function hasDoctrineTypeMappingFor($dbType) */ protected function initializeCommentedDoctrineTypes() { - $this->doctrineTypeComments = array(Type::TARRAY, Type::OBJECT); + $this->doctrineTypeComments = array(Type::TARRAY, Type::SIMPLE_ARRAY, Type::JSON_ARRAY, Type::OBJECT); } /** diff --git a/lib/Doctrine/DBAL/Types/JsonArrayType.php b/lib/Doctrine/DBAL/Types/JsonArrayType.php new file mode 100755 index 00000000000..1ca8881d76e --- /dev/null +++ b/lib/Doctrine/DBAL/Types/JsonArrayType.php @@ -0,0 +1,59 @@ +. + */ + +namespace Doctrine\DBAL\Types; + +/** + * Array Type which can be used to generate json arrays. + * + * @since 2.3 + * @author Johannes M. Schmitt + */ +class SimpleArrayType extends Type +{ + public function getSQLDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) + { + return $platform->getClobTypeDeclarationSQL($fieldDeclaration); + } + + public function convertToDatabaseValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) + { + if (null === $value) { + return null; + } + + return json_encode($value); + } + + public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) + { + if ($value === null) { + return array(); + } + + $value = (is_resource($value)) ? stream_get_contents($value) : $value; + + return json_decode($value, true); + } + + public function getName() + { + return Type::JSON_ARRAY; + } +} \ No newline at end of file diff --git a/lib/Doctrine/DBAL/Types/SimpleArrayType.php b/lib/Doctrine/DBAL/Types/SimpleArrayType.php index 1986f6cf241..8e95def626e 100755 --- a/lib/Doctrine/DBAL/Types/SimpleArrayType.php +++ b/lib/Doctrine/DBAL/Types/SimpleArrayType.php @@ -29,8 +29,6 @@ */ class SimpleArrayType extends Type { - const NAME = 'simple_array'; - public function getSQLDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) { return $platform->getClobTypeDeclarationSQL($fieldDeclaration); diff --git a/lib/Doctrine/DBAL/Types/Type.php b/lib/Doctrine/DBAL/Types/Type.php index 593a62f55f3..d3d021edc2f 100644 --- a/lib/Doctrine/DBAL/Types/Type.php +++ b/lib/Doctrine/DBAL/Types/Type.php @@ -35,6 +35,7 @@ abstract class Type { const TARRAY = 'array'; const SIMPLE_ARRAY = 'simple_array'; + const JSON_ARRAY = 'json_array'; const BIGINT = 'bigint'; const BOOLEAN = 'boolean'; const DATETIME = 'datetime'; @@ -58,6 +59,7 @@ abstract class Type private static $_typesMap = array( self::TARRAY => 'Doctrine\DBAL\Types\ArrayType', self::SIMPLE_ARRAY => 'Doctrine\DBAL\Types\SimpleArrayType', + self::JSON_ARRAY => 'Doctrine\DBAL\Types\JsonArrayType', self::OBJECT => 'Doctrine\DBAL\Types\ObjectType', self::BOOLEAN => 'Doctrine\DBAL\Types\BooleanType', self::INTEGER => 'Doctrine\DBAL\Types\IntegerType', From 1760be6469aae3848d3d04f0474e3ecbd4d9dbc9 Mon Sep 17 00:00:00 2001 From: "Johannes M. Schmitt" Date: Sun, 27 May 2012 08:20:22 -0500 Subject: [PATCH 3/3] fixed name --- lib/Doctrine/DBAL/Types/JsonArrayType.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Doctrine/DBAL/Types/JsonArrayType.php b/lib/Doctrine/DBAL/Types/JsonArrayType.php index 1ca8881d76e..37c60921e8a 100755 --- a/lib/Doctrine/DBAL/Types/JsonArrayType.php +++ b/lib/Doctrine/DBAL/Types/JsonArrayType.php @@ -25,7 +25,7 @@ * @since 2.3 * @author Johannes M. Schmitt */ -class SimpleArrayType extends Type +class JsonArrayType extends Type { public function getSQLDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) {