Skip to content

Commit

Permalink
Merge branch 'master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
Smolevich committed Feb 3, 2020
2 parents 0d15169 + c55d42d commit 7605464
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -460,7 +460,7 @@ Selects documents where values match a specified regular expression.
```php
use MongoDB\BSON\Regex;

User::where('name', 'regex', new Regex("/.*doe/i"))->get();
User::where('name', 'regex', new Regex('.*doe', 'i'))->get();
```

**NOTE:** you can also use the Laravel regexp operations. These are a bit more flexible and will automatically convert your regular expression string to a `MongoDB\BSON\Regex` object.
Expand Down
3 changes: 2 additions & 1 deletion src/Jenssegers/Mongodb/Auth/User.php
Expand Up @@ -3,6 +3,7 @@
namespace Jenssegers\Mongodb\Auth;

use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\MustVerifyEmail;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
Expand All @@ -15,5 +16,5 @@ class User extends Model implements
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
use Authenticatable, Authorizable, CanResetPassword, MustVerifyEmail;
}
31 changes: 17 additions & 14 deletions src/Jenssegers/Mongodb/Helpers/QueriesRelationships.php
Expand Up @@ -8,13 +8,14 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
use Illuminate\Database\Eloquent\Relations\Relation;
use Jenssegers\Mongodb\Eloquent\Model;

trait QueriesRelationships
{
/**
* Add a relationship count / exists condition to the query.
* @param string $relation
* @param Relation|string $relation
* @param string $operator
* @param int $count
* @param string $boolean
Expand All @@ -23,11 +24,13 @@ trait QueriesRelationships
*/
public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null)
{
if (strpos($relation, '.') !== false) {
return $this->hasNested($relation, $operator, $count, $boolean, $callback);
}
if (is_string($relation)) {
if (strpos($relation, '.') !== false) {
return $this->hasNested($relation, $operator, $count, $boolean, $callback);
}

$relation = $this->getRelationWithoutConstraints($relation);
$relation = $this->getRelationWithoutConstraints($relation);
}

// If this is a hybrid relation then we can not use a normal whereExists() query that relies on a subquery
// We need to use a `whereIn` query
Expand Down Expand Up @@ -59,25 +62,25 @@ public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', C
}

/**
* @param $relation
* @param Relation $relation
* @return bool
*/
protected function isAcrossConnections($relation)
protected function isAcrossConnections(Relation $relation)
{
return $relation->getParent()->getConnectionName() !== $relation->getRelated()->getConnectionName();
}

/**
* Compare across databases
* @param $relation
* @param Relation $relation
* @param string $operator
* @param int $count
* @param string $boolean
* @param Closure|null $callback
* @return mixed
* @throws Exception
*/
public function addHybridHas($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null)
public function addHybridHas(Relation $relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null)
{
$hasQuery = $relation->getQuery();
if ($callback) {
Expand All @@ -99,10 +102,10 @@ public function addHybridHas($relation, $operator = '>=', $count = 1, $boolean =
}

/**
* @param $relation
* @param Relation $relation
* @return string
*/
protected function getHasCompareKey($relation)
protected function getHasCompareKey(Relation $relation)
{
if (method_exists($relation, 'getHasCompareKey')) {
return $relation->getHasCompareKey();
Expand Down Expand Up @@ -147,14 +150,14 @@ protected function getConstrainedRelatedIds($relations, $operator, $count)

/**
* Returns key we are constraining this parent model's query with
* @param $relation
* @param Relation $relation
* @return string
* @throws Exception
*/
protected function getRelatedConstraintKey($relation)
protected function getRelatedConstraintKey(Relation $relation)
{
if ($relation instanceof HasOneOrMany) {
return $this->model->getKeyName();
return $relation->getLocalKeyName();
}

if ($relation instanceof BelongsTo) {
Expand Down
4 changes: 3 additions & 1 deletion src/Jenssegers/Mongodb/Query/Builder.php
Expand Up @@ -384,10 +384,12 @@ public function getFresh($columns = [])
if ($this->limit) {
$options['limit'] = $this->limit;
}
if ($this->hint) {
$options['hint'] = $this->hint;
}
if ($columns) {
$options['projection'] = $columns;
}
// if ($this->hint) $cursor->hint($this->hint);

// Fix for legacy support, converts the results to arrays instead of objects.
$options['typeMap'] = ['root' => 'array', 'document' => 'array'];
Expand Down
8 changes: 4 additions & 4 deletions tests/ModelTest.php
Expand Up @@ -361,7 +361,7 @@ public function testUnset(): void

$user1->unset('note1');

$this->assertObjectNotHasAttribute('note1', $user1);
$this->assertFalse(isset($user1->note1));
$this->assertTrue(isset($user1->note2));
$this->assertTrue(isset($user2->note1));
$this->assertTrue(isset($user2->note2));
Expand All @@ -370,15 +370,15 @@ public function testUnset(): void
$user1 = User::find($user1->_id);
$user2 = User::find($user2->_id);

$this->assertObjectNotHasAttribute('note1', $user1);
$this->assertFalse(isset($user1->note1));
$this->assertTrue(isset($user1->note2));
$this->assertTrue(isset($user2->note1));
$this->assertTrue(isset($user2->note2));

$user2->unset(['note1', 'note2']);

$this->assertObjectNotHasAttribute('note1', $user2);
$this->assertObjectNotHasAttribute('note2', $user2);
$this->assertFalse(isset($user2->note1));
$this->assertFalse(isset($user2->note2));
}

public function testDates(): void
Expand Down
21 changes: 21 additions & 0 deletions tests/QueryBuilderTest.php
Expand Up @@ -737,4 +737,25 @@ public function testValue()
$this->assertEquals('Herman', DB::collection('books')->value('author.first_name'));
$this->assertEquals('Melville', DB::collection('books')->value('author.last_name'));
}

public function testHintOptions()
{
DB::collection('items')->insert([
['name' => 'fork', 'tags' => ['sharp', 'pointy']],
['name' => 'spork', 'tags' => ['sharp', 'pointy', 'round', 'bowl']],
['name' => 'spoon', 'tags' => ['round', 'bowl']],
]);

$results = DB::collection('items')->hint(['$natural' => -1])->get();

$this->assertEquals('spoon', $results[0]['name']);
$this->assertEquals('spork', $results[1]['name']);
$this->assertEquals('fork', $results[2]['name']);

$results = DB::collection('items')->hint(['$natural' => 1])->get();

$this->assertEquals('spoon', $results[2]['name']);
$this->assertEquals('spork', $results[1]['name']);
$this->assertEquals('fork', $results[0]['name']);
}
}

0 comments on commit 7605464

Please sign in to comment.