Commit 14db4cbb authored by Jens Segers's avatar Jens Segers

Adding more collection operations for #239

parent 415ed78e
<?php namespace Jenssegers\Mongodb\Eloquent;
use Illuminate\Database\Eloquent\Collection as BaseCollection;
class Collection extends BaseCollection {
/**
* Simulate a basic where clause on the ollection.
*
* @param string $key
* @param string $operator
* @param mixed $value
* @param string $boolean
* @return $this
*/
public function where($key, $operator = null, $value = null)
{
// Here we will make some assumptions about the operator. If only 2 values are
// passed to the method, we will assume that the operator is an equals sign
// and keep going.
if (func_num_args() == 2)
{
list($value, $operator) = array($operator, '=');
}
return $this->filter(function($item) use ($key, $operator, $value)
{
$actual = $item->{$key};
switch ($operator)
{
case '<>':
case '!=':
return $actual != $value;
break;
case '>':
return $actual > $value;
break;
case '<':
return $actual < $value;
break;
case '>=':
return $actual >= $value;
break;
case 'between':
return $actual >= $value[0] and $actual <= $value[1];
break;
case '=':
default:
return $actual == $value;
break;
}
});
}
/**
* Simulate order by.
*
* @param string $key
* @param string $direction
* @return Illuminate\Database\Eloquent\Collection
*/
public function orderBy($key, $direction = 'asc')
{
$descending = strtolower($direction) == 'desc';
return $this->sortBy($key, SORT_REGULAR, $descending);
}
}
<?php namespace Jenssegers\Mongodb\Relations;
use MongoId;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\Collection;
use MongoId;
class EmbedsMany extends EmbedsOneOrMany {
/**
* Get the results of the relationship.
*
* @return Illuminate\Database\Eloquent\Collection
* @return Jenssegers\Mongodb\Eloquent\Collection
*/
public function getResults()
{
......@@ -290,20 +289,6 @@ class EmbedsMany extends EmbedsOneOrMany {
return parent::setEmbedded(array_values($models));
}
/**
* Simulate order by method.
*
* @param string $column
* @param string $direction
* @return Illuminate\Database\Eloquent\Collection
*/
public function orderBy($column, $direction = 'asc')
{
$descending = strtolower($direction) == 'desc';
return $this->getResults()->sortBy($column, SORT_REGULAR, $descending);
}
/**
* Handle dynamic method calls to the relationship.
*
......@@ -314,7 +299,7 @@ class EmbedsMany extends EmbedsOneOrMany {
public function __call($method, $parameters)
{
// Collection methods
if (method_exists('Illuminate\Database\Eloquent\Collection', $method))
if (method_exists('Jenssegers\Mongodb\Eloquent\Collection', $method))
{
return call_user_func_array(array($this->getResults(), $method), $parameters);
}
......
<?php namespace Jenssegers\Mongodb\Relations;
use MongoId;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\Collection;
use MongoId;
class EmbedsOne extends EmbedsOneOrMany {
/**
* Get the results of the relationship.
*
* @return Illuminate\Database\Eloquent\Collection
* @return \Illuminate\Database\Eloquent\Model
*/
public function getResults()
{
......
<?php namespace Jenssegers\Mongodb\Relations;
use MongoId;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\Collection;
use MongoId;
use Illuminate\Database\Eloquent\Collection as BaseCollection;
use Jenssegers\Mongodb\Eloquent\Collection;
abstract class EmbedsOneOrMany extends Relation {
......@@ -109,7 +110,7 @@ abstract class EmbedsOneOrMany extends Relation {
* @param string $relation
* @return array
*/
public function match(array $models, Collection $results, $relation)
public function match(array $models, BaseCollection $results, $relation)
{
foreach ($models as $model)
{
......@@ -126,7 +127,7 @@ abstract class EmbedsOneOrMany extends Relation {
/**
* Shorthand to get the results of the relationship.
*
* @return Illuminate\Database\Eloquent\Collection
* @return Jenssegers\Mongodb\Eloquent\Collection
*/
public function get()
{
......@@ -278,7 +279,7 @@ abstract class EmbedsOneOrMany extends Relation {
* Convert an array of records to a Collection.
*
* @param array $records
* @return Illuminate\Database\Eloquent\Collection
* @return Jenssegers\Mongodb\Eloquent\Collection
*/
protected function toCollection(array $records = array())
{
......@@ -294,7 +295,7 @@ abstract class EmbedsOneOrMany extends Relation {
$models = $this->eagerLoadRelations($models);
}
return $this->related->newCollection($models);
return new Collection($models);
}
/**
......
......@@ -416,14 +416,19 @@ class EmbeddedRelationsTest extends TestCase {
public function testEmbedsManyCollectionMethods()
{
$user = User::create(array('name' => 'John Doe'));
$user->addresses()->save(new Address(array('city' => 'New York')));
$user->addresses()->save(new Address(array('city' => 'Paris')));
$user->addresses()->save(new Address(array('city' => 'Brussels')));
$this->assertEquals(array('New York', 'Paris', 'Brussels'), $user->addresses()->lists('city'));
$this->assertEquals(array('Brussels', 'New York', 'Paris'), $user->addresses()->sortBy('city')->lists('city'));
$this->assertEquals(array('Brussels', 'New York', 'Paris'), $user->addresses()->orderBy('city')->lists('city'));
$this->assertEquals(array('Paris', 'New York', 'Brussels'), $user->addresses()->orderBy('city', 'desc')->lists('city'));
$user->addresses()->save(new Address(array('city' => 'Paris', 'country' => 'France')));
$user->addresses()->save(new Address(array('city' => 'Bruges', 'country' => 'Belgium')));
$user->addresses()->save(new Address(array('city' => 'Brussels', 'country' => 'Belgium')));
$user->addresses()->save(new Address(array('city' => 'Ghent', 'country' => 'Belgium')));
$this->assertEquals(array('Paris', 'Bruges', 'Brussels', 'Ghent'), $user->addresses()->lists('city'));
$this->assertEquals(array('Bruges', 'Brussels', 'Ghent', 'Paris'), $user->addresses()->sortBy('city')->lists('city'));
$this->assertEquals(array('Bruges', 'Brussels', 'Ghent', 'Paris'), $user->addresses()->orderBy('city')->lists('city'));
$this->assertEquals(array('Paris', 'Ghent', 'Brussels', 'Bruges'), $user->addresses()->orderBy('city', 'desc')->lists('city'));
$this->assertEquals(array(), $user->addresses()->where('city', 'New York')->lists('city'));
$this->assertEquals(array('Bruges', 'Brussels', 'Ghent'), $user->addresses()->where('country', 'Belgium')->lists('city'));
$this->assertEquals(array('Ghent', 'Brussels', 'Bruges'), $user->addresses()->where('country', 'Belgium')->orderBy('city', 'desc')->lists('city'));
}
public function testEmbedsOne()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment