Skip to content

Commit 1c4df65

Browse files
committed
Hide interal embedded documents attribute, fixes #229
1 parent 6df8a48 commit 1c4df65

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

src/Jenssegers/Mongodb/Model.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,28 @@ public function attributesToArray()
228228
// MongoDB related objects to a string representation. This kind
229229
// of mimics the SQL behaviour so that dates are formatted
230230
// nicely when your models are converted to JSON.
231-
foreach ($attributes as &$value)
231+
foreach ($attributes as $key => &$value)
232232
{
233233
if ($value instanceof MongoId)
234234
{
235235
$value = (string) $value;
236236
}
237+
238+
// If the attribute starts with an underscore, it might be the
239+
// internal array of embedded documents. In that case, we need
240+
// to hide these from the output so that the relation-based
241+
// attribute can take over.
242+
else if (starts_with($key, '_'))
243+
{
244+
$camelKey = camel_case($key);
245+
246+
// If we can find a method that responds to this relation we
247+
// will remove it from the output.
248+
if (method_exists($this, $camelKey))
249+
{
250+
unset($attributes[$key]);
251+
}
252+
}
237253
}
238254

239255
return $attributes;

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,31 @@ public function convertKey($id)
663663
return $id;
664664
}
665665

666+
/**
667+
* Add a basic where clause to the query.
668+
*
669+
* @param string $column
670+
* @param string $operator
671+
* @param mixed $value
672+
* @param string $boolean
673+
* @return \Illuminate\Database\Query\Builder|static
674+
*
675+
* @throws \InvalidArgumentException
676+
*/
677+
public function where($column, $operator = null, $value = null, $boolean = 'and')
678+
{
679+
// Remove the leading $ from operators.
680+
if (func_num_args() == 3)
681+
{
682+
if (starts_with($operator, '$'))
683+
{
684+
$operator = substr($operator, 1);
685+
}
686+
}
687+
688+
return parent::where($column, $operator, $value, $boolean);
689+
}
690+
666691
/**
667692
* Compile the where array.
668693
*

tests/EmbeddedRelationsTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ public function testEmbedsManySave()
8282
$this->assertEquals(array('London', 'Manhattan', 'Bruxelles'), $freshUser->addresses->lists('city'));
8383
}
8484

85+
public function testEmbedsToArray()
86+
{
87+
$user = User::create(array('name' => 'John Doe'));
88+
$user->addresses()->saveMany(array(new Address(array('city' => 'London')), new Address(array('city' => 'Bristol'))));
89+
90+
$array = $user->toArray();
91+
$this->assertFalse(array_key_exists('_addresses', $array));
92+
$this->assertTrue(array_key_exists('addresses', $array));
93+
}
94+
8595
public function testEmbedsManyAssociate()
8696
{
8797
$user = User::create(array('name' => 'John Doe'));

tests/QueryBuilderTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,9 @@ public function testOperators()
520520
$results = DB::collection('items')->where('tags', 'size', 2)->get();
521521
$this->assertEquals(2, count($results));
522522

523+
$results = DB::collection('items')->where('tags', '$size', 2)->get();
524+
$this->assertEquals(2, count($results));
525+
523526
$results = DB::collection('items')->where('tags', 'size', 3)->get();
524527
$this->assertEquals(0, count($results));
525528

0 commit comments

Comments
 (0)