diff --git a/src/Jenssegers/Mongodb/Eloquent/HybridRelations.php b/src/Jenssegers/Mongodb/Eloquent/HybridRelations.php index b6f898632..6887817c2 100644 --- a/src/Jenssegers/Mongodb/Eloquent/HybridRelations.php +++ b/src/Jenssegers/Mongodb/Eloquent/HybridRelations.php @@ -22,7 +22,7 @@ trait HybridRelations { public function hasOne($related, $foreignKey = null, $localKey = null) { // Check if it is a relation with an original model. - if ($related instanceof Model) + if (! is_subclass_of($related, 'Jenssegers\Mongodb\Model')) { return parent::hasOne($related, $foreignKey, $localKey); } @@ -49,7 +49,7 @@ public function hasOne($related, $foreignKey = null, $localKey = null) public function morphOne($related, $name, $type = null, $id = null, $localKey = null) { // Check if it is a relation with an original model. - if ($related instanceof Model) + if (! is_subclass_of($related, 'Jenssegers\Mongodb\Model')) { return parent::morphOne($related, $name, $type, $id, $localKey ); } @@ -76,7 +76,7 @@ public function morphOne($related, $name, $type = null, $id = null, $localKey = public function hasMany($related, $foreignKey = null, $localKey = null) { // Check if it is a relation with an original model. - if ($related instanceof Model) + if (! is_subclass_of($related, 'Jenssegers\Mongodb\Model')) { return parent::hasMany($related, $foreignKey, $localKey); } @@ -103,7 +103,7 @@ public function hasMany($related, $foreignKey = null, $localKey = null) public function morphMany($related, $name, $type = null, $id = null, $localKey = null) { // Check if it is a relation with an original model. - if ($related instanceof Model) + if (! is_subclass_of($related, 'Jenssegers\Mongodb\Model')) { return parent::morphMany($related, $name, $type, $id, $localKey); } @@ -144,7 +144,7 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat } // Check if it is a relation with an original model. - if ($related instanceof Model) + if (! is_subclass_of($related, 'Jenssegers\Mongodb\Model')) { return parent::belongsTo($related, $foreignKey, $otherKey, $relation); } @@ -235,7 +235,7 @@ public function belongsToMany($related, $collection = null, $foreignKey = null, } // Check if it is a relation with an original model. - if ($related instanceof Model) + if (! is_subclass_of($related, 'Jenssegers\Mongodb\Model')) { return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $relation); } diff --git a/src/Jenssegers/Mongodb/Model.php b/src/Jenssegers/Mongodb/Model.php index 03bd49ec2..449d77529 100644 --- a/src/Jenssegers/Mongodb/Model.php +++ b/src/Jenssegers/Mongodb/Model.php @@ -199,6 +199,16 @@ protected function getDateFormat() return $this->dateFormat ?: 'Y-m-d H:i:s'; } + /** + * Get a fresh timestamp for the model. + * + * @return MongoDate + */ + public function freshTimestamp() + { + return new MongoDate; + } + /** * Get the table associated with the model. * diff --git a/src/Jenssegers/Mongodb/Query/Builder.php b/src/Jenssegers/Mongodb/Query/Builder.php index 58ed14124..531197eba 100644 --- a/src/Jenssegers/Mongodb/Query/Builder.php +++ b/src/Jenssegers/Mongodb/Query/Builder.php @@ -147,7 +147,7 @@ public function find($id, $columns = []) */ public function get($columns = []) { - return parent::get($columns); + return $this->getFresh($columns); } /** @@ -349,6 +349,16 @@ public function aggregate($function, $columns = []) } } + /** + * Determine if any rows exist for the current query. + * + * @return bool + */ + public function exists() + { + return ! is_null($this->first()); + } + /** * Force the query to only return distinct results. * diff --git a/tests/QueryTest.php b/tests/QueryTest.php index 2db648d8b..9b3593257 100644 --- a/tests/QueryTest.php +++ b/tests/QueryTest.php @@ -206,6 +206,12 @@ public function testCount() $this->assertEquals(6, $count); } + public function testExists() + { + $this->assertFalse(User::where('age', '>', 37)->exists()); + $this->assertTrue(User::where('age', '<', 37)->exists()); + } + public function testSubquery() { $users = User::where('title', 'admin')->orWhere(function ($query)