Skip to content

Commit 7807580

Browse files
committed
Merge branch 'master' of github.com:jenssegers/laravel-mongodb
2 parents 87ac10c + e2a8fae commit 7807580

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

src/Jenssegers/Mongodb/Eloquent/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function update(array $values, array $options = [])
4444
return 1;
4545
}
4646

47-
return $this->query->update($this->addUpdatedAtColumn($values), $options);
47+
return $this->toBase()->update($this->addUpdatedAtColumn($values), $options);
4848
}
4949

5050
/**

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,16 @@ public function find($id, $columns = [])
204204
return $this->where('_id', '=', $this->convertKey($id))->first($columns);
205205
}
206206

207+
/**
208+
* @inheritdoc
209+
*/
210+
public function value($column)
211+
{
212+
$result = (array) $this->first([$column]);
213+
214+
return Arr::get($result, $column);
215+
}
216+
207217
/**
208218
* @inheritdoc
209219
*/

tests/QueryBuilderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,4 +701,16 @@ public function testProjections()
701701
$this->assertEquals(1, count($result['tags']));
702702
}
703703
}
704+
705+
public function testValue()
706+
{
707+
DB::collection('books')->insert([
708+
['title' => 'Moby-Dick', 'author' => ['first_name' => 'Herman', 'last_name' => 'Melville']]
709+
]);
710+
711+
$this->assertEquals('Moby-Dick', DB::collection('books')->value('title'));
712+
$this->assertEquals(['first_name' => 'Herman', 'last_name' => 'Melville'], DB::collection('books')->value('author'));
713+
$this->assertEquals('Herman', DB::collection('books')->value('author.first_name'));
714+
$this->assertEquals('Melville', DB::collection('books')->value('author.last_name'));
715+
}
704716
}

tests/QueryTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function setUp(): void
2121
public function tearDown(): void
2222
{
2323
User::truncate();
24+
Scoped::truncate();
2425
parent::tearDown();
2526
}
2627

@@ -309,4 +310,21 @@ public function testPaginate()
309310
$this->assertEquals(9, $results->total());
310311
$this->assertEquals(1, $results->currentPage());
311312
}
313+
314+
public function testUpdate()
315+
{
316+
$this->assertEquals(1, User::where(['name' => 'John Doe'])->update(['name' => 'Jim Morrison']));
317+
$this->assertEquals(1, User::where(['name' => 'Jim Morrison'])->count());
318+
319+
Scoped::create(['favorite' => true]);
320+
Scoped::create(['favorite' => false]);
321+
322+
$this->assertCount(1, Scoped::get());
323+
$this->assertEquals(1, Scoped::query()->update(['name' => 'Johnny']));
324+
$this->assertCount(1, Scoped::withoutGlobalScopes()->where(['name' => 'Johnny'])->get());
325+
326+
$this->assertCount(2, Scoped::withoutGlobalScopes()->get());
327+
$this->assertEquals(2, Scoped::withoutGlobalScopes()->update(['name' => 'Jimmy']));
328+
$this->assertCount(2, Scoped::withoutGlobalScopes()->where(['name' => 'Jimmy'])->get());
329+
}
312330
}

tests/models/Scoped.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
4+
use Jenssegers\Mongodb\Eloquent\Builder;
5+
6+
class Scoped extends Eloquent
7+
{
8+
protected $connection = 'mongodb';
9+
protected $collection = 'scoped';
10+
protected $fillable = ['name', 'favorite'];
11+
12+
protected static function boot()
13+
{
14+
parent::boot();
15+
16+
static::addGlobalScope('favorite', function (Builder $builder) {
17+
$builder->where('favorite', true);
18+
});
19+
}
20+
}

0 commit comments

Comments
 (0)