Skip to content

Commit 69736e6

Browse files
committed
Added bit type for Doctrine [skip ci]
1 parent d539f4b commit 69736e6

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

src/doctrine/BitType.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Pgvector\Doctrine;
4+
5+
use Doctrine\DBAL\Types\Type;
6+
use Doctrine\DBAL\Platforms\AbstractPlatform;
7+
8+
class BitType extends Type
9+
{
10+
public function getName(): string
11+
{
12+
return 'bit';
13+
}
14+
15+
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
16+
{
17+
$length = $fieldDeclaration['length'];
18+
return is_null($length) ? 'bit' : sprintf('bit(%d)', $length);
19+
}
20+
21+
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?string
22+
{
23+
if (is_null($value)) {
24+
return null;
25+
}
26+
27+
return (string) $value;
28+
}
29+
30+
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string
31+
{
32+
if (is_null($value)) {
33+
return null;
34+
}
35+
36+
return (string) $value;
37+
}
38+
}

tests/DoctrineTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function testTypes()
3232

3333
Type::addType('vector', 'Pgvector\Doctrine\VectorType');
3434
Type::addType('halfvec', 'Pgvector\Doctrine\HalfVectorType');
35+
Type::addType('bit', 'Pgvector\Doctrine\BitType');
3536
Type::addType('sparsevec', 'Pgvector\Doctrine\SparseVectorType');
3637

3738
$platform = $entityManager->getConnection()->getDatabasePlatform();
@@ -52,6 +53,7 @@ public function testTypes()
5253
$item = new DoctrineItem();
5354
$item->setEmbedding(new Vector([1, 2, 3]));
5455
$item->setHalfEmbedding(new HalfVector([4, 5, 6]));
56+
$item->setBinaryEmbedding('101');
5557
$item->setSparseEmbedding(new SparseVector([7, 8, 9]));
5658
$entityManager->persist($item);
5759
$entityManager->flush();
@@ -60,6 +62,7 @@ public function testTypes()
6062
$item = $itemRepository->find(1);
6163
$this->assertEquals([1, 2, 3], $item->getEmbedding()->toArray());
6264
$this->assertEquals([4, 5, 6], $item->getHalfEmbedding()->toArray());
65+
$this->assertEquals('101', $item->getBinaryEmbedding());
6366
$this->assertEquals([7, 8, 9], $item->getSparseEmbedding()->toArray());
6467
}
6568
}

tests/models/DoctrineItem.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class DoctrineItem
2020
#[ORM\Column(type: 'halfvec', length: 3)]
2121
private HalfVector $halfEmbedding;
2222

23+
#[ORM\Column(type: 'bit', length: 3)]
24+
private string $binaryEmbedding;
25+
2326
#[ORM\Column(type: 'sparsevec', length: 3)]
2427
private SparseVector $sparseEmbedding;
2528

@@ -43,6 +46,16 @@ public function setHalfEmbedding(HalfVector $embedding): void
4346
$this->halfEmbedding = $embedding;
4447
}
4548

49+
public function getBinaryEmbedding(): string
50+
{
51+
return $this->binaryEmbedding;
52+
}
53+
54+
public function setBinaryEmbedding(string $embedding): void
55+
{
56+
$this->binaryEmbedding = $embedding;
57+
}
58+
4659
public function getSparseEmbedding(): SparseVector
4760
{
4861
return $this->sparseEmbedding;

0 commit comments

Comments
 (0)