alex-qiu/php7-mongo-adapter

php7 mongodb adapter

v0.0.1 2018-11-07 07:36 UTC

This package is auto-updated.

Last update: 2024-10-22 20:29:43 UTC


README

Latest Stable Version Build Status Coverage Status codecov Total Downloads License

With the advent of PHP7 the old mongodb driver is no more supported.

The new driver available is a little bit low-level compared to the previous one so it can be a bit complicated to work with.

This is what this library was conceived for.

Installation

Using composer is quite simple, just run the following command:

$ composer require alex-qiu/php7-mongo-adapter

Prerequisites

Before using this library you should make sure to have installed PHP7.0 or major and MongoDb driver from pecl.

For those using a Linux distribution (make sure to have pecl installed) just run:

$ sudo pecl install mongodb

After that you should put the following string

extension=mongodb.so

Inside your php.ini

Usage

At first you need to define a connection string.

The format for connection strings is:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

For more information see the link: https://docs.mongodb.com/manual/reference/connection-string/

Once defined you need to instantiate a new Adapter:

use MongoDriver\Adapter;

// Enstablish a connection.
$adapter = new Adapter();
$adapter->connect(CONNECTION_STRING);

At this point you want to select a Database where do your query:

$adapter->selectDB('myDatabase');

NOTE: you could select a database directly on the constructor passing the database name as the 2nd parameter.

Find

Once selected the database we can simply query for the collection we want:

$items = $adapter->find('myCollection');

You can also filter your query:

use MongDriver\Filter;

$filters =
[
    new Filter('myField1', 'myValue1', Filter::IS_EQUALS),
    new Filter('myField2', ['myValue2', 'myValue3'], Filters::IS_IN_ARRAY)
];

$items = $adapter->find('myCollection', $filters);

Insert

If you want to insert an item you have simply to pass an array or an object to the insert function specifying the collection:

$item = new Person();
$item->name = 'Thomas';
$item->surname = 'Cocchiara');

// or: $item = ['name' => 'Thomas', 'surname' => 'Cocchiara'];

$adapter->insert('people', $item);

Hope you guys find this library useful.

Please share it and give me a feedback :)

Alex