Git Product home page Git Product logo

mongodb's Introduction

Doctrine MongoDB

The Doctrine MongoDB project is a simple layer that wraps the PECL Mongo extension and adds new functionality like logging, events, a query builder and improves the API user friendliness.

It is very easy to use, you only need a new Connection instance:

use Doctrine\MongoDB\Connection;

$connection = new Connection();

If the default setup is not good for you then you can pass some optional arguments like the following:

use Doctrine\MongoDB\Connection;
use Doctrine\MongoDB\Configuration;
use Doctrine\Common\EventManager;

$config = new Configuration();
$evm = new EventManager();
$conn = new Connection('mongodb://localhost', array('connect' => false), $config, $evm);

Now it is almost identical to the PECL extensions API:

$coll = $conn->selectDatabase('dbname')->selectCollection('users');
$user = array(
    'username' => 'jwage'
);
$coll->insert($user);

echo $user['_id']; // new id assigned in _id key

It comes with some additional functionality like an object oriented query builder:

$qb = $coll->createQueryBuilder()
    ->field('username')->equals('jwage');

$query = $qb->getQuery();
$user = $query->getSingleResult();

It also adds the ability to connect to events that get triggered internally. The list of available events are:

  • preBatchInsert
  • postBatchInsert
  • preSave
  • postSave
  • preInsert
  • postInsert
  • preUpdate
  • postUpdate
  • preRemove
  • postRemove
  • preFind
  • postFind
  • preFindOne
  • postFindOne
  • preFindAndRemove
  • postFindAndRemove
  • preFindAndUpdate
  • postFindAndUpdate
  • preGroup
  • postGroup
  • preGetDBRef
  • postGetDBRef
  • preCreateDBRef
  • postCreateDBRef
  • preDistinct
  • postDistinct
  • preNear
  • postNear
  • preCreateCollection
  • postCreateCollection
  • preSelectDatabase
  • postSelectDatabase
  • preDropDatabase
  • postDropDatabase
  • preSelectCollection
  • postSelectCollection
  • preDropCollection
  • postDropCollection
  • preGetGridFS
  • postGetGridFS
  • preConnect
  • postConnect

You can connect to these events using the EventManager:

use Doctrine\MongoDB\Events;
use Doctrine\MongoDB\Event\EventArgs;

$connectionEvents = new ConnectionEvents();
$evm->addEventListener(Events::preConnect, $connectionEvents);

Now we just need to write the simple PHP class we initialized above:

class ConnectionEvents
{
    public function preConnect(EventArgs $eventArgs)
    {
        // do something before the connection to mongodb is initialized
    }
}

mongodb's People

Contributors

avalanche123 avatar jwage avatar kriswallsmith avatar ornicar avatar pborreli avatar rybakit avatar sveneisenschmidt avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.