Skip to content

Commit 68eb1f2

Browse files
committed
Merge master
2 parents a54ef6d + 5b7f335 commit 68eb1f2

File tree

17 files changed

+821
-128
lines changed

17 files changed

+821
-128
lines changed

.travis.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ before_script:
1616
- echo "extension = mongo.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
1717
- mysql -e 'create database unittest;'
1818
- composer self-update
19+
- composer require satooshi/php-coveralls:dev-master
1920
- composer install --dev --no-interaction
2021

21-
script: phpunit
22+
script:
23+
- mkdir -p build/logs
24+
- phpunit --coverage-clover build/logs/clover.xml
25+
26+
after_script:
27+
- php vendor/bin/coveralls -v

README.md

Lines changed: 18 additions & 6 deletions
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)
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)
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

@@ -44,7 +44,7 @@ You can connect to multiple servers or replica sets with the following configura
4444

4545
'mongodb' => array(
4646
'driver' => 'mongodb',
47-
'host' => array('server1', 'server2),
47+
'host' => array('server1', 'server2'),
4848
'port' => 27017,
4949
'username' => 'username',
5050
'password' => 'password',
@@ -414,6 +414,11 @@ You can remove an embedded document by using the `destroy()` method:
414414
// or
415415
$user->books()->destroy($book);
416416

417+
If you want to add or remove embedded documents, without persistence, you can use the `associate` and `dissociate` methods. To write the changes to the database, save the parent object:
418+
419+
$user->books()->associate($book);
420+
$user->save();
421+
417422
Again, you may override the conventional local key by passing a second argument to the embedsMany method:
418423

419424
return $this->embedsMany('Book', 'local_key');
@@ -458,16 +463,23 @@ These expressions will be injected directly into the query.
458463

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

461-
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.
466+
You can also perform raw expressions on the internal MongoCollection object. If this is executed on the model class, it will return a collection of models. If this is executed on the query builder, it will return the original response.
467+
468+
// Returns a collection of User models.
469+
$models = User::raw(function($collection)
470+
{
471+
return $collection->find();
472+
});
462473

463-
User::raw(function($collection)
474+
// Returns the original MongoCursor.
475+
$cursor = DB::collection('users')->raw(function($collection)
464476
{
465477
return $collection->find();
466478
});
467479

468-
Or you can access the internal MongoCollection object directly:
480+
Optional: if you don't pass a closure to the raw method, the internal MongoCollection object will be accessible:
469481

470-
User::raw()->find();
482+
$model = User::raw()->findOne(array('age' => array('$lt' => 18)));
471483

472484
The MongoClient and MongoDB objects can be accessed like this:
473485

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
},
1818
"require-dev": {
1919
"illuminate/cache": "4.2.x",
20-
"illuminate/auth": "4.2.x"
20+
"illuminate/auth": "4.2.x",
21+
"mockery/mockery": "*"
2122
},
2223
"autoload": {
2324
"psr-0": {
2425
"Jenssegers\\Mongodb": "src/",
2526
"Jenssegers\\Eloquent": "src/"
2627
}
27-
},
28-
"minimum-stability": "dev"
28+
}
2929
}

src/Jenssegers/Eloquent/Model.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Database\Eloquent\Relations\Relation;
88
use Jenssegers\Mongodb\Relations\BelongsTo;
99
use Jenssegers\Mongodb\Relations\BelongsToMany;
10+
use Jenssegers\Mongodb\Relations\MorphTo;
1011
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
1112

1213
abstract class Model extends \Illuminate\Database\Eloquent\Model {
@@ -169,6 +170,51 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat
169170
return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation);
170171
}
171172

173+
/**
174+
* Define a polymorphic, inverse one-to-one or many relationship.
175+
*
176+
* @param string $name
177+
* @param string $type
178+
* @param string $id
179+
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
180+
*/
181+
public function morphTo($name = null, $type = null, $id = null)
182+
{
183+
// If no name is provided, we will use the backtrace to get the function name
184+
// since that is most likely the name of the polymorphic interface. We can
185+
// use that to get both the class and foreign key that will be utilized.
186+
if (is_null($name))
187+
{
188+
list(, $caller) = debug_backtrace(false);
189+
190+
$name = snake_case($caller['function']);
191+
}
192+
193+
list($type, $id) = $this->getMorphs($name, $type, $id);
194+
195+
// If the type value is null it is probably safe to assume we're eager loading
196+
// the relationship. When that is the case we will pass in a dummy query as
197+
// there are multiple types in the morph and we can't use single queries.
198+
if (is_null($class = $this->$type))
199+
{
200+
return new MorphTo(
201+
$this->newQuery(), $this, $id, null, $type, $name
202+
);
203+
}
204+
205+
// If we are not eager loading the relatinship, we will essentially treat this
206+
// as a belongs-to style relationship since morph-to extends that class and
207+
// we will pass in the appropriate values so that it behaves as expected.
208+
else
209+
{
210+
$instance = new $class;
211+
212+
return new MorphTo(
213+
with($instance)->newQuery(), $this, $id, $instance->getKeyName(), $type, $name
214+
);
215+
}
216+
}
217+
172218
/**
173219
* Define a many-to-many relationship.
174220
*

src/Jenssegers/Mongodb/Connection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@ public function getElapsedTime($start)
173173
return parent::getElapsedTime($start);
174174
}
175175

176+
/**
177+
* Get the PDO driver name.
178+
*
179+
* @return string
180+
*/
181+
public function getDriverName()
182+
{
183+
return '';
184+
}
185+
176186
/**
177187
* Dynamically pass methods to the connection.
178188
*

src/Jenssegers/Mongodb/Eloquent/Builder.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php namespace Jenssegers\Mongodb\Eloquent;
22

3+
use MongoCursor;
4+
35
class Builder extends \Illuminate\Database\Eloquent\Builder {
46

57
/**
@@ -12,4 +14,50 @@ class Builder extends \Illuminate\Database\Eloquent\Builder {
1214
'count', 'min', 'max', 'avg', 'sum', 'exists', 'push', 'pull'
1315
);
1416

17+
/**
18+
* Create a raw database expression.
19+
*
20+
* @param closure $expression
21+
* @return mixed
22+
*/
23+
public function raw($expression = null)
24+
{
25+
// Get raw results from the query builder.
26+
$results = $this->query->raw($expression);
27+
28+
$connection = $this->model->getConnectionName();
29+
30+
// Convert MongoCursor results to a collection of models.
31+
if ($results instanceof MongoCursor)
32+
{
33+
$results = iterator_to_array($results, false);
34+
35+
$models = array();
36+
37+
// Once we have the results, we can spin through them and instantiate a fresh
38+
// model instance for each records we retrieved from the database. We will
39+
// also set the proper connection name for the model after we create it.
40+
foreach ($results as $result)
41+
{
42+
$models[] = $model = $this->model->newFromBuilder($result);
43+
44+
$model->setConnection($connection);
45+
}
46+
47+
return $this->model->newCollection($models);
48+
}
49+
50+
// The result is a single object.
51+
else if (is_array($results) and array_key_exists('_id', $results))
52+
{
53+
$model = $this->model->newFromBuilder($results);
54+
55+
$model->setConnection($connection);
56+
57+
return $model;
58+
}
59+
60+
return $results;
61+
}
62+
1563
}

src/Jenssegers/Mongodb/Model.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,22 +228,34 @@ public function dropColumn($columns)
228228
}
229229

230230
/**
231-
* Pass push to the query builder.
231+
* Append one or more values to an array.
232232
*
233233
* @return mixed
234234
*/
235235
public function push()
236236
{
237237
if ($parameters = func_get_args())
238238
{
239-
$query = $this->newQuery();
239+
$query = $this->setKeysForSaveQuery($this->newQuery());
240240

241241
return call_user_func_array(array($query, 'push'), $parameters);
242242
}
243243

244244
return parent::push();
245245
}
246246

247+
/**
248+
* Remove one or more values from an array.
249+
*
250+
* @return mixed
251+
*/
252+
public function pull()
253+
{
254+
$query = $this->setKeysForSaveQuery($this->newQuery());
255+
256+
return call_user_func_array(array($query, 'pull'), func_get_args());
257+
}
258+
247259
/**
248260
* Create a new Eloquent query builder for the model.
249261
*

0 commit comments

Comments
 (0)