Skip to content

Instantly share code, notes, and snippets.

@xphere
Created November 7, 2013 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xphere/7354412 to your computer and use it in GitHub Desktop.
Save xphere/7354412 to your computer and use it in GitHub Desktop.
Store arrays of strings into comma-separated values with Doctrine2
<?php
namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
class CSVType extends StringType
{
const NAME = 'csv';
const DELIMITER = ',';
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if (!is_array($value)) {
return $value;
}
return implode(self::DELIMITER, $value);
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === null) {
return null;
}
return explode(self::DELIMITER, $value);
}
public function getName()
{
return self::NAME;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment