diff --git a/src/Jenssegers/Mongodb/Model.php b/src/Jenssegers/Mongodb/Model.php index 406ada4b66e920b2543d89a8cfe6ee38e68f97ff..7a8e51686d750b0457a08fe13b88c881040db0a4 100644 --- a/src/Jenssegers/Mongodb/Model.php +++ b/src/Jenssegers/Mongodb/Model.php @@ -228,12 +228,28 @@ abstract class Model extends \Jenssegers\Eloquent\Model { // MongoDB related objects to a string representation. This kind // of mimics the SQL behaviour so that dates are formatted // nicely when your models are converted to JSON. - foreach ($attributes as &$value) + foreach ($attributes as $key => &$value) { if ($value instanceof MongoId) { $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; diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index db0772c70e4f26418bec8f7e32203bc87ab1979e..542a6d90976e0b0338de82ecc70ed23ffb511d43 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -663,6 +663,31 @@ class Builder extends \Illuminate\Database\Query\Builder { 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. * diff --git a/tests/EmbeddedRelationsTest.php b/tests/EmbeddedRelationsTest.php index 284fe9f54df379c7e338c67811d6c8421d71c8ca..1b0d85f75ae7dcad4669defaf8726a2ca925a311 100644 --- a/tests/EmbeddedRelationsTest.php +++ b/tests/EmbeddedRelationsTest.php @@ -82,6 +82,16 @@ class EmbeddedRelationsTest extends TestCase { $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() { $user = User::create(array('name' => 'John Doe')); diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 309de7b1a46b79b2c28ab0ca0a05c01b759bef98..81d150425fd9642b1d0d910650a5238d94a15f80 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -520,6 +520,9 @@ class QueryBuilderTest extends TestCase { $results = DB::collection('items')->where('tags', 'size', 2)->get(); $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(); $this->assertEquals(0, count($results));