Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Jenssegers/Mongodb/Eloquent/HybridRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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 );
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
10 changes: 10 additions & 0 deletions src/Jenssegers/Mongodb/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
12 changes: 11 additions & 1 deletion src/Jenssegers/Mongodb/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function find($id, $columns = [])
*/
public function get($columns = [])
{
return parent::get($columns);
return $this->getFresh($columns);
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down