diff --git a/README.md b/README.md index cc23395..a613796 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [pgvector](https://github.com/pgvector/pgvector) support for PHP -Supports [Laravel](https://github.com/laravel/laravel), [Doctrine](https://github.com/doctrine/orm), and [PgSql](https://www.php.net/manual/en/book.pgsql.php) +Supports [Laravel](https://github.com/laravel/laravel), [Doctrine](https://github.com/doctrine/orm), [CodeIgniter](https://github.com/codeigniter4/CodeIgniter4), and [PgSql](https://www.php.net/manual/en/book.pgsql.php) [![Build Status](https://github.com/pgvector/pgvector-php/actions/workflows/build.yml/badge.svg)](https://github.com/pgvector/pgvector-php/actions) @@ -12,6 +12,7 @@ Follow the instructions for your database library: - [Laravel](#laravel) - [Doctrine](#doctrine) +- [CodeIgniter](#codeigniter) - [PgSql](#pgsql) Or check out some examples: @@ -169,6 +170,10 @@ $neighbors = $entityManager->createQuery('SELECT i FROM Item i ORDER BY l2_dista Also supports `max_inner_product`, `cosine_distance`, `l1_distance`, `hamming_distance`, and `jaccard_distance` +### CodeIgniter + +todo + ### PgSql Enable the extension diff --git a/composer.json b/composer.json index 12db7a6..c93b8dd 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ "php": ">= 8.1" }, "require-dev": { + "codeigniter4/framework": "^4.5", "doctrine/dbal": "^4", "doctrine/orm": "^3", "phpunit/phpunit": "^10", diff --git a/tests/CodeIgniterTest.php b/tests/CodeIgniterTest.php new file mode 100644 index 0000000..fc296a7 --- /dev/null +++ b/tests/CodeIgniterTest.php @@ -0,0 +1,64 @@ + 'vector', + ]; + + protected array $castHandlers = [ + 'vector' => CastVector::class, + ]; +} + +final class CodeIgniterTest extends TestCase +{ + public function testWorks() + { + $config = [ + 'DSN' => 'Postgre://localhost/pgvector_php_test?charset=utf8', + ]; + $db = Database::connect($config); + + $db->query('CREATE EXTENSION IF NOT EXISTS vector'); + $db->query('DROP TABLE IF EXISTS ci_items'); + $db->query('CREATE TABLE IF NOT EXISTS ci_items (id bigserial PRIMARY KEY, embedding vector(3))'); + + $itemModel = new ItemModel($db); + $itemModel->insert(['embedding' => new Vector([1, 1, 1])], false); + $itemModel->insert(['embedding' => new Vector([2, 2, 2])], false); + $itemModel->insert(['embedding' => new Vector([1, 1, 2])], false); + + $escaped = $db->escape(new Vector([1, 1, 1])); + $items = $itemModel->orderBy("embedding <-> $escaped")->findAll(); + $this->assertEquals([1, 3, 2], array_map(fn ($v) => $v['id'], $items)); + $this->assertEquals(new Vector([1, 1, 2]), $items[1]['embedding']); + $this->assertInstanceOf(Vector::class, $items[1]['embedding']); + } +}