2017 © Pedro Peláez
 

li3-libraries li3_embedded

Lithium PHP mongo embedded relations

image

brandonwestcott/li3_embedded

Lithium PHP mongo embedded relations

  • Friday, October 12, 2012
  • by brandonwestcott
  • Repository
  • 1 Watchers
  • 0 Stars
  • 16,343 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Lithium PHP Plugin to allow embedded relations for MongoDB

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

A plugin to add support to li3 embedded relations for MongoDb. Lithiums mongo adapter says it supports embedded relations, but after much investigation throught 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 appropiate model with the data returned from the parent., (*2)

Installation

Use Composer

Modify your projects composer.json file, (*3)

~~~ json { "require": { ... "brandonwestcott/li3_embedded": "master" ... } }, (*4)


Run `./composer.phar install` which will install this librarie 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``: ## Usage Add the plugin in your `config/bootstrap/libraries.php` file: ~~~ php <?php Libraries::add('li3_embedded'); ?>

Next, in your app/config/connections.php specify this extended MongoDB adapter. ~~~ php Connections::add('default', array( 'type' => 'MongoDb', 'adapter' => 'MongoEmbedded', 'host' => 'localhost', 'database' => 'foo' ));, (*5)


#### Using an Embedded Relation Continue defining relations in the lithium specified way as described [here](http://lithify.me/docs/manual/working-with-data/relationships.wiki), except for the embedded key ~~~ php 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 refernce the relation on a find query., (*6)

Options are:
to - specifieds 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), (*7)

Calling Relations

Relations are called in the lithium specified way as described here, (*8)

~~~ php Team::find('first', array( '_id' => 1, 'with' => array( 'Players', ), )); ~~~, (*9)

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()), (*10)

hasOnes will be set as a Document., (*11)

hasManies will be set as a DocumentSet., (*12)

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')., (*13)

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.

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., (*14)

The Versions

12/10 2012

dev-master

9999999-dev https://github.com/brandonwestcott/li3_embedded

Lithium PHP mongo embedded relations

  Sources   Download

GNU GENERAL PUBLIC LICENSE

The Requires

 

mongodb models lithium li3 relations