Skip to content
Embedded document relations support for MongoDB in li3
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Lithium PHP Plugin to allow embedded relations for MongoDB

This is in its early stages and only supports READ operations. Hopefully, this plugin will not be needed for very long as li3 plans to support embedded relations in the core.

A plugin to add support to li3 embedded relations for MongoDb. Lithiums mongo adapter says it supports embedded relations, but after much investigation through the core it appears this is not the case. Basically, when an embedded relation is specified, it does the single query for the parent and when the data is returned it creates the appropriate model with the data returned from the parent.


Use Composer

Modify your projects composer.json file

    "require": {
        "brandonwestcott/li3_mongo_embedded": "master"

Run ./composer.phar install which will install this library into your app/libraries

Alternately, just clone, download or submodule

  1. Clone/Download/submodule the plugin into your app's libraries directory.
  2. Tell your app to load the plugin by adding the following to your app's config/bootstrap/libraries.php:


Add the plugin in your config/bootstrap/libraries.php file:


Next, in your app/config/connections.php specify this extended MongoDB adapter.

Connections::add('default', array(
    'type' => 'MongoDb', 
    'adapter' => 'MongoEmbedded', 
    'host' => 'localhost', 
    'database' => 'foo'

Using an Embedded Relation

Continue defining relations in the lithium specified way as described here, except for the embedded key

class Team extends  \lithium\data\Model.php {

    public $hasMany = array(
        'Players' => array(
            'to'       => 'Players',
            'embedded' => 'players'
        'Scouts' => array(
            'to' => 'Scouts',
            'embedded' => 'miscellaneous.offseason.scouts',
            'fieldName' => 'scouts',

Key specified is the name used to reference the relation on a find query.

Options are:
to - specified target model
embedded - the key on which the data is embedded
fieldName - the key on which the related model will be attached (in the above example the nested scouts DocumentSet would be embedded onto $team->scouts)

Calling Relations

Relations are called in the lithium specified way as described here

Team::find('first', array(
    '_id' => 1,
    'with' => array(

This would return a Document of a Team. On the team would exists players, which would be an instance of the Players model. (Debating to make the reference live in a magic method vs a property - any input is welcome, aka ->players())

hasOnes will be set as a Document.

hasManies will be set as a DocumentSet.

However, when no data is returned, the behavior is slightly different. An empty hasOne will return null, as is the behavior of calling Model::find('first'). An empty hasMany will continue to return an empty DocumentSet, as is the behavior of calling Model::find('all').

Some Notes

  1. Beta Beta Beta - Currently, this plugin is being used heavily in a read MongoDB environment. However, writes will likely majorly screw up your db. Use with caution.
  2. Need to move the relations on to the $_relationships array on Entity

Plans for the future

Hopefully this plugin has a short future. This was a quick solution that allowed us not to hack core li3. I hope to move this work into a fork of the core and contribute there.

Something went wrong with that request. Please try again.