Skip to content

Instantly share code, notes, and snippets.

@darklow
Created July 17, 2012 12:13
Show Gist options
  • Save darklow/3129096 to your computer and use it in GitHub Desktop.
Save darklow/3129096 to your computer and use it in GitHub Desktop.
Doctrine2 PostgreSQL tsvector type class
<?php
namespace Acme\AcmeBundle\Dbal;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
class TsvectorType extends Type
{
public function getName()
{
return 'tsvector';
}
public function canRequireSQLConversion()
{
return true;
}
public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return "TSVECTOR";
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return $value;
}
public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform)
{
return sprintf('to_tsvector(%s)', $sqlExpr);
}
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if (is_array($value)) {
$value = implode(" ", $value);
}
return $value;
}
}
/** Config YML
doctrine:
dbal:
types:
tsvector: Acme\AcmeBundle\Dbal\TsvectorType
connections:
default:
mapping_types:
tsvector: tsvector
**/
// Entity column definition
/**
* @var array|string $tsv
* @ORM\Column(name="tsv", type="tsvector", nullable=true)
*/
private $tsv;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment