Commit 1c4df65a authored by Jens Segers's avatar Jens Segers

Hide interal embedded documents attribute, fixes #229

parent 6df8a487
...@@ -228,12 +228,28 @@ abstract class Model extends \Jenssegers\Eloquent\Model { ...@@ -228,12 +228,28 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
// MongoDB related objects to a string representation. This kind // MongoDB related objects to a string representation. This kind
// of mimics the SQL behaviour so that dates are formatted // of mimics the SQL behaviour so that dates are formatted
// nicely when your models are converted to JSON. // nicely when your models are converted to JSON.
foreach ($attributes as &$value) foreach ($attributes as $key => &$value)
{ {
if ($value instanceof MongoId) if ($value instanceof MongoId)
{ {
$value = (string) $value; $value = (string) $value;
} }
// If the attribute starts with an underscore, it might be the
// internal array of embedded documents. In that case, we need
// to hide these from the output so that the relation-based
// attribute can take over.
else if (starts_with($key, '_'))
{
$camelKey = camel_case($key);
// If we can find a method that responds to this relation we
// will remove it from the output.
if (method_exists($this, $camelKey))
{
unset($attributes[$key]);
}
}
} }
return $attributes; return $attributes;
......
...@@ -663,6 +663,31 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -663,6 +663,31 @@ class Builder extends \Illuminate\Database\Query\Builder {
return $id; return $id;
} }
/**
* Add a basic where clause to the query.
*
* @param string $column
* @param string $operator
* @param mixed $value
* @param string $boolean
* @return \Illuminate\Database\Query\Builder|static
*
* @throws \InvalidArgumentException
*/
public function where($column, $operator = null, $value = null, $boolean = 'and')
{
// Remove the leading $ from operators.
if (func_num_args() == 3)
{
if (starts_with($operator, '$'))
{
$operator = substr($operator, 1);
}
}
return parent::where($column, $operator, $value, $boolean);
}
/** /**
* Compile the where array. * Compile the where array.
* *
......
...@@ -82,6 +82,16 @@ class EmbeddedRelationsTest extends TestCase { ...@@ -82,6 +82,16 @@ class EmbeddedRelationsTest extends TestCase {
$this->assertEquals(array('London', 'Manhattan', 'Bruxelles'), $freshUser->addresses->lists('city')); $this->assertEquals(array('London', 'Manhattan', 'Bruxelles'), $freshUser->addresses->lists('city'));
} }
public function testEmbedsToArray()
{
$user = User::create(array('name' => 'John Doe'));
$user->addresses()->saveMany(array(new Address(array('city' => 'London')), new Address(array('city' => 'Bristol'))));
$array = $user->toArray();
$this->assertFalse(array_key_exists('_addresses', $array));
$this->assertTrue(array_key_exists('addresses', $array));
}
public function testEmbedsManyAssociate() public function testEmbedsManyAssociate()
{ {
$user = User::create(array('name' => 'John Doe')); $user = User::create(array('name' => 'John Doe'));
......
...@@ -520,6 +520,9 @@ class QueryBuilderTest extends TestCase { ...@@ -520,6 +520,9 @@ class QueryBuilderTest extends TestCase {
$results = DB::collection('items')->where('tags', 'size', 2)->get(); $results = DB::collection('items')->where('tags', 'size', 2)->get();
$this->assertEquals(2, count($results)); $this->assertEquals(2, count($results));
$results = DB::collection('items')->where('tags', '$size', 2)->get();
$this->assertEquals(2, count($results));
$results = DB::collection('items')->where('tags', 'size', 3)->get(); $results = DB::collection('items')->where('tags', 'size', 3)->get();
$this->assertEquals(0, count($results)); $this->assertEquals(0, count($results));
......
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