b2pweb / bdf-prime-mongodb
Bdf prime MongoDB component
dev-master / 1.x-dev
2022-01-19 10:21 UTC
Requires
- php: ~7.1 | ~8.0.0
- ext-mongodb: *
Requires (Dev)
- b2pweb/bdf-prime: ~1.3
- phpunit/phpunit: ~7.5|~8.0
- squizlabs/php_codesniffer: ~3.0
- vimeo/psalm: ~4.0
This package is auto-updated.
Last update: 2022-05-18 14:41:41 UTC
README
MongoDB driver for Prime
Installation
Install with composer :
composer require b2pweb/bdf-prime-mongodb
Create connection :
<?php use Bdf\Prime\ConnectionManager; // declare your connexion manager $connexions = new ConnectionManager(); // Declare connection without username and password $connexions->declareConnection('mongo', 'mongodb://127.0.0.1/my_collection?noAuth=true'); // With credentials $connexions->declareConnection('mongo', 'mongodb://user:password@127.0.0.1/my_database');
Usage
Declare a Mapper
MongoDB mapper declaration is almost same as SQL mapper, only primary key declaration differ :
<?php use Bdf\Prime\Mapper\Mapper; use Bdf\Prime\MongoDB\Odm\MongoIdGenerator; class MyDocumentMapper extends Mapper { /** * {@inheritdoc} */ public function configure() { // Declare generator for generation MongoId on insertion $this->setGenerator(MongoIdGenerator::class); } /** * {@inheritdoc} */ public function schema() { return [ 'connection' => 'mongo', 'table' => 'my_collection', // Use 'table' for define the collection name ]; } /** * {@inheritdoc} */ public function buildFields($builder) { $builder // The mongo id must be declared as primary ->string('oid')->primary() // The rest of fields declaration is same as other prime mappers ->string('name') ->object('value') // Unlike SQL, there is no need to define an alias for embedded values // By default (i.e. without alias), the embedded value will be stored as embedded document on the collection ->embedded('foo', function ($builder) { $builder ->string('bar') ->string('rab') ; }) ; } }
Querying MongoDB
The query system use Prime interfaces, so usage is almost the same :
<?php // Get the query /** @var \Bdf\Prime\MongoDB\Query\MongoQuery $query */ $query = MyDocument::builder(); $query ->where('name', 'John') // Simple where works as expected ->where('value.attr', ':like', 'P%') // "like" operator is converted to a regex ->where('value.foo', '$type', 'javascript') // Use mongodb operator ; // Get all documents which match with filters $query->all(); // First returns the first matching document or null $query->first();
Testing
The TestPack
of Prime is compatible with MongoDB
Case-insensitive search and index
To enable case-insensitive search by default, you can add default collation on table options. See Case Insensitive Indexes
<?php use Bdf\Prime\Mapper\Mapper; use Bdf\Prime\MongoDB\Odm\MongoIdGenerator; class MyDocumentMapper extends Mapper { /** * {@inheritdoc} */ public function configure() { // Declare generator for generation MongoId on insertion $this->setGenerator(MongoIdGenerator::class); } /** * {@inheritdoc} */ public function schema() { return [ 'connection' => 'mongo', 'table' => 'my_collection', 'tableOptions' => [ 'collation' => [ 'locale' => 'en', 'strength' => 2, ], ], ]; } // ... }