Skip to content

Commit f5c230f

Browse files
committed
Merge master
2 parents 72c6444 + 57c0d85 commit f5c230f

28 files changed

+1636
-953
lines changed

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Laravel MongoDB
22
===============
33

4-
[![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) [![Coverage Status](https://coveralls.io/repos/jenssegers/Laravel-MongoDB/badge.png?branch=master)](https://coveralls.io/r/jenssegers/Laravel-MongoDB?branch=master)
4+
[![Latest Stable Version](http://img.shields.io/github/release/jenssegers/laravel-mongodb.svg)](https://packagist.org/packages/jenssegers/mongodb) [![Total Downloads](http://img.shields.io/packagist/dm/jenssegers/mongodb.svg)](https://packagist.org/packages/jenssegers/mongodb) [![Build Status](http://img.shields.io/travis/jenssegers/laravel-mongodb.svg)](https://travis-ci.org/jenssegers/laravel-mongodb) [![Coverage Status](http://img.shields.io/coveralls/jenssegers/laravel-mongodb.svg)](https://coveralls.io/r/jenssegers/laravel-mongodb?branch=master)
55

66
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.*
77

@@ -136,6 +136,11 @@ If you want to use Laravel's native Auth functionality, register this included s
136136

137137
This service provider will slightly modify the internal DatabaseReminderRepository to add support for MongoDB based password reminders. If you don't use password reminders, you don't have to register this service provider and everything else should work just fine.
138138

139+
Sentry
140+
------
141+
142+
If yo want to use this library with [Sentry](https://cartalyst.com/manual/sentry), then check out https://github.com/jenssegers/Laravel-MongoDB-Sentry
143+
139144
Sessions
140145
--------
141146

@@ -423,6 +428,33 @@ Again, you may override the conventional local key by passing a second argument
423428

424429
return $this->embedsMany('Book', 'local_key');
425430

431+
### EmbedsOne Relations
432+
433+
There is also an EmbedsOne relation, which works similar to the EmbedsMany relation, but only stores one embedded model.
434+
435+
use Jenssegers\Mongodb\Model as Eloquent;
436+
437+
class Book extends Eloquent {
438+
439+
public function author()
440+
{
441+
return $this->embedsOne('Author');
442+
}
443+
444+
}
445+
446+
Now we can access the book's author through the dynamic property:
447+
448+
$author = Book::first()->author;
449+
450+
Inserting and updating embedded documents works just like the `embedsMany` relation:
451+
452+
$author = new Author(array('name' => 'John Doe'));
453+
454+
$book = Books::first();
455+
456+
$author = $user->author()->save($author);
457+
426458
### MySQL Relations
427459

428460
If you're using a hybrid MongoDB and SQL setup, you're in luck! The model will automatically return a MongoDB- or SQL-relation based on the type of the related model. Of course, if you want this functionality to work both ways, your SQL-models will need to extend `Jenssegers\Eloquent\Model`. Note that this functionality only works for hasOne, hasMany and belongsTo relations.

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
"authors": [
66
{
77
"name": "Jens Segers",
8-
"email": "hello@jenssegers.be"
8+
"homepage": "http://jenssegers.be"
99
}
1010
],
1111
"license" : "MIT",
1212
"require": {
13-
"php": ">=5.3.0",
13+
"php": ">=5.4.0",
1414
"illuminate/support": "4.2.x",
1515
"illuminate/database": "4.2.x",
1616
"illuminate/events": "4.2.x"
@@ -25,5 +25,8 @@
2525
"Jenssegers\\Eloquent": "src/"
2626
}
2727
},
28-
"minimum-stability": "dev"
28+
"suggest": {
29+
"jenssegers/mongodb-session": "Add MongoDB session support to Laravel-MongoDB",
30+
"jenssegers/mongodb-sentry": "Add Sentry support to Laravel-MongoDB"
31+
}
2932
}

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
</testsuite>
3535
<testsuite name="relations">
3636
<directory>tests/RelationsTest.php</directory>
37+
<directory>tests/EmbeddedRelationsTest.php</directory>
3738
</testsuite>
3839
<testsuite name="mysqlrelations">
3940
<directory>tests/RelationsTest.php</directory>

src/Jenssegers/Eloquent/Model.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php namespace Jenssegers\Eloquent;
22

3-
use Illuminate\Database\Eloquent\Relations\HasOne;
4-
use Illuminate\Database\Eloquent\Relations\HasMany;
53
use Illuminate\Database\Eloquent\Relations\MorphOne;
64
use Illuminate\Database\Eloquent\Relations\MorphMany;
75
use Illuminate\Database\Eloquent\Relations\Relation;
6+
use Jenssegers\Mongodb\Relations\HasOne;
7+
use Jenssegers\Mongodb\Relations\HasMany;
88
use Jenssegers\Mongodb\Relations\BelongsTo;
99
use Jenssegers\Mongodb\Relations\BelongsToMany;
1010
use Jenssegers\Mongodb\Relations\MorphTo;
@@ -134,12 +134,6 @@ public function morphMany($related, $name, $type = null, $id = null, $localKey =
134134
*/
135135
public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
136136
{
137-
// Check if it is a relation with an original model.
138-
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
139-
{
140-
return parent::belongsTo($related, $foreignKey, $otherKey, $relation);
141-
}
142-
143137
// If no relation name was given, we will use this debug backtrace to extract
144138
// the calling method's name and use that as the relationship name as most
145139
// of the time this will be what we desire to use for the relatinoships.
@@ -149,6 +143,12 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat
149143

150144
$relation = $caller['function'];
151145
}
146+
147+
// Check if it is a relation with an original model.
148+
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
149+
{
150+
return parent::belongsTo($related, $foreignKey, $otherKey, $relation);
151+
}
152152

153153
// If no foreign key was supplied, we can use a backtrace to guess the proper
154154
// foreign key name by using the name of the relationship function, which
@@ -227,19 +227,19 @@ public function morphTo($name = null, $type = null, $id = null)
227227
*/
228228
public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null, $relation = null)
229229
{
230-
// Check if it is a relation with an original model.
231-
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
232-
{
233-
return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $relation);
234-
}
235-
236230
// If no relationship name was passed, we will pull backtraces to get the
237231
// name of the calling function. We will use that function name as the
238232
// title of this relation since that is a great convention to apply.
239233
if (is_null($relation))
240234
{
241235
$relation = $this->getBelongsToManyCaller();
242236
}
237+
238+
// Check if it is a relation with an original model.
239+
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
240+
{
241+
return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $relation);
242+
}
243243

244244
// First, we'll need to determine the foreign key and "other key" for the
245245
// relationship. Once we have determined the keys we'll make the query

src/Jenssegers/Mongodb/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public function getElapsedTime($start)
180180
*/
181181
public function getDriverName()
182182
{
183-
return '';
183+
return 'mongodb';
184184
}
185185

186186
/**

src/Jenssegers/Mongodb/Eloquent/Builder.php

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php namespace Jenssegers\Mongodb\Eloquent;
22

33
use MongoCursor;
4+
use Illuminate\Database\Eloquent\Relations\Relation;
5+
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
46

5-
class Builder extends \Illuminate\Database\Eloquent\Builder {
7+
class Builder extends EloquentBuilder {
68

79
/**
810
* The methods that should be returned from query builder.
@@ -14,6 +16,56 @@ class Builder extends \Illuminate\Database\Eloquent\Builder {
1416
'count', 'min', 'max', 'avg', 'sum', 'exists', 'push', 'pull'
1517
);
1618

19+
/**
20+
* Add the "has" condition where clause to the query.
21+
*
22+
* @param \Illuminate\Database\Eloquent\Builder $hasQuery
23+
* @param \Illuminate\Database\Eloquent\Relations\Relation $relation
24+
* @param string $operator
25+
* @param int $count
26+
* @param string $boolean
27+
* @return \Illuminate\Database\Eloquent\Builder
28+
*/
29+
protected function addHasWhere(EloquentBuilder $hasQuery, Relation $relation, $operator, $count, $boolean)
30+
{
31+
$query = $hasQuery->getQuery();
32+
33+
// Get the number of related objects for each possible parent.
34+
$relationCount = array_count_values($query->lists($relation->getHasCompareKey()));
35+
36+
// Remove unwanted related objects based on the operator and count.
37+
$relationCount = array_filter($relationCount, function($counted) use ($count, $operator)
38+
{
39+
// If we are comparing to 0, we always need all results.
40+
if ($count == 0) return true;
41+
42+
switch ($operator)
43+
{
44+
case '>=':
45+
case '<':
46+
return $counted >= $count;
47+
case '>':
48+
case '<=':
49+
return $counted > $count;
50+
case '=':
51+
case '!=':
52+
return $counted == $count;
53+
}
54+
});
55+
56+
// If the operator is <, <= or !=, we will use whereNotIn.
57+
$not = in_array($operator, array('<', '<=', '!='));
58+
59+
// If we are comparing to 0, we need an additional $not flip.
60+
if ($count == 0) $not = !$not;
61+
62+
// All related ids.
63+
$relatedIds = array_keys($relationCount);
64+
65+
// Add whereIn to the query.
66+
return $this->whereIn($this->model->getKeyName(), $relatedIds, $boolean, $not);
67+
}
68+
1769
/**
1870
* Create a raw database expression.
1971
*

0 commit comments

Comments
 (0)