Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

increment, decrement, whereNotIn #5

Merged
merged 2 commits into from
Jul 14, 2013
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ branches:
php:
- 5.3
- 5.4
- 5.5

services: mongodb

Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ $users = User::where('votes', '>', 100)->orWhere('name', 'John')->get();
$users = User::whereIn('age', array(16, 18, 20))->get();
```

When using `whereNotIn` objects will be returned if the field is non existant. Combine with `whereNotNull('age')` to leave out those documents.

**Using Where Between**

```php
Expand Down Expand Up @@ -181,3 +183,30 @@ $user = Comment::where('body', 'like', '%spam%')->get();
**Inserts, updates and deletes**

All basic insert, update, delete and select methods should be implemented.

**Increments & decrements**

Perform increments (default 1) on specified attributes.
Attention: without a where-clause, every object will be modified.

```php
User::where('name', 'John Doe')->increment('age');
User::where('name', 'Bart De Wever')->decrement('weight', 50);
```

The number of updated objects is returned.

```php
$count = User->increment('age');
echo $count;
```

will return the number of users where `age` is a valid field.

These functions also allow for a third attribute:

```php
User::where('age', '29')->increment('age', 1, array('group' => 'thirty something'));

User::where('bmi', 30)->decrement('bmi', 1, array('category' => 'overweight'));
```
50 changes: 50 additions & 0 deletions src/Jenssegers/Mongodb/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,49 @@ public function update(array $values)
return 0;
}

/**
* Increment a column's value by a given amount.
*
* @param string $column
* @param int $amount
* @param array $extra
* @return int
*/
public function increment($column, $amount = 1, array $extra = array())
{
// build update statement
$update = array(
'$inc' => array($column => $amount),
'$set' => $extra,
);

// protect
$this->whereNotNull($column);

// perform
$result = $this->collection->update($this->compileWheres(), $update, array('multiple' => true));

if (1 == (int) $result['ok'])
{
return $result['n'];
}

return 0;
}

/**
* Decrement a column's value by a given amount.
*
* @param string $column
* @param int $amount
* @param array $extra
* @return int
*/
public function decrement($column, $amount = 1, array $extra = array())
{
return $this->increment($column, -1 * $amount, $extra);
}

/**
* Delete a record from the database.
*
Expand Down Expand Up @@ -429,6 +472,13 @@ private function compileWhereIn($where)
return array($column => array('$in' => $values));
}

private function compileWhereNotIn($where)
{
extract($where);

return array($column => array('$nin' => $values));
}

private function compileWhereNull($where)
{
$where['operator'] = '=';
Expand Down
50 changes: 50 additions & 0 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ public function testSelect()
$this->assertEquals('John Doe', $user->name);
$this->assertEquals(null, $user->age);

$user = User::select('name', 'title')->first();

$this->assertEquals('John Doe', $user->name);
$this->assertEquals('admin', $user->title);
$this->assertEquals(null, $user->age);

$user = User::get(array('name'))->first();

$this->assertEquals('John Doe', $user->name);
Expand Down Expand Up @@ -143,12 +149,22 @@ public function testIn()

$users = User::whereIn('age', array(33, 35, 13))->get();
$this->assertEquals(6, count($users));

$users = User::whereNotIn('age', array(33, 35))->get();
$this->assertEquals(4, count($users));

$users = User::whereNotNull('age')
->whereNotIn('age', array(33, 35))->get();
$this->assertEquals(3, count($users));
}

public function testWhereNull()
{
$users = User::whereNull('age')->get();
$this->assertEquals(1, count($users));

$users = User::whereNotNull('age')->get();
$this->assertEquals(8, count($users));
}

public function testOrder()
Expand All @@ -173,6 +189,37 @@ public function testOffset()
$this->assertEquals('Jane Doe', $users[0]->name);
}

public function testIncrements()
{
User::where('name', 'John Doe')->increment('age');
User::where('name', 'John Doe')->increment('age', 2, array('title' => 'user'));

$user = User::where('name', 'John Doe')->first();
$this->assertEquals(38, $user->age);
$this->assertEquals('user', $user->title);

User::where('name', 'John Doe')->decrement('age');
$num = User::where('name', 'John Doe')->decrement('age', 2, array('title' => 'admin'));

$user = User::where('name', 'John Doe')->first();
$this->assertEquals(35, $user->age);
$this->assertEquals('admin', $user->title);
$this->assertEquals(1, $num);

User::increment('age');
User::increment('age', 2);

$user = User::where('name', 'Mark Moe')->first();
$this->assertEquals(26, $user->age);

User::decrement('age', 2);
$num = User::decrement('age');

$user = User::where('name', 'Mark Moe')->first();
$this->assertEquals(23, $user->age);
$this->assertEquals(8, $num);
}

public function testAggregates()
{
$this->assertEquals(9, User::count());
Expand All @@ -183,6 +230,9 @@ public function testAggregates()

$this->assertEquals(35, User::where('title', 'admin')->max('age'));
$this->assertEquals(37, User::where('title', 'user')->max('age'));

$this->assertEquals(33, User::where('title', 'admin')->min('age'));
$this->assertEquals(13, User::where('title', 'user')->min('age'));
}

public function testGroupBy()
Expand Down