From 3d6192a5019f52858fa22ea3640f2a0fac05599b Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 09:52:03 +0000 Subject: [PATCH 01/60] Readme updated to show belongsToMany relationship availability --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index aebec2951..005f9ba73 100644 --- a/README.md +++ b/README.md @@ -266,6 +266,7 @@ Supported relations are: - hasOne - hasMany - belongsTo + - belongsToMany Example: From da047546326e303dc979edeedea6c226d822b8d9 Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Wed, 20 Nov 2013 22:35:25 +0100 Subject: [PATCH 02/60] Slimming down the BelongsToMany class --- .../Mongodb/Relations/BelongsToMany.php | 188 ++-------- tests/RelationsTest.php | 334 +++++++++--------- 2 files changed, 187 insertions(+), 335 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index a0aff091e..d09967c05 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -1,4 +1,5 @@ getSelectColumns($columns); - - $models = $this->query->addSelect($select)->getModels(); - - // If we actually found models we will also eager load any relationships that - // have been specified as needing to be eager loaded. This will solve the - // n + 1 query problem for the developer and also increase performance. - if (count($models) > 0) - { - $models = $this->query->eagerLoadRelations($models); - } - - return $this->related->newCollection($models); + // Do nothing } - + /** * Set the select clause for the relation query. * @@ -42,26 +28,6 @@ protected function getSelectColumns(array $columns = array('*')) return $columns; } - /** - * Get a paginator for the "select" statement. - * - * @param int $perPage - * @param array $columns - * @return \Illuminate\Pagination\Paginator - */ - public function paginate($perPage = null, $columns = array('*')) - { - $this->query->addSelect($this->getSelectColumns($columns)); - - // When paginating results, we need to add the pivot columns to the query and - // then hydrate into the pivot objects once the results have been gathered - // from the database since this isn't performed by the Eloquent builder. - $pager = $this->query->paginate($perPage, $columns); - - return $pager; - } - - /** * Set the base constraints on the relation query. * @@ -69,7 +35,7 @@ public function paginate($perPage = null, $columns = array('*')) */ public function addConstraints() { - if (static::$constraints) + if (static::$constraints) { // Make sure that the primary key of the parent // is in the relationship array of keys @@ -77,55 +43,6 @@ public function addConstraints() } } - /** - * Set the constraints for an eager load of the relation. - * - * @param array $models - * @return void - */ - public function addEagerConstraints(array $models) - { - $this->query->whereIn($this->getForeignKey(), $this->getKeys($models)); - } - - /** - * Save a new model and attach it to the parent model. - * - * @param \Illuminate\Database\Eloquent\Model $model - * @param array $joining - * @param bool $touch - * @return \Illuminate\Database\Eloquent\Model - */ - public function save(Model $model, array $joining = array(), $touch = true) - { - $model->save(array('touch' => false)); - - $this->attach($model->getKey(), $joining, $touch); - - return $model; - } - - /** - * Create a new instance of the related model. - * - * @param array $attributes - * @param array $joining - * @param bool $touch - * @return \Illuminate\Database\Eloquent\Model - */ - public function create(array $attributes, array $joining = array(), $touch = true) - { - $instance = $this->related->newInstance($attributes); - - // Save the new instance before we attach it to other models - $instance->save(array('touch' => false)); - - // Attach to the parent instance - $this->attach($instance->_id, $attributes, $touch); - - return $instance; - } - /** * Sync the intermediate tables with a list of IDs. * @@ -139,13 +56,13 @@ public function sync(array $ids, $detaching = true) // in this joining table. We'll spin through the given IDs, checking to see // if they exist in the array of current ones, and if not we will insert. $current = $this->parent->{$this->otherKey}; - + // Check if the current array exists or not on the parent model and create it // if it does not exist if (is_null($current)) $current = array(); $records = $this->formatSyncList($ids); - + $detach = array_diff($current, array_keys($records)); // Next, we will take the differences of the currents and given IDs and detach @@ -164,29 +81,6 @@ public function sync(array $ids, $detaching = true) $this->touchIfTouching(); } - /** - * Format the sync list so that it is keyed by ID. - * - * @param array $records - * @return array - */ - protected function formatSyncList(array $records) - { - $results = array(); - - foreach ($records as $id => $attributes) - { - if ( ! is_array($attributes)) - { - list($id, $attributes) = array($attributes, array()); - } - - $results[$id] = $attributes; - } - - return $results; - } - /** * Attach all of the IDs that aren't in the current array. * @@ -220,25 +114,25 @@ protected function attachNew(array $records, array $current, $touch = true) public function attach($id, array $attributes = array(), $touch = true) { if ($id instanceof Model) $id = $id->getKey(); - + // Generate a new parent query instance $parent = $this->newParentQuery(); - + // Generate a new related query instance $related = $this->related->newInstance(); - + // Set contraints on the related query $related = $related->where($this->related->getKeyName(), $id); $records = $this->createAttachRecords((array) $id, $attributes); - + // Get the ID's to attach to the two documents $otherIds = array_pluck($records, $this->otherKey); $foreignIds = array_pluck($records, $this->foreignKey); // Attach to the parent model $parent->push($this->otherKey, $otherIds[0])->update(array()); - + // Attach to the related model $related->push($this->foreignKey, $foreignIds[0])->update(array()); } @@ -296,54 +190,22 @@ public function detach($ids = array(), $touch = true) { $query->pull($this->otherKey, $id); } - - return count($ids); - } - - /** - * If we're touching the parent model, touch. - * - * @return void - */ - public function touchIfTouching() - { - if ($this->touchingParent()) $this->getParent()->touch(); - if ($this->getParent()->touches($this->relationName)) $this->touch(); - } - - /** - * Determine if we should touch the parent on sync. - * - * @return bool - */ - protected function touchingParent() - { - return $this->getRelated()->touches($this->guessInverseRelation()); - } - - /** - * Attempt to guess the name of the inverse of the relation. - * - * @return string - */ - protected function guessInverseRelation() - { - return strtolower(str_plural(class_basename($this->getParent()))); + return count($ids); } /** * Create a new query builder for the parent - * + * * @return Jenssegers\Mongodb\Builder */ protected function newParentQuery() { $query = $this->parent->newQuery(); - + return $query->where($this->parent->getKeyName(), '=', $this->parent->getKey()); } - + /** * Build model dictionary keyed by the relation's foreign key. * @@ -370,16 +232,6 @@ protected function buildDictionary(Collection $results) return $dictionary; } - /** - * Get the related model's updated at column name. - * - * @return string - */ - public function getRelatedFreshUpdate() - { - return array($this->related->getUpdatedAtColumn() => $this->related->freshTimestamp()); - } - /** * Get the fully qualified foreign key for the relation. * @@ -399,4 +251,4 @@ public function getOtherKey() { return $this->otherKey; } -} \ No newline at end of file +} diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index 2b9c0bef0..6492ecf33 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -2,214 +2,214 @@ class RelationsTest extends PHPUnit_Framework_TestCase { - public function setUp() { + public function setUp() { } public function tearDown() { - User::truncate(); - Book::truncate(); - Item::truncate(); - Role::truncate(); - Client::truncate(); + User::truncate(); + Book::truncate(); + Item::truncate(); + Role::truncate(); + Client::truncate(); } public function testHasMany() { - $author = User::create(array('name' => 'George R. R. Martin')); - Book::create(array('title' => 'A Game of Thrones', 'author_id' => $author->_id)); - Book::create(array('title' => 'A Clash of Kings', 'author_id' => $author->_id)); + $author = User::create(array('name' => 'George R. R. Martin')); + Book::create(array('title' => 'A Game of Thrones', 'author_id' => $author->_id)); + Book::create(array('title' => 'A Clash of Kings', 'author_id' => $author->_id)); - $books = $author->books; - $this->assertEquals(2, count($books)); + $books = $author->books; + $this->assertEquals(2, count($books)); - $user = User::create(array('name' => 'John Doe')); - Item::create(array('type' => 'knife', 'user_id' => $user->_id)); - Item::create(array('type' => 'shield', 'user_id' => $user->_id)); - Item::create(array('type' => 'sword', 'user_id' => $user->_id)); - Item::create(array('type' => 'bag', 'user_id' => null)); + $user = User::create(array('name' => 'John Doe')); + Item::create(array('type' => 'knife', 'user_id' => $user->_id)); + Item::create(array('type' => 'shield', 'user_id' => $user->_id)); + Item::create(array('type' => 'sword', 'user_id' => $user->_id)); + Item::create(array('type' => 'bag', 'user_id' => null)); - $items = $user->items; - $this->assertEquals(3, count($items)); - } + $items = $user->items; + $this->assertEquals(3, count($items)); +} public function testBelongsTo() { - $user = User::create(array('name' => 'George R. R. Martin')); - Book::create(array('title' => 'A Game of Thrones', 'author_id' => $user->_id)); - $book = Book::create(array('title' => 'A Clash of Kings', 'author_id' => $user->_id)); + $user = User::create(array('name' => 'George R. R. Martin')); + Book::create(array('title' => 'A Game of Thrones', 'author_id' => $user->_id)); + $book = Book::create(array('title' => 'A Clash of Kings', 'author_id' => $user->_id)); - $author = $book->author; - $this->assertEquals('George R. R. Martin', $author->name); + $author = $book->author; + $this->assertEquals('George R. R. Martin', $author->name); - $user = User::create(array('name' => 'John Doe')); - $item = Item::create(array('type' => 'sword', 'user_id' => $user->_id)); + $user = User::create(array('name' => 'John Doe')); + $item = Item::create(array('type' => 'sword', 'user_id' => $user->_id)); - $owner = $item->user; - $this->assertEquals('John Doe', $owner->name); + $owner = $item->user; + $this->assertEquals('John Doe', $owner->name); } public function testHasOne() { - $user = User::create(array('name' => 'John Doe')); - Role::create(array('type' => 'admin', 'user_id' => $user->_id)); + $user = User::create(array('name' => 'John Doe')); + Role::create(array('type' => 'admin', 'user_id' => $user->_id)); - $role = $user->role; - $this->assertEquals('admin', $role->type); + $role = $user->role; + $this->assertEquals('admin', $role->type); } public function testWithBelongsTo() { - $user = User::create(array('name' => 'John Doe')); - Item::create(array('type' => 'knife', 'user_id' => $user->_id)); - Item::create(array('type' => 'shield', 'user_id' => $user->_id)); - Item::create(array('type' => 'sword', 'user_id' => $user->_id)); - Item::create(array('type' => 'bag', 'user_id' => null)); - - $items = Item::with('user')->get(); - - $user = $items[0]->getRelation('user'); - $this->assertInstanceOf('User', $user); - $this->assertEquals('John Doe', $user->name); - $this->assertEquals(1, count($items[0]->getRelations())); - $this->assertEquals(null, $items[3]->getRelation('user')); + $user = User::create(array('name' => 'John Doe')); + Item::create(array('type' => 'knife', 'user_id' => $user->_id)); + Item::create(array('type' => 'shield', 'user_id' => $user->_id)); + Item::create(array('type' => 'sword', 'user_id' => $user->_id)); + Item::create(array('type' => 'bag', 'user_id' => null)); + + $items = Item::with('user')->get(); + + $user = $items[0]->getRelation('user'); + $this->assertInstanceOf('User', $user); + $this->assertEquals('John Doe', $user->name); + $this->assertEquals(1, count($items[0]->getRelations())); + $this->assertEquals(null, $items[3]->getRelation('user')); } public function testWithHashMany() { - $user = User::create(array('name' => 'John Doe')); - Item::create(array('type' => 'knife', 'user_id' => $user->_id)); - Item::create(array('type' => 'shield', 'user_id' => $user->_id)); - Item::create(array('type' => 'sword', 'user_id' => $user->_id)); - Item::create(array('type' => 'bag', 'user_id' => null)); + $user = User::create(array('name' => 'John Doe')); + Item::create(array('type' => 'knife', 'user_id' => $user->_id)); + Item::create(array('type' => 'shield', 'user_id' => $user->_id)); + Item::create(array('type' => 'sword', 'user_id' => $user->_id)); + Item::create(array('type' => 'bag', 'user_id' => null)); - $user = User::with('items')->find($user->_id); + $user = User::with('items')->find($user->_id); - $items = $user->getRelation('items'); - $this->assertEquals(3, count($items)); - $this->assertInstanceOf('Item', $items[0]); + $items = $user->getRelation('items'); + $this->assertEquals(3, count($items)); + $this->assertInstanceOf('Item', $items[0]); } public function testWithHasOne() { - $user = User::create(array('name' => 'John Doe')); - Role::create(array('type' => 'admin', 'user_id' => $user->_id)); - Role::create(array('type' => 'guest', 'user_id' => $user->_id)); + $user = User::create(array('name' => 'John Doe')); + Role::create(array('type' => 'admin', 'user_id' => $user->_id)); + Role::create(array('type' => 'guest', 'user_id' => $user->_id)); - $user = User::with('role')->find($user->_id); + $user = User::with('role')->find($user->_id); - $role = $user->getRelation('role'); - $this->assertInstanceOf('Role', $role); - $this->assertEquals('admin', $role->type); + $role = $user->getRelation('role'); + $this->assertInstanceOf('Role', $role); + $this->assertEquals('admin', $role->type); } public function testEasyRelation() { - // Has Many - $user = User::create(array('name' => 'John Doe')); - $item = Item::create(array('type' => 'knife')); - $user->items()->save($item); - - $user = User::find($user->_id); - $items = $user->items; - $this->assertEquals(1, count($items)); - $this->assertInstanceOf('Item', $items[0]); - - // Has one - $user = User::create(array('name' => 'John Doe')); - $role = Role::create(array('type' => 'admin')); - $user->role()->save($role); - - $user = User::find($user->_id); - $role = $user->role; - $this->assertInstanceOf('Role', $role); - $this->assertEquals('admin', $role->type); + // Has Many + $user = User::create(array('name' => 'John Doe')); + $item = Item::create(array('type' => 'knife')); + $user->items()->save($item); + + $user = User::find($user->_id); + $items = $user->items; + $this->assertEquals(1, count($items)); + $this->assertInstanceOf('Item', $items[0]); + + // Has one + $user = User::create(array('name' => 'John Doe')); + $role = Role::create(array('type' => 'admin')); + $user->role()->save($role); + + $user = User::find($user->_id); + $role = $user->role; + $this->assertInstanceOf('Role', $role); + $this->assertEquals('admin', $role->type); + } + + public function testHasManyAndBelongsTo() + { + $user = User::create(array('name' => 'John Doe')); + + $user->clients()->save(new Client(array('name' => 'Pork Pies Ltd.'))); + $user->clients()->create(array('name' => 'Buffet Bar Inc.')); + + $user = User::with('clients')->find($user->_id); + + $client = Client::with('users')->first(); + + $clients = $client->getRelation('users'); + $users = $user->getRelation('clients'); + + $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $users); + $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $clients); + $this->assertInstanceOf('Client', $users[0]); + $this->assertInstanceOf('User', $clients[0]); + $this->assertCount(2, $user->clients); + $this->assertCount(1, $client->users); + + // Now create a new user to an existing client + $client->users()->create(array('name' => 'Jane Doe')); + + $otherClient = User::where('name', '=', 'Jane Doe')->first()->clients()->get(); + + $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $otherClient); + $this->assertInstanceOf('Client', $otherClient[0]); + $this->assertCount(1, $otherClient); + + // Now attach an existing client to an existing user + $user = User::where('name', '=', 'Jane Doe')->first(); + $client = Client::Where('name', '=', 'Buffet Bar Inc.')->first(); + + // Check the models are what they should be + $this->assertInstanceOf('Client', $client); + $this->assertInstanceOf('User', $user); + + // Assert they are not attached + $this->assertFalse(in_array($client->_id, $user->client_ids)); + $this->assertFalse(in_array($user->_id, $client->user_ids)); + + // Attach the client to the user + $user->clients()->attach($client); + + // Get the new user model + $user = User::where('name', '=', 'Jane Doe')->first(); + $client = Client::Where('name', '=', 'Buffet Bar Inc.')->first(); + + // Assert they are attached + $this->assertTrue(in_array($client->_id, $user->client_ids)); + $this->assertTrue(in_array($user->_id, $client->user_ids)); + } + + public function testHasManyAndBelongsToAttachesExistingModels() + { + $user = User::create(array('name' => 'John Doe', 'client_ids' => array('1234523'))); + + $clients = array( + Client::create(array('name' => 'Pork Pies Ltd.'))->_id, + Client::create(array('name' => 'Buffet Bar Inc.'))->_id + ); + + $moreClients = array( + Client::create(array('name' => 'Boloni Ltd.'))->_id, + Client::create(array('name' => 'Meatballs Inc.'))->_id + ); + + // Sync multiple records + $user->clients()->sync($clients); + + $user = User::with('clients')->find($user->_id); + + // Assert non attached ID's are detached succesfully + $this->assertFalse(in_array('1234523', $user->client_ids)); + + // Assert there are two client objects in the relationship + $this->assertCount(2, $user->clients); + + $user->clients()->sync($moreClients); + + $user = User::with('clients')->find($user->_id); + + // Assert there are now 4 client objects in the relationship + $this->assertCount(4, $user->clients); } - - public function testHasManyAndBelongsTo() - { - $user = User::create(array('name' => 'John Doe')); - - $user->clients()->save(new Client(array('name' => 'Pork Pies Ltd.'))); - $user->clients()->create(array('name' => 'Buffet Bar Inc.')); - - $user = User::with('clients')->find($user->_id); - - $client = Client::with('users')->first(); - - $clients = $client->getRelation('users'); - $users = $user->getRelation('clients'); - - $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $users); - $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $clients); - $this->assertInstanceOf('Client', $users[0]); - $this->assertInstanceOf('User', $clients[0]); - $this->assertCount(2, $user->clients); - $this->assertCount(1, $client->users); - - // Now create a new user to an existing client - $client->users()->create(array('name' => 'Jane Doe')); - - $otherClient = User::where('name', '=', 'Jane Doe')->first()->clients()->get(); - - $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $otherClient); - $this->assertInstanceOf('Client', $otherClient[0]); - $this->assertCount(1, $otherClient); - - // Now attach an existing client to an existing user - $user = User::where('name', '=', 'Jane Doe')->first(); - $client = Client::Where('name', '=', 'Buffet Bar Inc.')->first(); - - // Check the models are what they should be - $this->assertInstanceOf('Client', $client); - $this->assertInstanceOf('User', $user); - - // Assert they are not attached - $this->assertFalse(in_array($client->_id, $user->client_ids)); - $this->assertFalse(in_array($user->_id, $client->user_ids)); - - // Attach the client to the user - $user->clients()->attach($client); - - // Get the new user model - $user = User::where('name', '=', 'Jane Doe')->first(); - $client = Client::Where('name', '=', 'Buffet Bar Inc.')->first(); - - // Assert they are attached - $this->assertTrue(in_array($client->_id, $user->client_ids)); - $this->assertTrue(in_array($user->_id, $client->user_ids)); - } - - public function testHasManyAndBelongsToAttachesExistingModels() - { - $user = User::create(array('name' => 'John Doe', 'client_ids' => array('1234523'))); - - $clients = array( - Client::create(array('name' => 'Pork Pies Ltd.'))->_id, - Client::create(array('name' => 'Buffet Bar Inc.'))->_id - ); - - $moreClients = array( - Client::create(array('name' => 'Boloni Ltd.'))->_id, - Client::create(array('name' => 'Meatballs Inc.'))->_id - ); - - // Sync multiple records - $user->clients()->sync($clients); - - $user = User::with('clients')->find($user->_id); - - // Assert non attached ID's are detached succesfully - $this->assertFalse(in_array('1234523', $user->client_ids)); - - // Assert there are two client objects in the relationship - $this->assertCount(2, $user->clients); - - $user->clients()->sync($moreClients); - - $user = User::with('clients')->find($user->_id); - - // Assert there are now 4 client objects in the relationship - $this->assertCount(4, $user->clients); - } } From 485c25af35d27d19a78e193945165c01f0e4c42e Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Wed, 20 Nov 2013 22:43:43 +0100 Subject: [PATCH 03/60] Tweaking BelongsToMany namespaces --- src/Jenssegers/Mongodb/Relations/BelongsToMany.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index d09967c05..58ba8e58f 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -1,8 +1,7 @@ Date: Thu, 21 Nov 2013 10:42:01 +0100 Subject: [PATCH 04/60] Small readme update for belongsToMany --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 005f9ba73..5766bfa4a 100644 --- a/README.md +++ b/README.md @@ -268,6 +268,8 @@ 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; From 293fa5c37e14bdc4244591da2529adab3be4d2e0 Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Thu, 21 Nov 2013 10:43:32 +0100 Subject: [PATCH 05/60] Markdown fixes --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 5766bfa4a..ce88b8144 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,6 @@ 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`: @@ -268,7 +267,7 @@ 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.* +*The belongsToMany relation will not use a pivot "table", but will push id's to a __related_ids__ attribute instead.* Example: From e9b83e56c07b91c2050934903fba1e5fece1c0c5 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 10:33:35 +0000 Subject: [PATCH 06/60] conflicts --- .settings/org.eclipse.core.resources.prefs | 2 + README.md | 5 +- README.md.conflict | 370 +++++++++++++++++++++ 3 files changed, 374 insertions(+), 3 deletions(-) create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 README.md.conflict diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 000000000..8a34f2ce5 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/README.md=UTF-8 diff --git a/README.md b/README.md index ce88b8144..ef7cbcc2d 100644 --- a/README.md +++ b/README.md @@ -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`: @@ -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; @@ -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* \ No newline at end of file diff --git a/README.md.conflict b/README.md.conflict new file mode 100644 index 000000000..ce88b8144 --- /dev/null +++ b/README.md.conflict @@ -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* From cc77faea2b8d7aa36c78b5797fb5bc8b1f370b17 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 10:36:35 +0000 Subject: [PATCH 07/60] removed conflict --- README.md.conflict | 370 --------------------------------------------- 1 file changed, 370 deletions(-) delete mode 100644 README.md.conflict diff --git a/README.md.conflict b/README.md.conflict deleted file mode 100644 index ce88b8144..000000000 --- a/README.md.conflict +++ /dev/null @@ -1,370 +0,0 @@ -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* From de0d88906b053ca4f368f72eaa32de0d51329f53 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 11:16:19 +0000 Subject: [PATCH 08/60] removed settings --- .settings/org.eclipse.core.resources.prefs | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 .settings/org.eclipse.core.resources.prefs diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index 8a34f2ce5..000000000 --- a/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -encoding/README.md=UTF-8 From fd5a4702633a696f4766f3fb14ab029b35741140 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 11:20:08 +0000 Subject: [PATCH 09/60] updated gitgnore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a4a4579c3..afe97b1e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .DS_Store phpunit.phar /vendor +/.settings composer.phar composer.lock *.sublime-project From 3b9b81ac796a506e4dd7dfee7c4cd7660e56e67a Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 25 Nov 2013 13:18:41 +0000 Subject: [PATCH 10/60] README update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ce88b8144..6bf6045f4 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Add the package to your `composer.json` and run `composer update`. "require": { "jenssegers/mongodb": "*" } - } + } Add the service provider in `app/config/app.php`: From e378c96565cf0105f81f44001803140906ae0080 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 09:52:03 +0000 Subject: [PATCH 11/60] Readme updated to show belongsToMany relationship availability --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6bf6045f4..ce88b8144 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Add the package to your `composer.json` and run `composer update`. "require": { "jenssegers/mongodb": "*" } - } + } Add the service provider in `app/config/app.php`: From c797abdb34f935015cb83c44cd9cd776a2053ecd Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Thu, 21 Nov 2013 10:42:01 +0100 Subject: [PATCH 12/60] Small readme update for belongsToMany --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ce88b8144..27a4ff937 100644 --- a/README.md +++ b/README.md @@ -267,7 +267,7 @@ 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.* +*The belongsToMany relation will not use a pivot "table", but will push id's to a **related_ids** attribute instead.* Example: From 46cbc34e58b5fc7981c54d83490be398b763f01b Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Thu, 21 Nov 2013 10:43:32 +0100 Subject: [PATCH 13/60] Markdown fixes --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 27a4ff937..ce88b8144 100644 --- a/README.md +++ b/README.md @@ -267,7 +267,7 @@ 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.* +*The belongsToMany relation will not use a pivot "table", but will push id's to a __related_ids__ attribute instead.* Example: From fb77334e0e0c98bc3dd2e9e977a1ee57f4438833 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 10:33:35 +0000 Subject: [PATCH 14/60] conflicts --- .settings/org.eclipse.core.resources.prefs | 2 + README.md | 5 +- README.md.conflict | 370 +++++++++++++++++++++ 3 files changed, 374 insertions(+), 3 deletions(-) create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 README.md.conflict diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 000000000..8a34f2ce5 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/README.md=UTF-8 diff --git a/README.md b/README.md index ce88b8144..ef7cbcc2d 100644 --- a/README.md +++ b/README.md @@ -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`: @@ -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; @@ -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* \ No newline at end of file diff --git a/README.md.conflict b/README.md.conflict new file mode 100644 index 000000000..ce88b8144 --- /dev/null +++ b/README.md.conflict @@ -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* From d7da34bc5e36a3cbabed2318780d794d3367fd84 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 10:36:35 +0000 Subject: [PATCH 15/60] removed conflict --- README.md.conflict | 370 --------------------------------------------- 1 file changed, 370 deletions(-) delete mode 100644 README.md.conflict diff --git a/README.md.conflict b/README.md.conflict deleted file mode 100644 index ce88b8144..000000000 --- a/README.md.conflict +++ /dev/null @@ -1,370 +0,0 @@ -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* From 754216e65271ca37605220b875790637f7c48a42 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 11:16:19 +0000 Subject: [PATCH 16/60] removed settings --- .settings/org.eclipse.core.resources.prefs | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 .settings/org.eclipse.core.resources.prefs diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index 8a34f2ce5..000000000 --- a/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -encoding/README.md=UTF-8 From 8d58aa0b2ee0fd01636af0e77f67488f66f5809f Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 11:20:08 +0000 Subject: [PATCH 17/60] updated gitgnore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a4a4579c3..afe97b1e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .DS_Store phpunit.phar /vendor +/.settings composer.phar composer.lock *.sublime-project From 4885879f03776176c9716cb21311aaae328670e0 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 27 Nov 2013 16:13:17 +0000 Subject: [PATCH 18/60] Fixed bug where related model doesn't have parent _id removed on sync --- .../Mongodb/Relations/BelongsToMany.php | 21 +++++++++++++++++-- tests/RelationsTest.php | 10 +++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index 58ba8e58f..7033dcd7a 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -167,9 +167,16 @@ protected function createAttachRecords($ids, array $attributes) public function detach($ids = array(), $touch = true) { if ($ids instanceof Model) $ids = (array) $ids->getKey(); - + + // Generate a new parent query $query = $this->newParentQuery(); - + + // Generate a new related query instance + $related = $this->related->newInstance(); + + // Remove the relation from the related model + $related->pull($this->foreignKey, $this->parent->getKey()); + // If associated IDs were passed to the method we will only delete those // associations, otherwise all of the association ties will be broken. // We'll return the numbers of affected rows when we do the deletes. @@ -192,6 +199,16 @@ public function detach($ids = array(), $touch = true) return count($ids); } + + protected function detachRelation() + { + + } + + protected function detachParent() + { + + } /** * Create a new query builder for the parent diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index 6492ecf33..c6b16fbfa 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -190,8 +190,8 @@ public function testHasManyAndBelongsToAttachesExistingModels() ); $moreClients = array( - Client::create(array('name' => 'Boloni Ltd.'))->_id, - Client::create(array('name' => 'Meatballs Inc.'))->_id + Client::create(array('name' => 'synced Boloni Ltd.'))->_id, + Client::create(array('name' => 'synced Meatballs Inc.'))->_id ); // Sync multiple records @@ -209,7 +209,9 @@ public function testHasManyAndBelongsToAttachesExistingModels() $user = User::with('clients')->find($user->_id); - // Assert there are now 4 client objects in the relationship - $this->assertCount(4, $user->clients); + // Assert there are now still 2 client objects in the relationship + $this->assertCount(2, $user->clients); + $this->assertStringStartsWith('synced', $user->clients[0]->name); + $this->assertStringStartsWith('synced', $user->clients[1]->name); } } From 706a3b8a1ac351bf3a13bb18d9aa701817309837 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 09:52:03 +0000 Subject: [PATCH 19/60] Readme updated to show belongsToMany relationship availability --- README.md.orig | 373 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 373 insertions(+) create mode 100644 README.md.orig diff --git a/README.md.orig b/README.md.orig new file mode 100644 index 000000000..1754319a3 --- /dev/null +++ b/README.md.orig @@ -0,0 +1,373 @@ +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 +<<<<<<< HEAD + +*The belongsToMany relation will not use a pivot "table", but will push id's to a __related_ids__ attribute instead.* +======= +>>>>>>> Readme updated to show belongsToMany relationship availability + +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* From 4351a13c41e7cb4a76cdfd6d148e6e7db61553db Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Thu, 21 Nov 2013 10:42:01 +0100 Subject: [PATCH 20/60] Small readme update for belongsToMany --- README.md.orig | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md.orig b/README.md.orig index 1754319a3..6f8feb381 100644 --- a/README.md.orig +++ b/README.md.orig @@ -266,11 +266,12 @@ Supported relations are: - hasMany - belongsTo - belongsToMany -<<<<<<< HEAD +<<<<<<< HEAD *The belongsToMany relation will not use a pivot "table", but will push id's to a __related_ids__ attribute instead.* ======= ->>>>>>> Readme updated to show belongsToMany relationship availability +*The belongsToMany relation will not use a pivot "table", but will push id's to a **related_ids** attribute instead.* +>>>>>>> Small readme update for belongsToMany Example: From 63ea2457a50d0d218be428643d8a6187940c6396 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 10:33:35 +0000 Subject: [PATCH 21/60] conflicts --- .settings/org.eclipse.core.resources.prefs | 2 + README.md | 5 +- README.md.conflict | 370 +++++++++++++++++++++ 3 files changed, 374 insertions(+), 3 deletions(-) create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 README.md.conflict diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 000000000..8a34f2ce5 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/README.md=UTF-8 diff --git a/README.md b/README.md index ce88b8144..ef7cbcc2d 100644 --- a/README.md +++ b/README.md @@ -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`: @@ -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; @@ -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* \ No newline at end of file diff --git a/README.md.conflict b/README.md.conflict new file mode 100644 index 000000000..ce88b8144 --- /dev/null +++ b/README.md.conflict @@ -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* From a874d19f01595a97d862c0fce908374d403c7c7a Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 10:36:35 +0000 Subject: [PATCH 22/60] removed conflict --- README.md.conflict | 370 --------------------------------------------- 1 file changed, 370 deletions(-) delete mode 100644 README.md.conflict diff --git a/README.md.conflict b/README.md.conflict deleted file mode 100644 index ce88b8144..000000000 --- a/README.md.conflict +++ /dev/null @@ -1,370 +0,0 @@ -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* From 680114905e158e38290d22b57d45c7cace107e2e Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 11:16:19 +0000 Subject: [PATCH 23/60] removed settings --- .settings/org.eclipse.core.resources.prefs | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 .settings/org.eclipse.core.resources.prefs diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index 8a34f2ce5..000000000 --- a/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -encoding/README.md=UTF-8 From be2e9562e044dc73726ecd143c8b5702be9cbea9 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 11:20:08 +0000 Subject: [PATCH 24/60] updated gitgnore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a4a4579c3..afe97b1e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .DS_Store phpunit.phar /vendor +/.settings composer.phar composer.lock *.sublime-project From 6a7d264185d1acd6f239224c6675b367d7ba406d Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 25 Nov 2013 13:18:41 +0000 Subject: [PATCH 25/60] README update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ef7cbcc2d..b87f2409c 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Add the package to your `composer.json` and run `composer update`. "require": { "jenssegers/mongodb": "*" } - } + } Add the service provider in `app/config/app.php`: From 9b9bd7c205bae660fbe5493ee4a299921ab46db6 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 09:52:03 +0000 Subject: [PATCH 26/60] Readme updated to show belongsToMany relationship availability --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b87f2409c..ef7cbcc2d 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Add the package to your `composer.json` and run `composer update`. "require": { "jenssegers/mongodb": "*" } - } + } Add the service provider in `app/config/app.php`: From b3ce53b0bf3983f791ad293a297399dc3b4d1289 Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Thu, 21 Nov 2013 10:42:01 +0100 Subject: [PATCH 27/60] Small readme update for belongsToMany --- README.md.orig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md.orig b/README.md.orig index 6f8feb381..b82e3ce8f 100644 --- a/README.md.orig +++ b/README.md.orig @@ -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`: @@ -268,11 +269,10 @@ Supported relations are: - belongsToMany <<<<<<< HEAD -*The belongsToMany relation will not use a pivot "table", but will push id's to a __related_ids__ attribute instead.* ======= *The belongsToMany relation will not use a pivot "table", but will push id's to a **related_ids** attribute instead.* ->>>>>>> Small readme update for belongsToMany +>>>>>>> Small readme update for belongsToMany Example: use Jenssegers\Mongodb\Model as Eloquent; From 1fcf12e386be98a09fbb0bb0bc54c533c1f0f516 Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Thu, 21 Nov 2013 10:43:32 +0100 Subject: [PATCH 28/60] Markdown fixes --- README.md.orig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md.orig b/README.md.orig index b82e3ce8f..063dc399e 100644 --- a/README.md.orig +++ b/README.md.orig @@ -270,9 +270,9 @@ Supported relations are: <<<<<<< HEAD ======= -*The belongsToMany relation will not use a pivot "table", but will push id's to a **related_ids** attribute instead.* +*The belongsToMany relation will not use a pivot "table", but will push id's to a __related_ids__ attribute instead.* ->>>>>>> Small readme update for belongsToMany +>>>>>>> Markdown fixes Example: use Jenssegers\Mongodb\Model as Eloquent; From 73edfab0fb1da0127fa7194bcc7c049eebe6904d Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 10:33:35 +0000 Subject: [PATCH 29/60] conflicts --- .settings/org.eclipse.core.resources.prefs | 2 + README.md.conflict | 370 +++++++++++++++++++++ 2 files changed, 372 insertions(+) create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 README.md.conflict diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 000000000..8a34f2ce5 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/README.md=UTF-8 diff --git a/README.md.conflict b/README.md.conflict new file mode 100644 index 000000000..ce88b8144 --- /dev/null +++ b/README.md.conflict @@ -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* From 3c1d0493d56748ce62177222999ae56fa46adc15 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 10:36:35 +0000 Subject: [PATCH 30/60] removed conflict --- README.md.conflict | 370 --------------------------------------------- 1 file changed, 370 deletions(-) delete mode 100644 README.md.conflict diff --git a/README.md.conflict b/README.md.conflict deleted file mode 100644 index ce88b8144..000000000 --- a/README.md.conflict +++ /dev/null @@ -1,370 +0,0 @@ -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* From bd5c147e26d2801331eab6295915e04d65d9e9d5 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 11:16:19 +0000 Subject: [PATCH 31/60] removed settings --- .settings/org.eclipse.core.resources.prefs | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 .settings/org.eclipse.core.resources.prefs diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index 8a34f2ce5..000000000 --- a/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -encoding/README.md=UTF-8 From 42dcd24de018933577b77dfa89a9e301142b2b42 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 27 Nov 2013 16:13:17 +0000 Subject: [PATCH 32/60] Fixed bug where related model doesn't have parent _id removed on sync --- .../Mongodb/Relations/BelongsToMany.php | 21 +++++++++++++++++-- tests/RelationsTest.php | 10 +++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index 58ba8e58f..7033dcd7a 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -167,9 +167,16 @@ protected function createAttachRecords($ids, array $attributes) public function detach($ids = array(), $touch = true) { if ($ids instanceof Model) $ids = (array) $ids->getKey(); - + + // Generate a new parent query $query = $this->newParentQuery(); - + + // Generate a new related query instance + $related = $this->related->newInstance(); + + // Remove the relation from the related model + $related->pull($this->foreignKey, $this->parent->getKey()); + // If associated IDs were passed to the method we will only delete those // associations, otherwise all of the association ties will be broken. // We'll return the numbers of affected rows when we do the deletes. @@ -192,6 +199,16 @@ public function detach($ids = array(), $touch = true) return count($ids); } + + protected function detachRelation() + { + + } + + protected function detachParent() + { + + } /** * Create a new query builder for the parent diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index 6492ecf33..c6b16fbfa 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -190,8 +190,8 @@ public function testHasManyAndBelongsToAttachesExistingModels() ); $moreClients = array( - Client::create(array('name' => 'Boloni Ltd.'))->_id, - Client::create(array('name' => 'Meatballs Inc.'))->_id + Client::create(array('name' => 'synced Boloni Ltd.'))->_id, + Client::create(array('name' => 'synced Meatballs Inc.'))->_id ); // Sync multiple records @@ -209,7 +209,9 @@ public function testHasManyAndBelongsToAttachesExistingModels() $user = User::with('clients')->find($user->_id); - // Assert there are now 4 client objects in the relationship - $this->assertCount(4, $user->clients); + // Assert there are now still 2 client objects in the relationship + $this->assertCount(2, $user->clients); + $this->assertStringStartsWith('synced', $user->clients[0]->name); + $this->assertStringStartsWith('synced', $user->clients[1]->name); } } From b0bd0b280c12dbc1fc50d2606b23f41dbdd7a9fe Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 27 Nov 2013 16:21:01 +0000 Subject: [PATCH 33/60] Deleted merge file --- README.md.orig | 374 ------------------------------------------------- 1 file changed, 374 deletions(-) delete mode 100644 README.md.orig diff --git a/README.md.orig b/README.md.orig deleted file mode 100644 index 063dc399e..000000000 --- a/README.md.orig +++ /dev/null @@ -1,374 +0,0 @@ -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 - -<<<<<<< HEAD -======= -*The belongsToMany relation will not use a pivot "table", but will push id's to a __related_ids__ attribute instead.* - ->>>>>>> Markdown fixes -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* From a9bed9a06078b4fe18e988f95f2d291e479712bf Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 27 Nov 2013 16:28:21 +0000 Subject: [PATCH 34/60] Removed methods --- src/Jenssegers/Mongodb/Relations/BelongsToMany.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index 7033dcd7a..4a1dd3a14 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -199,16 +199,6 @@ public function detach($ids = array(), $touch = true) return count($ids); } - - protected function detachRelation() - { - - } - - protected function detachParent() - { - - } /** * Create a new query builder for the parent From 21a59dc51051ae24badc2d6130d3529a322696d2 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 27 Nov 2013 16:44:50 +0000 Subject: [PATCH 35/60] Readme re-updated --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ef7cbcc2d..88f7370fb 100644 --- a/README.md +++ b/README.md @@ -268,6 +268,8 @@ 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; From 1b7a84ed6aa4b4b247d98bb88c48e4e855b66439 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 10:33:35 +0000 Subject: [PATCH 36/60] conflicts --- .settings/org.eclipse.core.resources.prefs | 2 + README.md | 5 +- README.md.conflict | 370 +++++++++++++++++++++ 3 files changed, 374 insertions(+), 3 deletions(-) create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 README.md.conflict diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 000000000..8a34f2ce5 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/README.md=UTF-8 diff --git a/README.md b/README.md index ce88b8144..ef7cbcc2d 100644 --- a/README.md +++ b/README.md @@ -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`: @@ -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; @@ -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* \ No newline at end of file diff --git a/README.md.conflict b/README.md.conflict new file mode 100644 index 000000000..ce88b8144 --- /dev/null +++ b/README.md.conflict @@ -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* From 48b32cffe5a34e1872f0c7c3128681893bd8b86a Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 10:36:35 +0000 Subject: [PATCH 37/60] removed conflict --- README.md.conflict | 370 --------------------------------------------- 1 file changed, 370 deletions(-) delete mode 100644 README.md.conflict diff --git a/README.md.conflict b/README.md.conflict deleted file mode 100644 index ce88b8144..000000000 --- a/README.md.conflict +++ /dev/null @@ -1,370 +0,0 @@ -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* From 0fdebc0fb9a4ee4dbdf663804155a834194e644d Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 11:16:19 +0000 Subject: [PATCH 38/60] removed settings --- .settings/org.eclipse.core.resources.prefs | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 .settings/org.eclipse.core.resources.prefs diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index 8a34f2ce5..000000000 --- a/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -encoding/README.md=UTF-8 From 0213c71ba0e28e49a27b399e7c9abdb2eac1cfda Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 11:20:08 +0000 Subject: [PATCH 39/60] updated gitgnore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a4a4579c3..afe97b1e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .DS_Store phpunit.phar /vendor +/.settings composer.phar composer.lock *.sublime-project From 47d1d588aa69fc4466d50600b2da8c2dcec6acca Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 25 Nov 2013 13:18:41 +0000 Subject: [PATCH 40/60] README update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ef7cbcc2d..b87f2409c 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Add the package to your `composer.json` and run `composer update`. "require": { "jenssegers/mongodb": "*" } - } + } Add the service provider in `app/config/app.php`: From 4a44c39bfa7f2b37af1e8d3b6127dcecbc28f9b4 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 09:52:03 +0000 Subject: [PATCH 41/60] Readme updated to show belongsToMany relationship availability --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b87f2409c..ef7cbcc2d 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Add the package to your `composer.json` and run `composer update`. "require": { "jenssegers/mongodb": "*" } - } + } Add the service provider in `app/config/app.php`: From ceed9ef16c154328345c8bd830476892d3d37612 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 10:33:35 +0000 Subject: [PATCH 42/60] conflicts --- .settings/org.eclipse.core.resources.prefs | 2 + README.md.conflict | 370 +++++++++++++++++++++ 2 files changed, 372 insertions(+) create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 README.md.conflict diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 000000000..8a34f2ce5 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/README.md=UTF-8 diff --git a/README.md.conflict b/README.md.conflict new file mode 100644 index 000000000..ce88b8144 --- /dev/null +++ b/README.md.conflict @@ -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* From 0a0fa83b017ff23561a8cd28b7e8149fa2a6ceef Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 10:36:35 +0000 Subject: [PATCH 43/60] removed conflict --- README.md.conflict | 370 --------------------------------------------- 1 file changed, 370 deletions(-) delete mode 100644 README.md.conflict diff --git a/README.md.conflict b/README.md.conflict deleted file mode 100644 index ce88b8144..000000000 --- a/README.md.conflict +++ /dev/null @@ -1,370 +0,0 @@ -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* From b865d8a8e77df4dbd1e4a9c613c2cd969454b72e Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 11:16:19 +0000 Subject: [PATCH 44/60] removed settings --- .settings/org.eclipse.core.resources.prefs | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 .settings/org.eclipse.core.resources.prefs diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index 8a34f2ce5..000000000 --- a/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -encoding/README.md=UTF-8 From dae8b718f10573f0b483797f316902ae755f76b7 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 27 Nov 2013 16:13:17 +0000 Subject: [PATCH 45/60] Fixed bug where related model doesn't have parent _id removed on sync --- .../Mongodb/Relations/BelongsToMany.php | 21 +++++++++++++++++-- tests/RelationsTest.php | 10 +++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index 58ba8e58f..7033dcd7a 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -167,9 +167,16 @@ protected function createAttachRecords($ids, array $attributes) public function detach($ids = array(), $touch = true) { if ($ids instanceof Model) $ids = (array) $ids->getKey(); - + + // Generate a new parent query $query = $this->newParentQuery(); - + + // Generate a new related query instance + $related = $this->related->newInstance(); + + // Remove the relation from the related model + $related->pull($this->foreignKey, $this->parent->getKey()); + // If associated IDs were passed to the method we will only delete those // associations, otherwise all of the association ties will be broken. // We'll return the numbers of affected rows when we do the deletes. @@ -192,6 +199,16 @@ public function detach($ids = array(), $touch = true) return count($ids); } + + protected function detachRelation() + { + + } + + protected function detachParent() + { + + } /** * Create a new query builder for the parent diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index 6492ecf33..c6b16fbfa 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -190,8 +190,8 @@ public function testHasManyAndBelongsToAttachesExistingModels() ); $moreClients = array( - Client::create(array('name' => 'Boloni Ltd.'))->_id, - Client::create(array('name' => 'Meatballs Inc.'))->_id + Client::create(array('name' => 'synced Boloni Ltd.'))->_id, + Client::create(array('name' => 'synced Meatballs Inc.'))->_id ); // Sync multiple records @@ -209,7 +209,9 @@ public function testHasManyAndBelongsToAttachesExistingModels() $user = User::with('clients')->find($user->_id); - // Assert there are now 4 client objects in the relationship - $this->assertCount(4, $user->clients); + // Assert there are now still 2 client objects in the relationship + $this->assertCount(2, $user->clients); + $this->assertStringStartsWith('synced', $user->clients[0]->name); + $this->assertStringStartsWith('synced', $user->clients[1]->name); } } From 36cea2dd33d7897c1d679ccdaacb017277547ab3 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 09:52:03 +0000 Subject: [PATCH 46/60] Readme updated to show belongsToMany relationship availability --- README.md.orig | 373 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 373 insertions(+) create mode 100644 README.md.orig diff --git a/README.md.orig b/README.md.orig new file mode 100644 index 000000000..1754319a3 --- /dev/null +++ b/README.md.orig @@ -0,0 +1,373 @@ +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 +<<<<<<< HEAD + +*The belongsToMany relation will not use a pivot "table", but will push id's to a __related_ids__ attribute instead.* +======= +>>>>>>> Readme updated to show belongsToMany relationship availability + +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* From 9428af0c3e8d26aa249b7f6a26b7336c406174bb Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Thu, 21 Nov 2013 10:42:01 +0100 Subject: [PATCH 47/60] Small readme update for belongsToMany --- README.md.orig | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md.orig b/README.md.orig index 1754319a3..6f8feb381 100644 --- a/README.md.orig +++ b/README.md.orig @@ -266,11 +266,12 @@ Supported relations are: - hasMany - belongsTo - belongsToMany -<<<<<<< HEAD +<<<<<<< HEAD *The belongsToMany relation will not use a pivot "table", but will push id's to a __related_ids__ attribute instead.* ======= ->>>>>>> Readme updated to show belongsToMany relationship availability +*The belongsToMany relation will not use a pivot "table", but will push id's to a **related_ids** attribute instead.* +>>>>>>> Small readme update for belongsToMany Example: From d5ee4ced95ced42a56d91c5b5e90b7de4f00241b Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 10:33:35 +0000 Subject: [PATCH 48/60] conflicts --- .settings/org.eclipse.core.resources.prefs | 2 + README.md.conflict | 370 +++++++++++++++++++++ 2 files changed, 372 insertions(+) create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 README.md.conflict diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 000000000..8a34f2ce5 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/README.md=UTF-8 diff --git a/README.md.conflict b/README.md.conflict new file mode 100644 index 000000000..ce88b8144 --- /dev/null +++ b/README.md.conflict @@ -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* From c0ee666b80799d3fe89ff984db68d1be2a67bd7e Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 10:36:35 +0000 Subject: [PATCH 49/60] removed conflict --- README.md.conflict | 370 --------------------------------------------- 1 file changed, 370 deletions(-) delete mode 100644 README.md.conflict diff --git a/README.md.conflict b/README.md.conflict deleted file mode 100644 index ce88b8144..000000000 --- a/README.md.conflict +++ /dev/null @@ -1,370 +0,0 @@ -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* From 1b6093abde8f76188379a8ca38b68ea55f9daa8f Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 11:16:19 +0000 Subject: [PATCH 50/60] removed settings --- .settings/org.eclipse.core.resources.prefs | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 .settings/org.eclipse.core.resources.prefs diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index 8a34f2ce5..000000000 --- a/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -encoding/README.md=UTF-8 From 995710c4e767bd7c4f391c0f26cd5aa35a88bc16 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 25 Nov 2013 13:18:41 +0000 Subject: [PATCH 51/60] README update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ef7cbcc2d..b87f2409c 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Add the package to your `composer.json` and run `composer update`. "require": { "jenssegers/mongodb": "*" } - } + } Add the service provider in `app/config/app.php`: From 29ce2b516796ca4b9d9997ea01b91009190beef2 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 09:52:03 +0000 Subject: [PATCH 52/60] Readme updated to show belongsToMany relationship availability --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b87f2409c..ef7cbcc2d 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Add the package to your `composer.json` and run `composer update`. "require": { "jenssegers/mongodb": "*" } - } + } Add the service provider in `app/config/app.php`: From db121a7f7d4f589b897931fcd2f50816361a9bef Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Thu, 21 Nov 2013 10:42:01 +0100 Subject: [PATCH 53/60] Small readme update for belongsToMany --- README.md.orig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md.orig b/README.md.orig index 6f8feb381..b82e3ce8f 100644 --- a/README.md.orig +++ b/README.md.orig @@ -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`: @@ -268,11 +269,10 @@ Supported relations are: - belongsToMany <<<<<<< HEAD -*The belongsToMany relation will not use a pivot "table", but will push id's to a __related_ids__ attribute instead.* ======= *The belongsToMany relation will not use a pivot "table", but will push id's to a **related_ids** attribute instead.* ->>>>>>> Small readme update for belongsToMany +>>>>>>> Small readme update for belongsToMany Example: use Jenssegers\Mongodb\Model as Eloquent; From 990960ce77f7b9c48d9f1f15b650736e9b6270eb Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Thu, 21 Nov 2013 10:43:32 +0100 Subject: [PATCH 54/60] Markdown fixes --- README.md.orig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md.orig b/README.md.orig index b82e3ce8f..063dc399e 100644 --- a/README.md.orig +++ b/README.md.orig @@ -270,9 +270,9 @@ Supported relations are: <<<<<<< HEAD ======= -*The belongsToMany relation will not use a pivot "table", but will push id's to a **related_ids** attribute instead.* +*The belongsToMany relation will not use a pivot "table", but will push id's to a __related_ids__ attribute instead.* ->>>>>>> Small readme update for belongsToMany +>>>>>>> Markdown fixes Example: use Jenssegers\Mongodb\Model as Eloquent; From 09cb5510a42b6824bd0a0111f50bd42bb60b02e2 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 10:33:35 +0000 Subject: [PATCH 55/60] conflicts --- .settings/org.eclipse.core.resources.prefs | 2 + README.md.conflict | 370 +++++++++++++++++++++ 2 files changed, 372 insertions(+) create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 README.md.conflict diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 000000000..8a34f2ce5 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/README.md=UTF-8 diff --git a/README.md.conflict b/README.md.conflict new file mode 100644 index 000000000..ce88b8144 --- /dev/null +++ b/README.md.conflict @@ -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* From d3266c7c7f9fc697bbe31e37e26d3dd8556a44dd Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 10:36:35 +0000 Subject: [PATCH 56/60] removed conflict --- README.md.conflict | 370 --------------------------------------------- 1 file changed, 370 deletions(-) delete mode 100644 README.md.conflict diff --git a/README.md.conflict b/README.md.conflict deleted file mode 100644 index ce88b8144..000000000 --- a/README.md.conflict +++ /dev/null @@ -1,370 +0,0 @@ -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* From 2657b32c1e6c7e70b449d9eaf781cea137a1aafd Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 21 Nov 2013 11:16:19 +0000 Subject: [PATCH 57/60] removed settings --- .settings/org.eclipse.core.resources.prefs | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 .settings/org.eclipse.core.resources.prefs diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index 8a34f2ce5..000000000 --- a/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -encoding/README.md=UTF-8 From b6cba21bdf9a843aec647d3554dba969c7b9a69f Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 27 Nov 2013 16:21:01 +0000 Subject: [PATCH 58/60] Deleted merge file --- README.md.orig | 374 ------------------------------------------------- 1 file changed, 374 deletions(-) delete mode 100644 README.md.orig diff --git a/README.md.orig b/README.md.orig deleted file mode 100644 index 063dc399e..000000000 --- a/README.md.orig +++ /dev/null @@ -1,374 +0,0 @@ -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 - -<<<<<<< HEAD -======= -*The belongsToMany relation will not use a pivot "table", but will push id's to a __related_ids__ attribute instead.* - ->>>>>>> Markdown fixes -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* From 9b2d939a3a9d02506a784e2afaa1c6e6e37e635e Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 27 Nov 2013 16:28:21 +0000 Subject: [PATCH 59/60] Removed methods --- src/Jenssegers/Mongodb/Relations/BelongsToMany.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php index 7033dcd7a..4a1dd3a14 100644 --- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php +++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php @@ -199,16 +199,6 @@ public function detach($ids = array(), $touch = true) return count($ids); } - - protected function detachRelation() - { - - } - - protected function detachParent() - { - - } /** * Create a new query builder for the parent From c1854933d5d68115198a99c9d08a21944e048d16 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 27 Nov 2013 16:44:50 +0000 Subject: [PATCH 60/60] Readme re-updated --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ef7cbcc2d..88f7370fb 100644 --- a/README.md +++ b/README.md @@ -268,6 +268,8 @@ 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;