Skip to content

Fixed belongsToMany sync() #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 64 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
3d6192a
Readme updated to show belongsToMany relationship availability
Daveawb Nov 21, 2013
da04754
Slimming down the BelongsToMany class
jenssegers Nov 20, 2013
485c25a
Tweaking BelongsToMany namespaces
jenssegers Nov 20, 2013
97e00b6
Small readme update for belongsToMany
jenssegers Nov 21, 2013
293fa5c
Markdown fixes
jenssegers Nov 21, 2013
e9b83e5
conflicts
Daveawb Nov 21, 2013
cc77fae
removed conflict
Daveawb Nov 21, 2013
062412a
Merge remote branch 'upstream/master'
Daveawb Nov 21, 2013
de0d889
removed settings
Daveawb Nov 21, 2013
fd5a470
updated gitgnore
Daveawb Nov 21, 2013
3b9b81a
README update
Daveawb Nov 25, 2013
e378c96
Readme updated to show belongsToMany relationship availability
Daveawb Nov 21, 2013
c797abd
Small readme update for belongsToMany
jenssegers Nov 21, 2013
46cbc34
Markdown fixes
jenssegers Nov 21, 2013
fb77334
conflicts
Daveawb Nov 21, 2013
d7da34b
removed conflict
Daveawb Nov 21, 2013
754216e
removed settings
Daveawb Nov 21, 2013
8d58aa0
updated gitgnore
Daveawb Nov 21, 2013
1f99417
Merge branch 'master' of github.com:DyeH/Laravel-MongoDB
Daveawb Nov 25, 2013
4885879
Fixed bug where related model doesn't have parent _id removed on sync
Daveawb Nov 27, 2013
706a3b8
Readme updated to show belongsToMany relationship availability
Daveawb Nov 21, 2013
4351a13
Small readme update for belongsToMany
jenssegers Nov 21, 2013
63ea245
conflicts
Daveawb Nov 21, 2013
a874d19
removed conflict
Daveawb Nov 21, 2013
6801149
removed settings
Daveawb Nov 21, 2013
be2e956
updated gitgnore
Daveawb Nov 21, 2013
6a7d264
README update
Daveawb Nov 25, 2013
9b9bd7c
Readme updated to show belongsToMany relationship availability
Daveawb Nov 21, 2013
b3ce53b
Small readme update for belongsToMany
jenssegers Nov 21, 2013
1fcf12e
Markdown fixes
jenssegers Nov 21, 2013
73edfab
conflicts
Daveawb Nov 21, 2013
3c1d049
removed conflict
Daveawb Nov 21, 2013
bd5c147
removed settings
Daveawb Nov 21, 2013
42dcd24
Fixed bug where related model doesn't have parent _id removed on sync
Daveawb Nov 27, 2013
9f4ed09
Merge branch 'master' of github.com:DyeH/Laravel-MongoDB
Daveawb Nov 27, 2013
b0bd0b2
Deleted merge file
Daveawb Nov 27, 2013
a9bed9a
Removed methods
Daveawb Nov 27, 2013
21a59dc
Readme re-updated
Daveawb Nov 27, 2013
1b7a84e
conflicts
Daveawb Nov 21, 2013
48b32cf
removed conflict
Daveawb Nov 21, 2013
0fdebc0
removed settings
Daveawb Nov 21, 2013
0213c71
updated gitgnore
Daveawb Nov 21, 2013
47d1d58
README update
Daveawb Nov 25, 2013
4a44c39
Readme updated to show belongsToMany relationship availability
Daveawb Nov 21, 2013
ceed9ef
conflicts
Daveawb Nov 21, 2013
0a0fa83
removed conflict
Daveawb Nov 21, 2013
b865d8a
removed settings
Daveawb Nov 21, 2013
dae8b71
Fixed bug where related model doesn't have parent _id removed on sync
Daveawb Nov 27, 2013
36cea2d
Readme updated to show belongsToMany relationship availability
Daveawb Nov 21, 2013
9428af0
Small readme update for belongsToMany
jenssegers Nov 21, 2013
d5ee4ce
conflicts
Daveawb Nov 21, 2013
c0ee666
removed conflict
Daveawb Nov 21, 2013
1b6093a
removed settings
Daveawb Nov 21, 2013
995710c
README update
Daveawb Nov 25, 2013
29ce2b5
Readme updated to show belongsToMany relationship availability
Daveawb Nov 21, 2013
db121a7
Small readme update for belongsToMany
jenssegers Nov 21, 2013
990960c
Markdown fixes
jenssegers Nov 21, 2013
09cb551
conflicts
Daveawb Nov 21, 2013
d3266c7
removed conflict
Daveawb Nov 21, 2013
2657b32
removed settings
Daveawb Nov 21, 2013
b6cba21
Deleted merge file
Daveawb Nov 27, 2013
9b2d939
Removed methods
Daveawb Nov 27, 2013
c185493
Readme re-updated
Daveawb Nov 27, 2013
74d4f7e
Merge branch 'master' of github.com:DyeH/Laravel-MongoDB
Daveawb Nov 27, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
conflicts
  • Loading branch information
Daveawb committed Nov 21, 2013
commit e9b83e56c07b91c2050934903fba1e5fece1c0c5
2 changes: 2 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/README.md=UTF-8
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ If you are using a different database driver as the default one, you will need t
Everything else works just like the original Eloquent model. Read more about the Eloquent on http://laravel.com/docs/eloquent

### Optional: Alias
-------------------

You may also register an alias for the MongoDB model by adding the following to the alias array in `app/config/app.php`:

Expand Down Expand Up @@ -267,8 +268,6 @@ Supported relations are:
- belongsTo
- belongsToMany

*The belongsToMany relation will not use a pivot "table", but will push id's to a __related_ids__ attribute instead.*

Example:

use Jenssegers\Mongodb\Model as Eloquent;
Expand Down Expand Up @@ -367,4 +366,4 @@ By default, Laravel keeps a log in memory of all queries that have been run for

DB::connection()->disableQueryLog();

*From: http://laravel.com/docs/database#query-logging*
*From: http://laravel.com/docs/database#query-logging*
370 changes: 370 additions & 0 deletions README.md.conflict
Original file line number Diff line number Diff line change
@@ -0,0 +1,370 @@
Laravel MongoDB
===============

[![Latest Stable Version](https://poser.pugx.org/jenssegers/mongodb/v/stable.png)](https://packagist.org/packages/jenssegers/mongodb) [![Total Downloads](https://poser.pugx.org/jenssegers/mongodb/downloads.png)](https://packagist.org/packages/jenssegers/mongodb) [![Build Status](https://travis-ci.org/jenssegers/Laravel-MongoDB.png?branch=master)](https://travis-ci.org/jenssegers/Laravel-MongoDB)

An Eloquent model and Query builder with support for MongoDB, inspired by LMongo, but using the original Laravel methods. *This library extends the original Laravel classes, so it uses exactly the same methods.*

Installation
------------

Add the package to your `composer.json` and run `composer update`.

{
"require": {
"jenssegers/mongodb": "*"
}
}

Add the service provider in `app/config/app.php`:

'Jenssegers\Mongodb\MongodbServiceProvider',

The service provider will register a mongodb database extension with the original database manager. There is no need to register additional facades or objects. When using mongodb connections, Laravel will automatically provide you with the corresponding mongodb objects.

Configuration
-------------

Change your default database connection name in `app/config/database.php`:

'default' => 'mongodb',

And add a new mongodb connection:

'mongodb' => array(
'driver' => 'mongodb',
'host' => 'localhost',
'port' => 27017,
'username' => 'username',
'password' => 'password',
'database' => 'database'
),

You can connect to multiple servers or replica sets with the following configuration:

'mongodb' => array(
'driver' => 'mongodb',
'host' => array('server1', 'server2),
'port' => 27017,
'username' => 'username',
'password' => 'password',
'database' => 'database',
'options' => array('replicaSet' => 'replicaSetName')
),

Eloquent
--------

Tell your model to use the MongoDB model and set the collection (alias for table) property. The lower-case, plural name of the class will be used for the collection name, unless another name is explicitly specified.

use Jenssegers\Mongodb\Model as Eloquent;

class MyModel extends Eloquent {

protected $collection = 'mycollection';

}

If you are using a different database driver as the default one, you will need to specify the mongodb connection within your model by changing the `connection` property:

use Jenssegers\Mongodb\Model as Eloquent;

class MyModel extends Eloquent {

protected $connection = 'mongodb';

}

Everything else works just like the original Eloquent model. Read more about the Eloquent on http://laravel.com/docs/eloquent

### Optional: Alias

You may also register an alias for the MongoDB model by adding the following to the alias array in `app/config/app.php`:

'Moloquent' => 'Jenssegers\Mongodb\Model',

This will allow you to use your registered alias like:

class MyModel extends Moloquent {

protected $collection = 'mycollection';

}

Query Builder
-------------

The database driver plugs right into the original query builder. When using mongodb connections you will be able to build fluent queries to perform database operations. For your convenience, there is a `collection` alias for `table` as well as some additional mongodb specific operations like `push` and `pull`.

// With custom connection
$user = DB::connection('mongodb')->collection('users')->get();

// Using default connection
$users = DB::collection('users')->get();
$user = DB::collection('users')->where('name', 'John')->first();

Read more about the query builder on http://laravel.com/docs/queries

Schema
------

The database driver also has (limited) schema builder support. You can easily manipulate collections and set indexes:

Schema::create('users', function($collection)
{
$collection->index('name');
$collection->unique('email');
});

Supported operations are:

- create and drop
- collection
- hasCollection
- index and dropIndex (compound indexes supported as well)
- unique
- background, sparse, expire (MongoDB specific)

Read more about the schema builder on http://laravel.com/docs/schema

Sessions
--------

The MongoDB session driver is available in a separate package, check out https://github.com/jenssegers/Laravel-MongoDB-Session

Examples
--------

### Basic Usage

**Retrieving All Models**

$users = User::all();

**Retrieving A Record By Primary Key**

$user = User::find('517c43667db388101e00000f');

**Wheres**

$users = User::where('votes', '>', 100)->take(10)->get();

**Or Statements**

$users = User::where('votes', '>', 100)->orWhere('name', 'John')->get();

**Using Where In With An Array**

$users = User::whereIn('age', array(16, 18, 20))->get();

When using `whereNotIn` objects will be returned if the field is non existent. Combine with `whereNotNull('age')` to leave out those documents.

**Using Where Between**

$users = User::whereBetween('votes', array(1, 100))->get();

**Where null**

$users = User::whereNull('updated_at')->get();

**Order By**

$users = User::orderBy('name', 'desc')->get();

**Offset & Limit**

$users = User::skip(10)->take(5)->get();

**Distinct**

Distinct requires a field for which to return the distinct values.

$users = User::distinct()->get(array('name'));
// or
$users = User::distinct('name')->get();

Distinct can be combined with **where**:

$users = User::where('active', true)->distinct('name')->get();

**Advanced Wheres**

$users = User::where('name', '=', 'John')->orWhere(function($query)
{
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();

**Group By**

Selected columns that are not grouped will be aggregated with the $last function.

$users = Users::groupBy('title')->get(array('title', 'name'));

**Aggregation**

*Aggregations are only available for MongoDB versions greater than 2.2.*

$total = Order::count();
$price = Order::max('price');
$price = Order::min('price');
$price = Order::avg('price');
$total = Order::sum('price');

Aggregations can be combined with **where**:

$sold = Orders::where('sold', true)->sum('price');

**Like**

$user = Comment::where('body', 'like', '%spam%')->get();

**Incrementing or decrementing a value of a column**

Perform increments or decrements (default 1) on specified attributes:

User::where('name', 'John Doe')->increment('age');
User::where('name', 'Jaques')->decrement('weight', 50);

The number of updated objects is returned:

$count = User->increment('age');

You may also specify additional columns to update:

User::where('age', '29')->increment('age', 1, array('group' => 'thirty something'));
User::where('bmi', 30)->decrement('bmi', 1, array('category' => 'overweight'));

### Inserts, updates and deletes

All basic insert, update, delete and select methods should be implemented.

### Dates

Eloquent allows you to work with Carbon/DateTime objects instead of MongoDate objects. Internally, these dates will be converted to MongoDate objects when saved to the database. If you wish to use this functionality on non-default date fields you will need to manually specify them as described here: http://laravel.com/docs/eloquent#date-mutators

Example:

use Jenssegers\Mongodb\Model as Eloquent;

class User extends Eloquent {

protected $dates = array('birthday');

}

Which allows you to execute queries like:

$users = User::where('birthday', '>', new DateTime('-18 years'))->get();

### Relations

Supported relations are:

- hasOne
- hasMany
- belongsTo
- belongsToMany

*The belongsToMany relation will not use a pivot "table", but will push id's to a __related_ids__ attribute instead.*

Example:

use Jenssegers\Mongodb\Model as Eloquent;

class User extends Eloquent {

public function items()
{
return $this->hasMany('Item');
}

}

And the inverse relation:

use Jenssegers\Mongodb\Model as Eloquent;

class Item extends Eloquent {

public function user()
{
return $this->belongsTo('User');
}

}

Other relations are not yet supported, but may be added in the future. Read more about these relations on http://four.laravel.com/docs/eloquent#relationships

### Raw Expressions

These expressions will be injected directly into the query.

User::whereRaw(array('age' => array('$gt' => 30, '$lt' => 40)))->get();

You can also perform raw expressions on the internal MongoCollection object, note that this will return the original response, and not a collection of models.

User::raw(function($collection)
{
return $collection->find();
});

Or you can access the internal MongoCollection object directly:

User::raw()->find();

The MongoClient and MongoDB objects can be accessed like this:

$client = DB::getMongoClient();
$db = DB::getMongoDB();

### MongoDB specific operations

**Upsert**

Update or insert a document. Additional options for the update method are passed directly to the native update method.

DB::collection('users')->where('name', 'John')
->update($data, array('upsert' => true));

**Push**

Add an items to an array.

DB::collection('users')->where('name', 'John')->push('items', 'boots');
DB::collection('users')->where('name', 'John')->push('messages', array('from' => 'Jane Doe', 'message' => 'Hi John'));

**Pull**

Remove an item from an array.

DB::collection('users')->where('name', 'John')->pull('items', 'boots');
DB::collection('users')->where('name', 'John')->pull('messages', array('from' => 'Jane Doe', 'message' => 'Hi John'));

**Unset**

Remove one or more fields from a document.

DB::collection('users')->where('name', 'John')->unset('note');

You can also perform an unset on a model.

$user = User::where('name', 'John')->first();
$user->unset('note');

### Query Caching

You may easily cache the results of a query using the remember method:

$users = User::remember(10)->get();

*From: http://laravel.com/docs/queries#caching-queries*

### Query Logging

By default, Laravel keeps a log in memory of all queries that have been run for the current request. However, in some cases, such as when inserting a large number of rows, this can cause the application to use excess memory. To disable the log, you may use the `disableQueryLog` method:

DB::connection()->disableQueryLog();

*From: http://laravel.com/docs/database#query-logging*